博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj1845 Sumdiv
阅读量:2445 次
发布时间:2019-05-10

本文共 2016 字,大约阅读时间需要 6 分钟。

Sumdiv
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 15033   Accepted: 3706

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是素数,我们可以知道a^b的所有因子是1,a,a^2,a^3,...,a^b,

则a^b的所有因子之和为1+a+a^2+a^3+a^4+...+a^b;

如果a不是素数,那么我们可以将a转换为由多个素数组成的合数即a=(p[0]^n[0])*(p[1]^n[1])*(p[2]^n[2])*...*(p[k]^n[k]),(p[0],p[1],p[2],...,p[n]都是素数),

那么a^b转换为p[0]^(n[0]*b) * p[1]^(n[1]*b) * p[2]^(n[2]*b) * ... * p[n]^(n[n]*b),

则所有因子之和为[(1+p[0]+p[0]^2+...+p[0]^(n[0]*b) )*(1+p[1]+p[1]^2+...+p[1]^(n[1]*b) )*...*(1+p[n]+p[n]^2+...+p[n]^(n[n]*b) )

参考代码:

#include 
#include
#include
typedef long long ll;using namespace std;#define m 9901ll mod_pow(ll a,ll b){ //快速幂 ll ans=1; while (b>0){ if (b&1) ans=ans*a%m; a=a*a%m; b>>=1; } return ans;} ll sum(ll p,ll n) //递归二分求 (1 + p + p^2 + p^3 +...+ p^n)%mod { //奇数二分式 (1 + p + p^2 +...+ p^(n/2)) * (1 + p^(n/2+1)) if(n==0) //偶数二分式 (1 + p + p^2 +...+ p^(n/2-1)) * (1+p^(n/2+1)) + p^(n/2) return 1; if(n%2) //n为奇数, return (sum(p,n/2)*(1+mod_pow(p,n/2+1)))%m; else //n为偶数 return (sum(p,n/2-1)*(1+mod_pow(p,n/2+1))+mod_pow(p,n/2))%m; } /* ll sum(ll a,ll b){ //求 1+a^1+a^2+...+a^b的值 ll ans=mod_pow(a,b+1); return (ans-1)/(a-1);}*/int main(){ int a,b; int p[10000],n[10000]; while (cin>>a>>b){ int k=0; memset(n,0,sizeof(n)); for (int i=2;i<=a;i++){ //将a转换为(p[0]^n[0])*(p[1]^n[1])*(p[2]^n[2])*...*(p[k]^n[k]) if (a%i==0){ p[k]=i; while (a%i==0){ n[k]++; a/=i; } k++; } } ll ans=1; for (int i=0;i

转载地址:http://dfbqb.baihongyu.com/

你可能感兴趣的文章
第13章 术 语 大 全 (9) (转)
查看>>
人月神话读书笔记(二) (转)
查看>>
附录 UML元模 (转)
查看>>
Microsoft Office XP 智能标记的安装和安全性 (转)
查看>>
A Brief Look at C++ 中文版 (转)
查看>>
JBuilder Editor中光标不能正确定位问题的解决 (转)
查看>>
XML加ASP实现网页“本地化” (转)
查看>>
Java中的异步网络编程 (转)
查看>>
用于核心模式驱动程序的网络体系结构(1) (转)
查看>>
More Effective C++ 条款20 (转)
查看>>
一个程序员的爱恋 (转)
查看>>
足球战术->边锋之Decorator篇 (转)
查看>>
编写优质无错代码(1) (转)
查看>>
MySQL 4.1.0 中文参考手册 --- 6.3 用于 SELECT 和 WHERE 子句的函数 (1) (转)
查看>>
vs.net beta 2中利用DataGrid分页详解 (转)
查看>>
Process-Display-Process (PDP) pattern (转)
查看>>
基于构件复用的软件方法与COM支持 (转)
查看>>
DELPHI中使用API函数详解 (转)
查看>>
Single Entry Point to EJB Layer (转)
查看>>
InsideJVM(3)--Method area(方法区) (转)
查看>>