Sumdiv(poj1845)(数学)

/*
http://poj.org/problem?id=1845
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 11846 Accepted: 2835
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). 
Source


Romania OI 2002
解析:求A^B得所有约数之和,即因子之和,并其结果模去9901
思路:
递归+分治
这里运用了整数划分定理和求约数和公式
1.整数唯一分解定理:任何正整数有且只有一种方式写出素因子的乘积表达式
即:A=(p1^k1)*(p2*k2)*(p3^k3)*.....(pn*kn);
(pi均为素数)
2.约分和公式:(从定理1以及排列组合原理可得理解)
s=(1+p1^1+....+p1^k1)*(1+p2^1+.....+p2^k2).....(1+pn^1+....+pn^kn);
3.利用递归分治法求s:令sum=1+p^1+p^2+...p^n则
(1)n%2==1:sum=(1+p+p^2+....p^(n/2))*(1+p^(n/2+1));
  (2)n%2==0:sum=(1+p+p^2+...p^(n/2-1))*(1+p^(n/2+1))+p^(n/2)
Problem Result Memory Time Language Length Submit Time
Accepted 1836 KB 47 ms C++ 1631 B 2013-07-24 16:56:20
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=100000+10;
bool isprime[maxn];
long long  prime[maxn],k[maxn],p[maxn];//
long long cnt,t;
int n,m;
const int mod=9901;
void makeprime()//构造素数表
{
	memset(prime,0,sizeof(prime));
	memset(isprime,true,sizeof(isprime));
	t=0;
	for(int i=2;i<=maxn;i++)
	{   if(isprime[i])
	     prime[t++]=i;
		for(int j=0;j<t&&i*prime[j]<=maxn;j++)
		{
			isprime[i*prime[j]]=false;
			if(i%prime[j]==0)
			 break;
		}
	}
//	for(int i=0;i<t;i++)
//	printf("%d\n",prime[i]);
}
void dividedata()//整数划分
{
	memset(k,0,sizeof(k));
	int i,j;
	for(i=0;i<t&&prime[i]<=n;i++)
	{
		if(n%prime[i]==0)
		{p[++cnt]=prime[i];
		while(n%prime[i]==0)
		 {k[cnt]++;
		n=n/prime[i];
		 }
		}
	}
	if(n!=1)//还需要对余下的数进行判断
	{
		p[++cnt]=n;
		k[cnt]=1;
	}
}
int pow_mod(long long a,long long  b)
{
	if(b==0)
	 return 1;
	 int x=pow_mod(a,b/2);
	 long long ans=(long long )x*x%mod;
	 if(b%2==1)
	 ans=ans*a%mod;
	 return (int)ans;
}
int sum_mod(long long  a,long long b)
{
	if(b==0)
	 return 1;
	 long long s1;
	long long s2;
	 if(b%2==1)
	 {
	s1=sum_mod(a,b/2);
	s2=pow_mod(a,b/2+1);
	 	return (s1+s1*s2)%mod;
	 }
	 else
	 {
     s1=sum_mod(a,b/2-1);
     s2=pow_mod(a,b/2);
     return (s1+s2+a*s1*s2)%mod;
	 }
}
int main()
{
	 //freopen("out.txt","w",stdout);
	makeprime();
	int i,j;
scanf("%d%d",&n,&m);
		if(n==0)
		{printf("0\n");
		return 0;
		}
		if(m==0||n==1)
		{
			printf("1\n");
			return 0;
		}
		cnt=0;
		dividedata();
		int ans=1;
		for(i=1;i<=cnt;i++)
		  ans=ans*sum_mod(p[i],k[i]*m)%mod;
		  printf("%d\n",ans);
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值