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
Author: 2008 Asia Hangzhou Regional Contest Online
这道题是杭州赛区的网络预选赛的赛题,当时打死也没过,现在也没有当时的代码了,不知道哪里错了,今天一看想起了一个公式就是
1^3+2^3+……n^3=(n)^2*(n+1)*^2/4;记得这个公式还是高二老师教会的着,具体证明我们可以数学归纳法证明一下,简单的很自己证明吧!
而那时自己网络赛的时候不知道怎么推导出过10007是个循环,可以把b的取模,而现在有了这个公式就可以一步搞定为什么对b取模!
要解题就必须求出a ^b的因子数,这不是难点
证明如下:
N=a1^b1*a2^b2*……*aN^bN;根据排列组合原理我们知道N的因子数可以如下解出:
即(b1+1)*(b2+1)*.......*(bN+1);而现在是a^b只需要把(b1*b+1)*(b2*b+1)*.......*(bN*b+1);极为因子数:
而这个时候我们知道1<=a,b<=1000000超出整形范围:我们进一步优化得到
因为有:m*n%x=(m%x*n%x);(m+n)%x=(m%x+n%x);
所以我们必须在过程中优化算法!
这样这道题目就出来了!当然求因子时必不可少素数打表!可以优化打表1000,减少时间!当然还可以把1-10007的密码表打出,更优时间啊
素数打表代码如下:
memset(prim,0,sizeof(prim));
for(sign=0,i=2;i<MAXN;i++)
if(!prim[i]){
num[sign++]=i;
for(j=0;i*j<MAXN;j++)
prim[i*j]=true;
}
posted on 2009-03-23 19:20
KNIGHT 阅读(514)
评论(0) 编辑 收藏 引用