RXD and math
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1363 Accepted Submission(s): 747
Problem Description
RXD is a good mathematician.
One day he wants to calculate:
output the answer module 109+7 .
1≤n,k≤1018
p1,p2,p3…pk are different prime numbers
One day he wants to calculate:
∑i=1nkμ2(i)×⌊nki−−−√⌋
output the answer module 109+7 .
1≤n,k≤1018
μ(n)=1(n=1)
μ(n)=(−1)k(n=p1p2…pk)
μ(n)=0(otherwise)
p1,p2,p3…pk are different prime numbers
Input
There are several test cases, please keep reading until EOF.
There are exact 10000 cases.
For each test case, there are 2 numbers n,k .
There are exact 10000 cases.
For each test case, there are 2 numbers n,k .
Output
For each test case, output "Case #x: y", which means the test case number and the answer.
Sample Input
10 10
Sample Output
Case #1: 999999937
题意很清楚,就是给你n和k输出结果。
大言不惭的说数论,其实我是找到的规律。
当n=1,k=1时, ans=1
当n=2,k=1时, ans=2
当n=3,k=1时, ans=3
当n=1,k=2时, ans=1
当n=2,k=2时, ans=4
当n=3,k=2时, ans=9
然后,就不用再写下去了,ans不就是n^k吗?然后就用快速幂出结果就OK了,然而还是WA,这里的N和K应该可能很大,所以一开始就可以超过int,然后快速幂的第一步就爆了long long 所以,要先对n取模,再进行快速幂。
后来才知道那个U的函数叫做莫比乌斯函数,好像好多人都是找规律得到的答案。
代码实现:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))
using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e5+5;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
int main()
{
ll n,k,i,j,flag=1;
while(~scanf("%lld%lld",&n,&k))
{
n=n%mod;
ll ans=fpow(n,k);
cout<<"Case #"<<flag++<<": "<<ans<<endl;
}
return 0;
}