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%MOD
记得对n先做预处理取模,否则快速幂也救不了啊
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 #include<cstring> 5 using namespace std; 6 const long long MOD=1e9+7; 7 8 long long quickmod(long long a,long long b,long long m) 9 { 10 long long ans = 1; 11 while(b)//用一个循环从右到左遍历b的所有二进制位 12 { 13 if(b&1)//判断此时b[i]的二进制位是否为1 14 { 15 ans = (ans*a)%m;//乘到结果上,这里a是a^(2^i)%m 16 b--;//把该为变0 17 } 18 b/=2; 19 a = a*a%m; 20 } 21 return ans; 22 } 23 24 int main() 25 { 26 long long n,k; 27 int t=1; 28 while(~scanf("%lld%lld",&n,&k)) 29 { 30 printf("Case #%d: ",t++); 31 n%=MOD; 32 printf("%lld ",quickmod(n,k,MOD)); 33 } 34 return 0; 35 }
打表程序如下:
1 #include<cstdio> 2 #include<iostream> 3 #include<cmath> 4 using namespace std; 5 6 #define MOD 1000000000+7 7 8 bool panduan (long long num) 9 { 10 long long i; 11 for(i=2;i<=sqrt((double)num)+1;i++) 12 { 13 if(num%(i*i)==0) 14 return true; 15 } 16 return false; 17 } 18 19 int main() 20 { 21 int n,k; 22 long long num; 23 long long res=0; 24 for(int n=1;n<=10;n++) 25 for(int k=1;k<=10;k++) 26 { 27 res=0; 28 num=pow((double)n,(double)k); 29 for(int i=1;i<=num;i++) 30 { 31 if(!panduan(i)) 32 res+=(long long)(sqrt((double)(num/i))); 33 res%=MOD; 34 } 35 printf("%d %d %lld ",n,k,res); 36 } 37 return 0; 38 }
每一行三个数字分别表示n,k,res