LightOJ 1054 - Efficient Pseudo Code (求n^m的因子和)

来源:互联网 发布:在淘宝开店怎么样 编辑:程序博客网 时间:2024/05/10 21:10
1054 - Efficient Pseudo Code
  PDF (English)StatisticsForum
Time Limit: 1 second(s)Memory Limit: 32 MB

Sometimes it's quite useful to write pseudo codes forproblems. Actually you can write the necessary steps to solve a particularproblem. In this problem you are given a pseudo code to solve a problem and youhave to implement the pseudo code efficiently. Simple! Isn't it? :)

pseudo code

{

    take two integers n and m

    let p = n ^ m (n tothe powerm)

    let sum = summation of all thedivisors ofp

    let result = sum MODULO1000,000,007

}

Now given n and m you have to find the desiredresult from the pseudo code. For example ifn = 12 and m = 2.Then if we follow the pseudo code, we get

pseudo code

{

    take two integers n and m

    so, n = 12 and m = 2

    let p = n ^ m (n tothe powerm)

    so, p = 144

    let sum = summation of all thedivisors ofp

    so, sum = 403, since the divisors ofp are 1, 2, 3, 4, 6, 8, 9, 12, 16, 18, 24, 36, 48, 72, 144

    let result = sum MODULO1000,000,007

    so, result = 403

}

Input

Input starts with an integer T (≤ 5000),denoting the number of test cases.

Each test case will contain two integers, n (1 ≤ n)and m (0 ≤ m). Each of n and m will be fit into a32bit signed integer.

Output

For each case of input you have to print the case number andthe result according to the pseudo code.

Sample Input

Output for Sample Input

3

12 2

12 1

36 2

Case 1: 403

Case 2: 28

Case 3: 3751

 






题意:看题就能知道了,求n^m的因子和mod1000000007

思路:卡时间,普通的分解因子不行,所以说打个素数表优化分解,然后优化下等比数列就好了



ac代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<stack>#include<set>#include<queue>#include<vector>#include<iostream>#include<algorithm>#define MAXN 1000100#define LL long long#define ll __int64#define INF 0x7fffffff#define mem(x) memset(x,0,sizeof(x))#define PI acos(-1)#define eps 1e-10using namespace std;int gcd(int a,int b){return b?gcd(b,a%b):a;}int lcm(int a,int b){return a/gcd(a,b)*b;}LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}//headstruct s{int num;int cnt;}p[MAXN];int prime[MAXN];int v[MAXN];int q;LL fun(LL a,LL n,LL MOD){if(n==0)return 1;if(n%2)return ((1+powmod(a,n/2+1,MOD))%MOD*fun(a,n/2,MOD)%MOD)%MOD;elsereturn (powmod(a,n/2,MOD)+(1+powmod(a,n/2+1,MOD))%MOD*fun(a,(n-1)/2,MOD)%MOD)%MOD;}void db(){int i,j;q=0;mem(v);for(i=2;i<=100000;i++){if(!v[i]){prime[q++]=i;for(j=i*2;j<=100000;j+=i)v[j]=1;}}}int main(){db();LL a,b,i;int tt,cas=0;scanf("%d",&tt);while(tt--){scanf("%lld%lld",&a,&b);int x=a;int ccnt=0;for(i=0;i<q&&prime[i]*prime[i]<=x;i++){if(x%prime[i]==0){p[ccnt].num=prime[i];int t=0;while(x%prime[i]==0)                x/=prime[i],t++;p[ccnt++].cnt=t;}if(x==1)break;}if(x!=1){p[ccnt].num=x;p[ccnt].cnt=1;ccnt++;}LL ans=1;for(i=0;i<ccnt;i++)ans=(ans*fun(p[i].num,p[i].cnt*b,1000000007))%1000000007;printf("Case %d: ",++cas);printf("%lld\n",ans);}return 0;}


0 0