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-1be 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, a0 = 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
题意:N=A^B,求N的因子的总数的立方和M
比如:N=8,因子有1,2,4,8,共四个,M=1^3+2^3+3^3+4^3
基本思路:
先用线性欧拉筛求出A^B唯一分解的素因子指数
再根据积性函数的性质g(m*n)=g(m)*g(n) 求出立方和M
见下图解析:
本题在唯一分解时只对A分解,因为B是幂,所以只要对A进行了唯一分解,求出素因子指数,再乘以B即可
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=100005;
int prime[maxn];
bool isPrime[maxn];
int cnt=0;
//线性欧拉筛
void Init()
{
for(int i=2;i<maxn;i++)
{
if(!isPrime[i]) prime[cnt++]=i;
for(int j=0;j<cnt&&(ll)i*prime[j]<maxn;j++)
{
isPrime[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
int main()
{
Init();
ll res[10005];
ll a,b;
int p=1;
while(~scanf("%lld%lld",&a,&b))
{
int t=0;
memset(res,0,sizeof(res));
//求唯一分解的素因子的指数
for(int i=0;i<cnt&&prime[i]*prime[i]<=a;i++)//一定要有这个条件 prime[i]<=a
{
if(a%prime[i]==0)
{
a/=prime[i];
res[t]++;
while(a%prime[i]==0)
{
a/=prime[i];
res[t]++;
}
t++;
}
}
if(a>1)
{
res[t++]++;
}
//积性函数+质因数分解
ll tmp=1;
ll ans=1;
for(int i=0;i<t;i++)
{
tmp=(res[i]*b+1)*(res[i]*b+2)/2;
tmp%=10007;
tmp=tmp*tmp%10007;
ans=ans*tmp%10007;
}
printf("Case %d: %lld\n",p++,ans);
}
return 0;
}