Deciphering Password
Xiaoming has just come up with a new way for encryption, by calculating the key from a publicly viewable number in the following way:
Let the public key N = A^ B, where 1 <= A, B <= 1000000, and a 0, a 1, a 2, …, a k-1 be the factors of N, then the private key M is calculated by summing the cube of number of factors of all ais. For example, if A is 2 and B is 3, then N = A^B = 8, a 0 = 1, a 1 = 2, a 2 = 4, a 3 = 8, so the value of M is 1 + 8 + 27 + 64 = 100.
However, contrary to what Xiaoming believes, this encryption scheme is extremely vulnerable. Can you write a program to prove it?
Input
There are multiple test cases in the input file. Each test case starts with two integers A, and B. (1 <= A, B <= 1000000). Input ends with End-of-File.
Note: There are about 50000 test cases in the input file. Please optimize your algorithm to ensure that it can finish within the given time limit.
Output
For each test case, output the value of M (mod 10007) in the format as indicated in the sample output.
Sample Input
2 2
1 1
4 7
Sample Output
Case 1: 36
Case 2: 1
Case 3: 4393
题意:求A^B的所有因子的因子数的立方的和
例:if A is 2 and B is 3, then N = A^B = 8, a 0 = 1, a 1 = 2, a 2 = 4, a 3 = 8, so the value of M is 1 + 8 + 27 + 64 = 100.
1的因子数是1 (1),2的因子数是2 (1,2), 4的因子数是3 (1,2,4), 8的因子数是4 (1,2,4,8)
所以M=
1
3
1^3
13+
2
3
2^3
23+
3
3
3^3
33+
4
3
4^3
43=100
知识储备
一个很重要的公式
1
3
1^3
13+
2
3
2^3
23+
3
3
3^3
33+······+
n
3
n^3
n3=
(
n
∗
(
n
+
1
)
2
)
2
\left(\frac{n*(n+1)}{2}\right)^2
(2n∗(n+1))2
题目给的数据范围是1e6,但是只需要筛1~1e3就行,因为任何一个数n都最多只可能有一个质因子超过根号n(要不乘积就大于n了)
而本题我们并不需要n的质因子的值,而只需要每个质因子的数量,于是,打一个范围1000的素数表,再用质因数分解的方式去分解n,如果n还有别的因子,那么res数组中只需要再加一个指数为1的质因子,时间优化了很多。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e3+5;
const int mod=10007;
typedef long long ll;
bool p[maxn];
int prime[maxn],cnt=0;
ll res[maxn];
void isp(){ //线性筛
p[0]=p[1]=true;
for(int i=2;i<maxn;i++){
if(!p[i]) prime[cnt++]=i;
for(int j=0;j<cnt&&(ll)i*prime[j]<maxn;j++){
p[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
int main(){
isp();
int a,b;
int Case=0;
while(~scanf("%d%d",&a,&b)){
int t=0;
memset(res,0,sizeof(res));
for(int i=0;a!=1&&i<cnt;i++){
while(a%prime[i]==0){
res[t]++;//prime[i]的指数++
a/=prime[i];
}
t++;
}
if(a!=1){
res[t++]=1;
}
ll ans=1;
ll s;
b%=mod;
for(int i=0;i<t;i++){
s=((b*res[i]+1)*(b*res[i]+2)/2)%mod;
s=(s*s)%mod;
ans=(ans*s)%mod;
}
printf("Case %d: %lld\n",++Case,ans);
}
return 0;
}