HDU 6063 RXD and math 打表找规律 快速幂
2017-08-01 19:40
537 查看
题意没什么多说的,就是一个公式,计算这个公式的结果。
![](https://img-blog.csdn.net/20170801193718231?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG1oYWNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
按照这个公式先来打个表处理一下看能得到一个怎样的结果,会发现:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
const long long mod=1000000007;
long long vis[100005];
long long mu[100005];
long long prime[100005];
void Init()
{
memset(vis,0,sizeof(vis));
mu[1]=1;
int cnt=0;
for(int i=2;i<100000;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
mu[i]=-1;
}
for(int j=0;j<cnt&&i*prime[j]<100000;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j])
mu[i*prime[j]]=-mu[i];
else
{
mu[i*prime[j]]=0;
break;
}
}
}
}
int main()
{
long long n,k;
Init();
int i;
int Case=0;
long long ans;
long long p;
/*while(scanf("%lld%lld",&n,&k)!=EOF)
{
Case++;
cout<<"Case #"<<Case<<": ";*/
for(n=1;n<=10;n++)
{
for(k=1;k<=5;k++)
{
ans=0;
p=n;
for(i=0;i<k-1;i++)
{
p*=n;
}
for(i=1;i<=p;i++)
{
ans+=mu[i]*mu[i]*(long long)sqrt(p/i);
ans%=mod;
}
cout<<ans<<" ";
}
cout<<endl;
}
return 0;
}
![](https://img-blog.csdn.net/20170801193901373?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbG1oYWNt/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
通过表格可以发现,式子最后是符合n^k这个规律的,直接用快速幂得出结果就可以了。
(PS:其实比赛时候这题不是我做的,所以我也相当于已经知道是有规律了再去打的表→_→)
下面AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
const long long mod=1000000007;
long long qpow(long long a,long long b)
{
long long ans;
ans=1;
a=a%mod;
while(b>0)
{
if(b%2==1)
{
ans=(ans*a)%mod;
}
b=b/2;
a=(a*a)%mod;
}
return ans;
}
int main()
{
long long n,k;
int i;
int Case=0;
while(scanf("%lld%lld",&n,&k)!=EOF)
{
Case++;
cout<<"Case #"<<Case<<": ";
cout<<qpow(n,k)<<endl;
}
return 0;
}
按照这个公式先来打个表处理一下看能得到一个怎样的结果,会发现:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
const long long mod=1000000007;
long long vis[100005];
long long mu[100005];
long long prime[100005];
void Init()
{
memset(vis,0,sizeof(vis));
mu[1]=1;
int cnt=0;
for(int i=2;i<100000;i++)
{
if(!vis[i])
{
prime[cnt++]=i;
mu[i]=-1;
}
for(int j=0;j<cnt&&i*prime[j]<100000;j++)
{
vis[i*prime[j]]=1;
if(i%prime[j])
mu[i*prime[j]]=-mu[i];
else
{
mu[i*prime[j]]=0;
break;
}
}
}
}
int main()
{
long long n,k;
Init();
int i;
int Case=0;
long long ans;
long long p;
/*while(scanf("%lld%lld",&n,&k)!=EOF)
{
Case++;
cout<<"Case #"<<Case<<": ";*/
for(n=1;n<=10;n++)
{
for(k=1;k<=5;k++)
{
ans=0;
p=n;
for(i=0;i<k-1;i++)
{
p*=n;
}
for(i=1;i<=p;i++)
{
ans+=mu[i]*mu[i]*(long long)sqrt(p/i);
ans%=mod;
}
cout<<ans<<" ";
}
cout<<endl;
}
return 0;
}
通过表格可以发现,式子最后是符合n^k这个规律的,直接用快速幂得出结果就可以了。
(PS:其实比赛时候这题不是我做的,所以我也相当于已经知道是有规律了再去打的表→_→)
下面AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
const long long mod=1000000007;
long long qpow(long long a,long long b)
{
long long ans;
ans=1;
a=a%mod;
while(b>0)
{
if(b%2==1)
{
ans=(ans*a)%mod;
}
b=b/2;
a=(a*a)%mod;
}
return ans;
}
int main()
{
long long n,k;
int i;
int Case=0;
while(scanf("%lld%lld",&n,&k)!=EOF)
{
Case++;
cout<<"Case #"<<Case<<": ";
cout<<qpow(n,k)<<endl;
}
return 0;
}
相关文章推荐
- HDU 6063 RXD and math【快速幂】【打表找规律】
- (2017多校训练第三场)HDU - 6063 RXD and math 找规律 + 快速幂
- HDU 6063 RXD and math(快速幂)
- (打表或数学)HDU 6063 RXD and math
- hdu 6063 RXD and math(快速幂)
- HDU 6063 RXD and math+(快速幂)+多校联赛第三场
- hdu 6063 RXD and math (规律题)
- HDU 6063 17多校3 RXD and math(暴力打表题)
- HDU 6063 RXD and math (打表)
- HDU 6063-RXD and math
- HDU 6063 RXD and math
- 多校3 HDU - 6063 RXD and math 数学
- RXD and math(HDU 6063 快速幂)
- Hdu 6063 - RXD and math(思维)
- HDU-6063-RXD and math-
- RXD and math HDU - 6063
- 2017杭电多校联赛第三场-RXD and math (hdu6063) 找规律快速幂
- HDU 6063 RXD and math【数学】
- HDU 6063 RXD and math
- 17暑假多校联赛3.8 HDU 6063 RXD and math