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. LeetCode之Binary Tree Level Order Traversal 层序遍历二叉树

    Binary Tree Level Order Traversal 题目描述: Given a binary tree, return the level order traversal of its ...

  2. 重温WCF之构建一个简单的WCF(一)(1)通过控制台和IIS寄宿服务

    一.理解什么是WCFWCF就是.NET平台下各种分布式技术的集成,并提供了一套统一的编程接口 二.WCF的定义WCF(Windows Communication Foundation)是微软为构建面向 ...

  3. CentOS版本选择说明

    官方下载站http://www.centos.org/download/ 所有版本下载地址http://vault.centos.org/ 首先对一些镜像文件做个简单的介绍: LiveCD一般用来修复 ...

  4. android 入门-防微信拍摄视频 按钮事件处理

    package com.cc.view; import com.cc.R; import com.cc.R.layout; import com.cc.R.menu; import android.o ...

  5. pythonchallenge之C++学习篇-00

    前言 最近学习下C++,之前是python的用户,python解释器有诸多实现,其中最出名的要数C实现了,而且很多python的扩展模块可能要用C或者C++来写的,所以很有必要学习下C++了 为了避免 ...

  6. 通过PID获取进程路径的几种方法

    通过PID获取进程路径的几种方法 想获得进程可执行文件的路径最常用的方法是通过GetModuleFileNameEx函数获得可执行文件的模块路径这个函数从Windows NT 4.0开始到现在的Vis ...

  7. MySQL数据库在WINDOWS系统CMD下的编码问题

    MySQL数据库在WINDOWS系统CMD下的编码问题 1. 查看MySQL数据库编码 * SHOW VARIABLES LIKE 'char%'; 2. 编码解释 * character_set_c ...

  8. Android开发之ViewPager+ActionBar+Fragment实现响应式可滑动Tab

     今天我们要实现的这个效果呢,在Android的应用中十分地常见,我们可以看到下面两张图,无论是系统内置的联系人应用,还是AnyView的阅读器应用,我们总能找到这样的影子,当我们滑动屏幕时,Tab可 ...

  9. Loadrunner中百分比模式和Vuser模式

    从百分比模式切换到Vuser模式后,多个脚本时候,每个脚本的比例仍然维持不变: 切换到Vuser模式后: 如果在场景执行过程中需要动态添加Vuser,只能在Vuser模式下执行场景 如果需要执行“组” ...

  10. Codeforces Round #355 (Div. 2)-C

    C. Vanya and Label 题目链接:http://codeforces.com/contest/677/problem/C While walking down the street Va ...