hdu_1452_Happy 2004 (乘法逆元
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 (乘法逆元的更多相关文章
- Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)
Problem Description Considera positive integer X,and let S be the sum of all positive integer diviso ...
- 数学--数论--Hdu 1452 Happy 2004(积性函数性质+和函数公式+快速模幂+乘法逆元)
Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...
- HDU 1452 (约数和+乘法逆元)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1452 题目大意:求2004^X所有约数和,结果mod 29. 解题思路: ①整数唯一分解定理: 一个 ...
- Bzoj2154 Crash的数字表格 乘法逆元+莫比乌斯反演(TLE)
题意:求sigma{lcm(i,j)},1<=i<=n,1<=j<=m 不妨令n<=m 首先把lcm(i,j)转成i*j/gcd(i,j) 正解不会...总之最后化出来的 ...
- 51nod1256(乘法逆元)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1256 题意:中文题诶~ 思路: M, N 互质, 求满足 K ...
- 【板子】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 ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
- Codeforces 543D Road Improvement(树形DP + 乘法逆元)
题目大概说给一棵树,树的边一开始都是损坏的,要修复一些边,修复完后要满足各个点到根的路径上最多只有一条坏的边,现在以各个点为根分别求出修复边的方案数,其结果模1000000007. 不难联想到这题和H ...
- HDU 1576 (乘法逆元)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思 ...
随机推荐
- [原创]hibernate更新后jdbc读取不到数据问题
最近在做工作流插件时使用的是自己基于hibernate连接封装的orm框架,按说跟hibernate共用的一个连接,应该在同一个事务中,但在使用时hibernate saveOrUpdate后(未提交 ...
- xubuntu 安装一款漂亮的图标
sudo add-apt-repository ppa:noobslab/icons sudo apt-get update sudo apt-get install square-beam-icon ...
- pat1043. Is It a Binary Search Tree (25)
1043. Is It a Binary Search Tree (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...
- Misc1
什么是编译进内核与制作成模块 编译进内核意味着内核对这一类的功能不会在依赖其他的东西, 说白了就是所谓静态编译, 内核在启动的时候就会拥有这一部分的功能, 但是这样内核的体积就会变大 编译成模块, 其 ...
- 自定义Qt组件-通讯模块(P1)
通讯模块Communicator 通讯模块是整个项目设计中位于最底层的模块,用于处理与串口或网络等设备的通讯,所有设备的通讯通过CommManager类完成,上层软件设计时需要根据comm模块(主要是 ...
- GitKraken使用教程-基础部分(1)
1. 首次打开程序 第一次打开GitKraken程序时, GitKraken会提示需要登陆,可以用github.com的账号登陆,或者用邮箱创建账号登陆(如图 1‑1). 图 1‑1登陆帐户界面 登陆 ...
- c# 小数点格式化
1.只要求保留N位不四舍5入 float f = 0.55555f; int i =(int)(f * 100); f = (float)(i*1.0)/100 ...
- grep和管道
1.管道命令: 可以连接多个Linux命令 命令1 | 命令2 | 命令3 2.grep 正则表达式 从文件或者字符串中搜索字符串 格式:grep ‘’ file 返回符合规则的行 需求:查询 ...
- the wait queue
using System; using System.Collections.Concurrent; using System.Threading; namespace Base { public c ...
- mysql-查询的案例
查询每个专业的男生人数和女生人数分别是多少 #方式一: select count(*) 个数,sex,majorid from student group by sex,majorid; #方式二: ...