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的所有的因子和然后 mod 29。 一开始没有头绪,后面百度了下,N的因子和是积性函数,所以就开始用积性函数的方法解题。 积性函数 : 当gcd(a,b)=1时 s(a*b)=s(a)*s(b);
如果p是素数[或素数取X模后的数] s(p^n)=1+p+p^2+...+p^n= (p^(n+1)-1) /(p-1)
/*/ /*/ 2004=2*2*3*167 质因子 /*/ /*/ σ(2004^x)=σ(2^2x)*σ(3^x)*σ(167^x) mod 29 积性函数。 /*/ /*/ 167%29 = 22 /*/ /*/ σ(2004^x)=σ(2^2x)*σ(3^x)*σ(22^x) mod 29 /*/ /*/ σ(2^2x)= (2^(2x+1)-1)/(2-1)=(2^(2x+1)-1)/1 /*/ /*/ σ(3^x) = (3^(x+1)-1)/(3-1) =(3^(x+1)-1) /2 /*/ /*/ σ(22^x)=(22^(x+1)-1)/(22-1)=(22^(x+1)-1)/21 /*/ /*/ σ(2004^x)=(2^(2x+1)-1)*(3^(x+1)-1)/2*(22^(x+1)-1)/21 /*/ /*/ 1/2 对于29的逆元x=15 1=2*15 -29*1 /*/ /*/ 1/21对于29的逆元x=18 1=21*18-29*13 /*/ /*/ σ(2004^x)=((2^(2x+1)-1)*(3^(x+1)-1)*15*(22^(x+1)-1)*18)%29 /*/ /*/ 然后开始用快速幂求解。 /*/ /*/ 这个题目的重点在在于这个数学推导过程。 /*/ // AC代码:
#include"cmath"
#include"string"
#include"cstdio"
#include"cstring"
#include"iostream"
#include"algorithm"
using namespace std;
typedef long long LL; LL mod_pow(LL x,LL y,LL p) {
LL rt=1;
LL t=x;
while(y) {
if(y&1)rt=(rt*t)%p;
t=t*t%p;
y>>=1;
}
return rt;
} int main() {
LL x,ans;
while(~scanf("%I64d",&x)) {
if(x==0)break;
/*/ σ(2004^x)=((2^(2x+1)-1)*(3^(x+1)-1)*15*(22^(x+1)-1)*18)%29 /*/
ans=1;
ans*=(mod_pow(2,2*x+1,29)-1)%29;
ans*=(mod_pow(3,x+1,29)-1)*15%29;
ans*=(mod_pow(22,x+1,29)-1)*18%29;
printf("%I64d\n",ans%29);
}
return 0;
}

  

ACM: Happy 2004-数论专题-因子求和-快速幂的更多相关文章

  1. 从BZOJ2242看数论基础算法:快速幂,gcd,exgcd,BSGS

    LINK 其实就是三个板子 1.快速幂 快速幂,通过把指数转化成二进制位来优化幂运算,基础知识 2.gcd和exgcd gcd就是所谓的辗转相除法,在这里用取模的形式体现出来 \(gcd(a,b)\) ...

  2. [原]sdut2605 A^X mod P 山东省第四届ACM省赛(打表,快速幂模思想,哈希)

    本文出自:http://blog.csdn.net/svitter 题意: f(x) = K, x = 1 f(x) = (a*f(x-1) + b)%m , x > 1 求出( A^(f(1) ...

  3. Description has only two Sentences(欧拉定理 +快速幂+分解质因数)

    Description has only two Sentences Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...

  4. ACM&OI 基础数论算法专题

    ACM&OI 基础数学算法专题 一.数论基础 质数及其判法 (已完结) 质数的两种筛法 (已完结) 算数基本定理与质因数分解 (已完结) 约数与整除 (已完结) 整除分块 (已完结) 最大公约 ...

  5. ACM数论-快速幂

    ACM数论——快速幂 快速幂定义: 顾名思义,快速幂就是快速算底数的n次幂.其时间复杂度为 O(log₂N), 与朴素的O(N)相比效率有了极大的提高. 原理: 以下以求a的b次方来介绍: 把b转换成 ...

  6. ACM学习历程—HDU5667 Sequence(数论 && 矩阵乘法 && 快速幂)

    http://acm.hdu.edu.cn/showproblem.php?pid=5667 这题的关键是处理指数,因为最后结果是a^t这种的,主要是如何计算t. 发现t是一个递推式,t(n) = c ...

  7. ACM数论之旅2---快速幂,快速求a^b((ノ`Д´)ノ做人就要坚持不懈)

    a的b次方怎么求 pow(a, b)是数学头文件math.h里面有的函数 可是它返回值是double类型,数据有精度误差 那就自己写for循环咯 LL pow(LL a, LL b){//a的b次方 ...

  8. acm数论之旅(转载) -- 快速幂

    0和1都不是素数,也不是合数. a的b次方怎么求 pow(a, b)是数学头文件math.h里面有的函数 可是它返回值是double类型,数据有精度误差 那就自己写for循环咯 LL pow(LL a ...

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

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

随机推荐

  1. 清空mysql的历史记录

    # vi ~/.mysql_history show tables; show databases; 清空里面的内容,并不用退出当前shell,就可以清除历史命令!!

  2. Hibernate的ORM原理和实现

    >>Hibernate和ORM ORM的全称是Object Relational Mapping,即对象关系映射.它的实现思想就是将关系数据库中表的数据映射成为对象,以对象的形式展现,这样 ...

  3. 重温WCF之群聊天程序(十)

    完成的效果图: 服务器端代码: using System; using System.Collections.Generic; using System.Linq; using System.Serv ...

  4. 【数据结构】建立和平衡AVL二叉树

    一步一步写平衡二叉树(AVL树) 原文地址:http://www.cppblog.com/cxiaojia/archive/2012/08/20/187776.html 我添加了一些内容,以充实整个算 ...

  5. C# 将文件转化成byte[]数组

    /// <summary> /// 将文件转换成byte[] 数组 /// </summary> /// <param name="fileUrl"& ...

  6. 在source insight中集成astyle

    转自:http://www.cnblogs.com/xuxm2007/archive/2013/04/06/3002390.html 好吧,我有代码格式的强迫症,代码不整齐,我看的都头疼,之前一直喜欢 ...

  7. 如何给你的ASP.NET页面添加HelpPage

    如何给你的ASP.NET页面添加HelpPage 最近写了一些webAPI,所以需要搞一套API的帮助文档,google了一下,发现这是可以自动生成的,以下就是如何自动生成HelpPage的说明. 参 ...

  8. android MPAndroidChart饼图实现图例后加数字或文本(定制图例)

    转载请注明:http://blog.csdn.net/ly20116/article/details/50905789 MPAndroidChart是一个非常优秀的开源图表库,MPAndroidCha ...

  9. hdu1087 最大递增子段和

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1087 状态方程:sum[j]=max{sum[i]}+a[j]; 其中,0<=i<=j, ...

  10. poj1611 并查集 (路径不压缩)

    http://poj.org/problem?id=1611 题目大意: 有一个学校,有N个学生,编号为0-N-1,现在0号学生感染了非典,凡是和0在一个社团的人就会感染,并且这些人如果还参加了别的社 ...