G - Happy 2004

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

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.

 

Input

The 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.

 

Output

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

Sample Input

1
10000
0
 

Sample Output

6
10
题意:
求2004的x次幂的因子和并对29取余。
题解:

6的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12;

20的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42;

2的因子是1,2; 2的因子和是 s(2)=1+2=3;

3的因子是1,3; 3的因子和是 s(3)=1+3=4;

4的因子和是 s(4)=1+2+4=7;

5的因子和是 s(5)=1+5=6;

s(6)=s(2)*s(3)=3*4=12;

s(20)=s(4)*s(5)=7*6=42;

这是巧合吗?

再看 s(50)= 1+2+5+10+25+50=93=3*31=s(2)*s(25),s(25)=1+5+25=31.

这在数论中叫积性函数,当gcd(a,b)=1时 s(a*b)=s(a)*s(b);

具体的证明过程请看我之前的博客:传送门

如果p是素数

s(p^n)=1+p+p^2+...+p^n= (p^(n+1)-1) /(p-1) (1)

计算 因子和 s(2004^X) mod 29 ,

2004=2^2 *3 *167

s(2004^X) ) = (s(2^2X))) * (s(3^X))) * (s(167^X)))

167)=22;

s(2004^X) ) = (s(2^2X))) * (s(3^X))) * (s(22^X)))

a=s(2^2X)=(2^(2X+1)-1) //根据 (1)

b=s(3^X)= (3^(X+1)-1)/2 //根据 (1)

c=s(22^X)= (22^(X+1)-1)/21 //根据 (1)

%运算法则 1. (a*b) %p= ( a%p) *(b%p)

%运算法则 2. (a/b) %p= ( a *b^(-1)%p)

b^(-1)是 b的逆元素 (%p)可以用扩展欧几里德逆元模板求

2的逆元素是15 ()) ,因为2*15=30 % 29=1 % 29

21的逆元素是18 ()) ,因为21*18=378% 29 =1 % 29

因此

a=(powi(2,2*x+1,29)-1)% 29;

b=(powi(3,x+1,29)-1)*15 % 29;

c=(powi(22,x+1,29)-1)*18 % 29;

ans=(a*b)% 29*c % 29;

#include <iostream>
using namespace std;
typedef long long ll;
const int mod=;
ll pow(ll a,ll b)
{
ll ans=;
while(b)
{
if(b&)
ans=ans*a%mod;
b>>=;
a=a*a%mod;
}
return ans;
}
int main()
{
int x;
while(cin>>x&&x)
{
ll a=(pow(,*x+)-+mod)%mod;
ll b=(pow(,x+)-+mod)*%mod;
ll c=(pow(,x+)-+mod)*%mod;
ll ans=a*b*c%mod;
cout<<ans<<endl;
}
return ;
}

HDU 1452 Happy 2004 (逆元+快速幂+积性函数)的更多相关文章

  1. hdu1452 Happy 2004(规律+因子和+积性函数)

    Happy 2004 题意:s为2004^x的因子和,求s%29.     (题于文末) 知识点: 素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en 因子 ...

  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 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)

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

  4. HDU1452Happy 2004(高次幂取模+积性函数+逆元)

    题目意思:2004^x的所有正因数的和(S)对29求余:输出结果: 原题链接 题目解析:解析参照来源:点击打开链接 因子和 6的因子是1,2,3,6; 6的因子和是s(6)=1+2+3+6=12; 2 ...

  5. HDU 1452 Happy 2004(因子和的积性函数)

    题目链接 题意 : 给你一个X,让你求出2004的X次方的所有因子之和,然后对29取余. 思路 : 原来这就是积性函数,点这里这里这里,这里讲得很详细. 在非数论的领域,积性函数指所有对于任何a,b都 ...

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

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

  7. Happy 2004(快速幂+乘法逆元)

    Happy 2004 问题描述 : Consider a positive integer X,and let S be the sum of all positive integer divisor ...

  8. HDU 1452 Happy 2004(因数和+费马小定理+积性函数)

    Happy 2004 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. hdu 1452 Happy 2004

    因子和: 的因子是1,2,3,6; 6的因子和是 s(6)=1+2+3+6=12; 的因子是1,2,4,5,10,20; 20的因子和是 s(20)=1+2+4+5+10+20=42; 的因子是1,2 ...

随机推荐

  1. opencv笔记4:模板运算和常见滤波操作

    time:2015年10月04日 星期日 00时00分27秒 # opencv笔记4:模板运算和常见滤波操作 这一篇主要是学习模板运算,了解各种模板运算的运算过程和分类,理论方面主要参考<图像工 ...

  2. CruiseControl.NET开篇

    在这里说明一下,我终于踏上了CruiseControl.NET这条不归路了,为什么我会觉得是一条不归路,原因很简单,就是这东西在现在这个阶段已经很久没有在园子里有活跃度了,基本上到了已经可以到了让大家 ...

  3. QA要懂的Linux命令

    <一>软件安装相关QA经常需要安装测试软件(jmeter.Mock.python环境搭建.java环境搭建),或者配置测试环境(nginx.ci等),需要了解linux下如何安装软件.在工 ...

  4. DataFrame转矩阵Np-Array

    DataFrame.as_matrix(columns=None)¶ Convert the frame to its Numpy-array representation.

  5. mySQL 增量备份方案(转)

    1.在 /etc/my.cnf 下面设置开启bin-log 编辑 vim /etc/my.cnf [mysqld] binlog_format       = MIXED                ...

  6. Swift翻译之-关于Swift

    IMPORTANT 重要的 This is a preliminary document for an API or technology in development. Apple is suppl ...

  7. 如何有效的保护 JAVA 程序

    从头到尾保护 JAVA 目前关于 JAVA 程序的加密方式不外乎 JAVA 模糊处理(Obfuscator)和运用 ClassLoader 方法进行加密处理这两种方式(其他的方式亦有,但大多是这两种的 ...

  8. oracle中Blob和Clob类型的区别

    BLOB和CLOB都是大字段类型,BLOB是按二进制来存储的,而CLOB是可以直接存储文字的.其实两个是可以互换的的,或者可以直接用LOB字段代替这两个.但是为了更好的管理ORACLE数据库,通常像图 ...

  9. 编译过程中,termcap.h 文件找不到路径 licli.a终于生成

    编译过程中,termcap.h      文件找不到路径   查看是linux  源码下找不到termcap.h文件   安装了所有关于*cap*的源码包也不起作用     今天终于解决了这个问题,搜 ...

  10. 各种工具使用手册:http://www.itshouce.com.cn/linux/linux-tcpdump.html 关于tcpdump!!!!

    各种工具使用手册:http://www.itshouce.com.cn/linux/linux-tcpdump.html 关于tcpdump!!!! 实用tcpdump命令 //查看本机与mysql的 ...