您的位置:首页 > 其它

hdu 2421 分解质因子

2015-08-05 11:50 225 查看


Deciphering Password

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768
K (Java/Others)



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 = AB, where 1 <= A, B <= 1000000, and a0, a1, a2, …, ak-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 = AB = 8, a0 = 1, a1 = 2, a2 = 4, a3 = 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


定理一:任何一个大于2的数都可以分解成 num=p1^e1*p2^e2*p3^e3*...*pn^en,这个数的因子个数为(e1+1)*(e2+1)*...*(en+1)

定理二:1^3+2^3+...+N^3=((n*(n+1)/2))^2
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=1010;
const int mod=10007;
bool vis[maxn];
int p[170];
int cnt;
void unit()
{
memset(vis,0,sizeof(vis));
cnt=0;
for(int i=2;i<=maxn;i++)
{
if(!vis[i])
{
p[cnt++]=i;
for(int j=i+i;j<=maxn;j+=i)
vis[j]=1;
}
}
}
int main()
{
int a,b;
unit();
long long ans;
int cas=1;long long count;
while(scanf("%d%d",&a,&b)!=EOF)
{ ans=1;
for(int i=0;i<cnt;i++)
{   count=0;
if(a%p[i]==0)
{
while(a%p[i]==0)
{
count++;
a/=p[i];
}
}
count=(count*b+1)%mod;
count=(count*(count+1)/2)%mod;
ans=ans*(count*count%mod)%mod;
if(a==1) break;
}
if(a!=1)
{
count=(b+1)%mod;
count=count*(count+1)/2%mod;
ans=ans*(count*count%mod)%mod;
}
printf("Case %d: %I64d\n",cas++,ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: