Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your job is to determine S modulo 29 (the rest of the division of S by 29).

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 29 is equal to 6.

InputThe input consists of several test cases. Each test case contains a line with the integer X (1 <= X <= 10000000).

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

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

Sample Input

1
10000
0

Sample Output

6
10

唯一分解定理:

  一个整数A一定能被分成:A=(P1^K1)*(P2^K2)*(P3^K3).....*(Pn^Kn)的形式。其中Pn为素数。

约数和公式

  对于一个已经被分解的整数A=(P1^K1)*(P2^K2)*(P3^K3).....*(Pn^Kn),有约数和S=(1+P1+P12+P13+.....P1k1)*.....(1+Pn+Pn2+Pn3+.....Pnkn)。

等比数列求和公式

  SUM=P1(1- P1^n)/(1-P1)=P1(P1^n  -1)/(P1-1)

S=SUM1+SUM2+......+SUMn

 对于此题ans=2^(2n+1)-1+(3^(n+1)-1)/2+(167^(n-1)-1)/166

乘法逆元:

  如果ax≡1 (mod p),且gcd(a,p)=1(a与p互质),则称a关于模p的乘法逆元为x。

扩展欧几里得在这里就不多说了。这里说一下费马小定理:

  假如a是整数,p为素数,则a^p-a为p的倍数。  由此可得a^p   - a=1 mod p  =>a^p=a mod p  =>a^(p-1) =1 mod p,结合逆元的定义,a*x=1 mod p。则逆元x=a^(p-2) mod p。

取模不可用除法,所以ans*乘法逆元,剩下的就是求出逆元可解。乘法逆元可由扩展欧几里得算法求得,也可有费马小定理求得。
  欧拉定理求逆元:a^(phi(m)-1);

代码如下:
#include<iostream>
#include<cstdio>
#define LL long long
#define mod 29
using namespace std;
LL pow(LL a,LL n)
{
LL base=a,ret=1;
while(n)
{
if(n&1) ret=(ret*base)%mod;
base=(base*base)%mod;
n>>=1;
}
return ret%mod;
}
int main()
{ LL T,x;
while(~scanf("%lld",&x),x)
{
LL rev=pow(13,27);//逆元
LL res=(pow(2,2*x+1)-1)*(pow(3,x+1)-1)*(pow(22,x+1)-1);
printf("%lld\n",res*rev%mod);
}
}

  

												

hdu_1452_Happy 2004 (乘法逆元的更多相关文章

  1. Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)

    Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...

  2. 数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

  3. HDU 1452 (约数和+乘法逆元)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1452 题目大意:求2004^X所有约数和,结果mod 29. 解题思路: ①整数唯一分解定理: 一个 ...

  4. Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)

    题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...

  5. 51nod1256(乘法逆元)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256 题意:中文题诶~ 思路: M, N 互质, 求满足 K ...

  6. 【板子】gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数

    1.gcd int gcd(int a,int b){ return b?gcd(b,a%b):a; } 2.扩展gcd )extend great common divisor ll exgcd(l ...

  7. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

  8. Codeforces 543D Road Improvement(树形DP + 乘法逆元)

    题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...

  9. HDU 1576 (乘法逆元)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思 ...

随机推荐

  1. 创建weblogic domain

    做成了PDF文档 这里可下载可以看 config.sh 命令目录 weblogic/Oracle/Middleware/wlserver_10.3/common/bin├── commEnv.sh├─ ...

  2. HDU 5353—— Average——————【贪心+枚举】

    Average Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total S ...

  3. iis部署网站打不开

    事件查看器 1.crypt32报错 2.ASP.NET 2.0错误 解决办法: 1.crpt32报错,根本原因是计算机无法连接更新根证书所致,可能造成死循环. [1]必须连接到 Internet 或关 ...

  4. springboot整合mybatis+oracle

    第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择sp ...

  5. array_map()关于回调函数的总结

    array_map()函数的第一个参数可以是匿名函数,系统函数,也可以是自己自定义的函!在全局空间下,这些函数的调用都很简单 在这里就不多说了!我们主要讨论的是该函数调用类中的方法和静态方法的区别 摘 ...

  6. JQuery中Checkbox选择

    判断是否选中 $(this).is(":checked") 取消选中 $(this).prop("checked", false) 选中 $(this).pro ...

  7. 关于使用Encoding转码的问题,以及StreamWriter的小应用

    StreamWriter write = new StreamWriter("../../test2.txt"); write.WriteLine("中国123巴西red ...

  8. java 生成12位随机数,解决The literal 9999999999999 of type int is out of range 问题

    原本想这样产生一个随机数,但是你会看到,只要数字超过了9位数,就会出问题,提示“The literal 1000000000000 of type int is out of range” 解决方式是 ...

  9. 【洛谷5251】[LnOI2019] 第二代图灵机(线段树+ODT)

    点此看题面 大致题意: 有单点修改数字和区间着色两种修改操作,询问你某段区间内包含所有颜色且数字和最小的子区间的数字和,或某段区间内没有重复颜色且数字和最大的子区间的数字和.数据随机. \(ODT\) ...

  10. DP,得到最多苹果,(POJ2385)

    题目链接:http://poj.org/problem?id=2385 题意: 牛在两棵苹果树下收集苹果,牛只能在这两棵树之间走动w次,在t时刻,某棵树会掉下苹果. 解题报告: ///dp[t][w] ...