Problem Description

Considera positive integer X,and let S be the sum of all positive integer divisors of2004^X. Your job is to determine S modulo 29 (the rest of the division of S by29).



Take X = 1 for an example. The positive integer divisors of 2004^1 are 1, 2, 3,4, 6, 12, 167, 334, 501, 668, 1002 and 2004. Therefore S = 4704 and S modulo 29is equal to 6.

Input

Theinput consists of several test cases. Each test case contains a line with theinteger X (1 <= X <= 10000000). 



A test case of X = 0 indicates the end of input, and should not be processed.

Output

Foreach test case, in a separate line, please output the result of S modulo 29.

Sample Input

1

10000

0

Sample Output

6

10

/***************************

解题思路:

参考大神的:http://www.cnblogs.com/372465774y/archive/2012/10/22/2733977.html

体重主要用到除数和函数。

除数和函数 :F(n) 求n的约数的和 ( 约数大于等于1 小于n )

除数和函数是一个积性函数,满足性质 :当m , n 互质时, f(m*n) = f(m) * f(n)

如果 p 是一个素数,则 f(p^n) = 1 + p + p^2 +p^3 +p^4 + .... + p^(n-1) + p^n = (p^(n+1) -1)/p-1  (等比数列求和)

则题目中  f(2004^n) = f(2^(2*n)) * f(3^n) * f(167^n)

= (2^(2*n+1) -1) * (3^(n+1) -1)/2  *(167^(n+1) -1)/166

用到乘法逆元:(同余性质)

a^k/d = a^k*(d-1)     d-1 即为d的逆元。   3的逆元为15    
167 的逆元为18

具体参考:http://baike.baidu.com/link?url=pcN2WyxgeFP9isdQxd9bTobeiRH3MnXcrdIwHh7jCBsYkVyTfFhF5QiS-d8-HgNgslVb334pgqkClTiIp359Xa

然后还要用到 快速幂模:转换为位运算,这题要用这个,一般的会超时,具体看代码吧。

*************************/

Code:

#include<stdio.h>
using namespace std;
int Mod(int a,int b)// 快速幂模函数
{
int t = 1;
while(b)
{
if(b&1)
t = t*a%29;
b>>=1;
a = a*a%29;
}
return t;
}
int main()
{ int n,a,b,c;
while(scanf("%d",&n)&&n)
{
a=(Mod(2,2*n+1)-1);
b=(Mod(3,n+1)-1)*15;
c=(Mod(22,n+1)-1)*18;
printf("%d\n",a*b*c%29);
}
return 0;
}

Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)的更多相关文章

  1. HDU 1061.Rightmost Digit-规律题 or 快速幂取模

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  2. 【数论】【组合数】【快速幂】【乘法逆元】洛谷 P2265 路边的水沟

    从左上角到右下角,共经过n+m个节点,从其中选择n各节点向右(或者m各节点向下),所以答案就是C(n+m,n)或者C(n+m,m),组合数暴力算即可,但是要取模,所以用了乘法逆元. #include& ...

  3. hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

    Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  4. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  5. hdu 1097 A hard puzzle 快速幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...

  6. XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】

    1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS  Memory Limit: 128 MBSubmit: 45  Solved: 8[Submit][Status][W ...

  7. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  8. hdu 2065 "红色病毒"问题(快速幂求模)

    n=1  --> ans = 2 = 1*2 = 2^0(2^0+1) n=2  -->  ans = 6 = 2*3 = 2^1(2^1+1) n=3  -->  ans = 20 ...

  9. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  10. 《Java语言实现快速幂取模》

    快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算过程中最麻烦的就是我们的5^1003这个过程 ...

随机推荐

  1. 百度参投 Uber中国12亿美元融资已到账

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  2. 向架构师进军---&gt;怎样编写软件架构文档

    假设你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",增加这个PM.架构师的大家庭 问:为什么要编写软件架构文档,它的优点是什么? 答: 有文档的架构有助于不同利益相关 ...

  3. 将View兑换Bitmap

    /** * 中间View保罗转化成Bitmap * */ private Bitmap saveViewBitmap(View view) { // get current view bitmap v ...

  4. [Android]使用platform密钥来给apk文件签名的命令

    1.使用platform密钥对apk进行签名 1.1.进入<Android_Source_Path>/build/target/product/security,找到[platform.p ...

  5. string的操作

    除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...

  6. 配置servers时,错误:Setting property 'source' to 'org.eclipse.jst.jee.server:hczm' did not find a matching property

    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.e ...

  7. 转:what is TWO_TASK

    https://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:89412348059 You Asked what is t ...

  8. centos6.5 apache+tomcat结合 负载均衡

    一. 负载均衡:根据 uri 表达式把请求分发给各个服务器处理.如:/*.jsp = lb_s  就是说把所有jsp请求交给lb_s , 当然它是个Tomcat. 二. 安装配置mod_jk    m ...

  9. ThinkPHP函数详解:import方法

    import方法是ThinkPHP框架用于类库导入的封装实现,尤其对于项目类库.扩展类库和第三方类库的导入支持,import方法早期的版本可以和java的import方法一样导入目录和通配符导入,后来 ...

  10. 转载-优秀程序员的十个tips

    理解技术债务 技术债务就像信用卡一样,会有很高的利息,时间越长,修复所化的代价就越大.团队应该培养一种保证设计质量的文化,鼓励重构.同时应当鼓励其它有关代码质量的实践. 保持对原理的好奇心 做Andr ...