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

1054 - Efficient Pseudo Code
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 power m)

    let sum = summation of all thedivisors of p

    let result = sum MODULO1000,000,007

}

Now given n and m you have to find the desiredresult from the pseudo code. For example if n = 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 power m)

    so, p = 144

    let sum = summation of all thedivisors of p

    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 a 32bit 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-10
using 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;}
//head
struct 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;
	else
	return (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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值