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. JQuery Dialog 禁用X按钮关闭对话框,-摘自网络

    JQuery Dialog 禁用X按钮关闭对话框,禁用ESC键关闭代码如下:$("#div1").dialog({   closeOnEscape: false,   open: ...

  2. 局域网内Linux服务器时间同步

    局域网内Linux服务器时间同步   1.将一台能够上网的服务器作为时间服务器:  # /usr/bin/rdate -s time-b.timefreq.bldrdoc.gov //将时间服务器与互 ...

  3. JavaScript高级程序设计—阅读笔记(第一部分)

    第一章 JavaScript简介1.JavaScript组成(核心(ECMAScript),文档对象模型(DOM),浏览器对象模型(BOM))2.文档对象模型(DOM),提供访问和操作网页内容的方法和 ...

  4. jqgrid使用简单记录

    我要为id为jqGrid的table使用jqgrid插件. $("#jqGrid").jqGrid({ url: 'data/test.json', mtype: "GE ...

  5. Oracle--SQL Developer创建连接及使用

    安装好Oracle之后,有几种方式可以来管理Oracle中的数据库,首先就是登陆网页版的界面:https://localhost:1158/em,这种方式管理的东西太多,使用起来有点不方便,第二种方式 ...

  6. FZOJ2110 star(DFS)

    Overpower often go to the playground with classmates. They play and chat on the playground. One day, ...

  7. 【转】oracle创建表空间

    原文:http://www.cnblogs.com/netsql/articles/1745978.html 注意点: 1.如果在PL/SQL 等工具里打开的话,直接修改下面的代码中[斜体加粗部分]执 ...

  8. [RxJS] Drag and Drop example

    Improving our mouse drag event Our mouse drag event is a little too simple. Notice that when we drag ...

  9. insert例子

    11.20 使用insert代替下标操作. #include<iostream> #include<map> #include<string> #include&l ...

  10. base查找方法的实现JAVA

    import java.util.List; import java.util.ArrayList; import java.util.Scanner; /*在一个有序数组中查找一个数的过程,模拟二分 ...