Deciphering Password
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1550 Accepted Submission(s): 374
Problem Description
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?
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.
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
Source
代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
#define MAXN 1010
using namespace std;
int prim[MAXN],p[200];
void init()
{
int i, j, k = 0;
memset(prim,1,sizeof(prim));
for(i=2;i<35;++i)
{
if(prim[i])
for(j=i;j*i<MAXN;++j)
prim[i*j]=0;
}
for(i=2;i<MAXN;++i)
if(prim[i])
p[k++] = i;
}
int main()
{
int m,n,sum=1,ans,t=1,tmp,i,j;
init();
while(~scanf("%d%d",&m,&n))
{
sum=1;
for(i=0;i<169;i++)
{
tmp=0;
while(m%p[i]==0&&m!=1)
{
m/=p[i];
tmp++;
}
tmp=(tmp*n%10007+1)%10007;
tmp=tmp*(tmp+1)/2%10007;
sum=sum*(tmp*tmp%10007)%10007;
if(m==1) break;
}
if(m!=1)
{
tmp=(n+1)%10007;
tmp=tmp*(tmp+1)/2%10007;
sum=sum*(tmp*tmp%10007)%10007;
}
printf("Case %d: %d\n",t++,sum);
}
return 0;
}