题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1452

题目大意:求2004^X所有约数和,结果mod 29。

解题思路

①整数唯一分解定理:

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

如2004=(22)*3*167。

那么2004x=(22x)*(3x)*(167x)。

②约数和公式

对于一个已经被分解的整数A=(P1^K1)*(P2^K2)*(P3^K3).....*(Pn^Kn),

有约数和S=(1+P12+P13+.....P1k1)*.....(1+Pn2+Pn3+.....Pnkn)。

(1+P12+P13+.....P1k1)是一个等比数列,化简为(P1k1+1 -1)/(P1-1).

对于2004^X, 只要求出a=pow(2,2*x+1)-1,b=pow(3,x+1)-1,c=pow(167,x+1)-1即可,使用快速幂计算,注意快速幂模板里要mod。

关键问题在于ans=(a*b/2*c/166) mod 29的计算问题,因为除法是不能同余计算的,所以要计算2*166关于29的乘法逆元,转化成乘法取模。

所以ans=(a*b*c*rev) mod 29。

#include "cstdio"
#define LL long long
#define mod 29
LL ex_gcd(LL a,LL b,LL &x,LL &y)
{
if(a==&&b==) return -;
if(b==) {x=;y=;return a;}
LL d=ex_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
LL mod_reverse(LL a,LL n)
{
LL x,y,d=ex_gcd(a,n,x,y);
if(d==) return (x%n+n)%n;
else return -;
}
LL pow(LL a,LL n)
{
LL base=a,ret=;
while(n)
{
if(n&) ret=(ret*base)%mod;
base=(base*base)%mod;
n>>=;
}
return ret%mod;
}
int main()
{
LL T,x;
while(scanf("%I64d",&x)!=EOF&&x)
{
LL a=pow(,*x+)-,b=pow(,x+)-,c=pow(,x+)-,rev=mod_reverse(*,mod);
printf("%I64d\n",(a*b*c*rev)%mod);
}
}
12170066 2014-11-13 11:02:46 Accepted 1452 0MS 228K 734 B C++ Physcal

HDU 1452 (约数和+乘法逆元)的更多相关文章

  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 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

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

  4. HDU 1576 (乘法逆元)

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

  5. hdu 2669 Romantic (乘法逆元)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU 1452 Happy 2004 (逆元+快速幂+积性函数)

    G - Happy 2004 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  7. 【题解】POJ1845 Sumdiv(乘法逆元+约数和)

    POJ1845:http://poj.org/problem?id=1845 思路: AB可以表示成多个质数的幂相乘的形式:AB=(a1n1)*(a2n2)* ...*(amnm) 根据算数基本定理可 ...

  8. HDU 5407(2015多校10)-CRB and Candies(组合数最小公倍数+乘法逆元)

    题目地址:pid=5407">HDU 5407 题意:CRB有n颗不同的糖果,如今他要吃掉k颗(0<=k<=n),问k取0~n的方案数的最小公倍数是多少. 思路:首先做这道 ...

  9. poj 1845 Sumdiv(约数和,乘法逆元)

    题目: 求AB的正约数之和. 输入: A,B(0<=A,B<=5*107) 输出: 一个整数,AB的正约数之和 mod 9901. 思路: 根据正整数唯一分解定理,若一个正整数表示为:A= ...

随机推荐

  1. web storm

    常用插件: code glance 代码预览功能,用过的都知道有多爽...

  2. JAVA数据库基本操作 (转)

    JAVA数据库基本操作指南   Java数据库操作基本流程:取得数据库连接 - 执行sql语句 - 处理执行结果 - 释放数据库连接. 一.取得数据库连接 1.用DriverManager取数据库连接 ...

  3. php 上传文件实例 注册账号

    注册界面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  4. 与你相遇好幸运,Sail.js创建.sailsrc文件

    在项目根目录下创建.sailsrc文件 {  "generators": {    "modules": {}  },  "hooks": ...

  5. JavaScript - UnderScore

    UnderScore 第一步 call(this) (function() {}.call(this)); 一些简单的初始化操作 (function() { var root = this; var ...

  6. Win10 UAP 标题栏

    //自定义标题栏 var view = Windows.UI.ViewManagement.ApplicationView.GetForCurrentView(); ApplicationViewTi ...

  7. long和int的区别

    转自:http://blog.sina.com.cn/s/blog_6f62c9510101svjz.html 突然间就想到了long和int到底什么区别(发现有很多问题都是突然间想到的),然后百度. ...

  8. [liferay6.2]input-date日期控件

    input-date日期控件 liferay6.2中默认提供了一个简单的日期控件input-date,调用代码片段如下: <% Calendar calendar = Calendar.getI ...

  9. C语言中运算符的口决

  10. MFC 丢失MSVCR120D.dll 丢失mfc120ud.dll