HDU2421Deciphering Password(数论&质因数分解)

来源:互联网 发布:淘宝代销分账怎么设置 编辑:程序博客网 时间:2024/06/07 05:27

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 21 14 7
 

Sample Output
Case 1: 36Case 2: 1Case 3: 4393
 
题意:给定a,b,a^b所有因子的因子个数的立方和.
如:
a=2,b=3; a^b=8;
8的因子有1,2,4,8;
所以ans=1+8+27+64.

设n=a1^p1*a2^p2*......*an^pn

则n的任意一个约数都具有a1^b1*a2^b2*......*an^bn(0<=bi<=pi)的形式。也就是说分别从1、ai、ai^2、ai^3。。。。ai^pi抽出一个相乘,而(1+a1+....a1^p1)*......*(1+an+....an^pn)恰有这种形式。ai^pi的因子为ai^i,其约数的个数为i+1.所以ai^pi的因子的约数个数的立方和为1+2^3+...+(pi+1)^3=(pi+1)^2*(pi+2)^2/4。

对于此题来说当a为质数时,那么答案就是(b+1)^2*(b+2)^2/4;a不是质数时,我们分解质因数,就是上面所讲的解法了。

/*********************************************************************> File Name: HDU2421.cpp> Author: Tailless> Mail: xihuanjin1@gmail.com ********************************************************************/#include <algorithm>#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <string>#include <vector>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <list>#include <map>#include <set>#define C 0.57721566490153286060651209#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;typedef long long LL;const int INF=0x3f3f3f3f;const double eps=1e-10;const double PI=acos(-1.0);const int maxn=1000009;const int maxm=1000009;bool vis[maxn];int prime[maxn];int num=0;LL ans=1;void init(){for(int i=2;i<=maxn;i++){if(!vis[i])prime[num++]=i;for(int j=0;j<num&&i*prime[j]<=maxn;j++){vis[i*prime[j]]=true;if(i%prime[j]==0)break;}}}int main(){init();int cas=1;int a,b;while(~scanf("%d%d",&a,&b)){ans=1;int k=0;while(a>1){int j=0;if(!vis[a]){j=b;ans=(ans*(j+1)*(j+2)/2)%10007;ans=(ans*(j+1)*(j+2)/2)%10007;break;}while(a%prime[k]==0){a/=prime[k];j++;}j*=b;ans=(ans*(j+1)*(j+2)/2)%10007;ans=(ans*(j+1)*(j+2)/2)%10007;k++;}printf("Case %d: %lld\n",cas++,ans);}    return 0;}




0 0
原创粉丝点击