POJ 1845 Sumdiv (大数据因子和)

Sumdiv
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 17287 Accepted: 4343

Description

Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S modulo 9901 (the rest of the division of S by 9901).

Input

The only line contains the two natural numbers A and B, (0 <= A,B <= 50000000)separated by blanks.

Output

The only line of the output will contain S modulo 9901.

Sample Input

2 3

Sample Output

15

Hint

2^3 = 8.
The natural divisors of 8 are: 1,2,4,8. Their sum is 15.

15 modulo 9901 is 15 (that should be output).



题意:求出A^B的因子和


思路:根据唯一分解定理得A=p1^k1*p2^k2*...*pn^kn,所以说A^B=p1^(k1*B)*p2^(k2*B)*...*pn^(kn*B),所以说根据因子和公式得(1+p1+p1^2+...+p1^(k1*B))*(1+p2+p2^2+...+p2^(k2*B))*...*(1+pn+pn^2+...+pn^(kn*B)),结果就是一个个等比数列的乘积,但是数据是5e8,这样直接按照等比数列的求和公式写会超时或者RE,所以说要优化等比数列求和,用折半二分递归的方法求等比数列的和,这里就先不说了,等后来总结的时候再深究。



ac代码:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 100010
#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 fun(int a,int n,int 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;
}
int main()
{
	int a,b,i;
	while(scanf("%d%d",&a,&b)!=EOF)
	{
		int x=a;
		int ccnt=0;
		for(i=2;i*i<=x;i++)
		{
			if(x%i==0)
			{
				p[ccnt].num=i;
				int t=0;
				while(x%i==0)
                x/=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,9901))%9901;
		printf("%d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值