Happy 2004

题意:s为2004^x的因子和,求s%29.     (题于文末)

知识点:

素因子分解:n = p1 ^ e1 * p2 ^ e2 *..........*pn ^ en

因子和:    Sum=(p1^0+p1^1….p1^e1)*(p2^0+p2^1…p2^e2)……(pn^0+…pn^en)

=;

积性函数:s(xy)=s(x)*s(y)    (比如:幂函数,因子和,欧拉函数,莫比乌斯函数)

对于正整数n的一个算术函数 f(n),若f(1)=1,且当a,b互质时f(ab)=f(a)f(b),在数论上就称它为积性函数。

若对于某积性函数 f(n),就算a, b不互质,也有f(ab)=f(a)f(b),则称它为完全积性的。

%运算:  % k = =                    为m模k的逆元

题解:

一般模值(mod)较小时会有规律,可以找下循环节。

发现答案的循环结为28.

#include<iostream>
using namespace std;
int a[]={6,16,8,10,25,7,14,3,23,17,13,17,0,27,7,14,15,17,26,26,20,17,9,22,22,23,0,1}; int main()
{
int x;
while(cin>>x&&x)
{
cout<<a[(x-1)%28]<<endl;
}
/*找规律过程
    for(x=1;x<=100;x++)
{
int a2=1,a3=1,a167=1,ans2=0,ans3=0,ans167=0;
for(int i=0;i<=x*2;i++)
{
ans2+=a2;
a2*=2;
ans2%=29;
a2%=29;
}
for(int i=0;i<=x;i++)
{
ans3+=a3;
a3*=3;
ans3%=29;
a3%=29;
}
for(int i=0;i<=x;i++)
{
ans167+=a167;
a167*=167;
ans167%=29;
a167%=29;
}
cout<<ans2*ans3*ans167%29<<endl;
}*/
return 0;
}

再给出一般解:

因子和为积性函数,so  sum(2004^X)= sum(2^2X) * sum(3^X)* sum(167^X)

sum(2004^X)%29=sum(2^2X) %29  *  sum(3^X)%29  *  sum(167^X)%29

=sum((2%29)^2X) %29  *  sum((3%29)^X)%29  *  sum((167%29)^X)%29

=sum(2^2X)   *  sum(3^X)  *  sum(22^X)%29

=   *      *   %29

2的逆元是15  ,21的逆元是18

=((2^(2X+1)-1)* (3^(X+1)-1)*15 *(22^(X+1)-1)*18)%29

快速幂取模,实现2^2x,3^x,22^x   O(logn)的运算

#include <iostream>
#include <cstdio>
#include <cmath> using namespace std; int quick_mod( int a, int n )
{
int b = 1;
while( n > 1 )
if( n % 2 == 0 )
{
a = ( a * a ) % 29;
n /= 2;
}
else
{
b = b * a % 29;
n--;
}
return a * b % 29;
}
int main()
{
int X;
int a, b, c;
while( scanf("%d",&X), X )
{
a = quick_mod( 2, 2 * X + 1 );
b = quick_mod( 3, ( X + 1 ) );
c = quick_mod( 22, ( X + 1 ) );
printf("%d\n",( a - 1 ) * (( b - 1 ) * 15) * ( c - 1 ) * 18 % 29) ;
}
return 0;
}

全题:

Happy 2004

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

Submit Status

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

hdu1452 Happy 2004(规律+因子和+积性函数)的更多相关文章

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

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

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

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

  3. HDU1452:Happy 2004(积性函数)(因子和)

    题意 给出\(x\),求\(2004^x\)的所有因子和 分析 \(2004=2*2*3*167\) 则\(2004^x\)=\(2^{2x}*3^x*167^x\) s[\(2004^x\)]=s[ ...

  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(积性函数性质+和函数公式+快速模幂+乘法逆元)

    Consider a positive integer X,and let S be the sum of all positive integer divisors of 2004^X. Your ...

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

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

  7. POJ 2480 Longge's problem (积性函数,欧拉函数)

    题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...

  8. Divisor counting [线性筛积性函数]

    Divisor counting 题目大意:定义f(n)表示整数n的约数个数.给出正整数n,求f(1)+f(2)+...+f(n)的值. 注释:1<=n<=1000,000 想法:我们再次 ...

  9. [模板] 积性函数 && 线性筛

    积性函数 数论函数指的是定义在正整数集上的实或复函数. 积性函数指的是当 \((a,b)=1\) 时, 满足 \(f(a*b)=f(a)*f(b)\) 的数论函数. 完全积性函数指的是在任何情况下, ...

随机推荐

  1. JavaScript - reduce方法,reduceRight方法 (Array)

    JavaScript - reduce方法 (Array) 解释:reduce() 方法接收一个函数作为累加器(accumulator),数组 中的每个值(从左到右)开始合并,最终为一个值. 语法:a ...

  2. ASP.NET 5 Target framework dnx451 and dnxcore50

    中文不知如何定义标题,所以干脆就直接贴出关键字,在 ASP.NET 5 项目的 project.json 配置文件中,会有这样的定义: "frameworks": { " ...

  3. C++异常处理:try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

  4. 几道web前端练习题目

    在 HTML 语言中,以下哪个属性不是通用属性?A]<class>B]<title>C]<href>D]<style> 在线练习:http://hove ...

  5. SPI 2分频MOSI实现

    module spi_25M(input clk,input rst_n,output reg sdin,output reg sclk,output reg cs);reg [7:0]cnt;reg ...

  6. JAVA基础培训(isoft)

    我们

  7. 使用MyBatis Generator自动创建代码(dao,mapping,poji)

    连接的数据库为SQL server2008,所以需要的文件为sqljdbc4.jar 使用的lib库有: 在lib库目录下新建一个src文件夹用来存放生成的文件,然后新建generatorConfig ...

  8. 关于docker

    摘要: 最近很多阿里内部的同学和客户私信来咨询如何学习 Docker 技术.为此,我们列了一个路线图供大家学习Docker和阿里云容器服务.这个列表包含了一些社区的优秀资料和我们的原创文章.我们会随着 ...

  9. 使用tomcat manager 管理和部署项目

    在部署tomcat项目的时候,除了把war文件直接拷贝到tomcat的webapp目录下,还有一种方法可以浏览器中管理和部署项目,那就是使用tomcat manager. 默认情况下,tomcat m ...

  10. Sublime Text通过插件编译Sass为CSS及中文编译异常解决

    虽然PostCSS才是未来,但是Sass成熟稳定,拥有一大波忠实的使用者,及开源项目,且最近Bootstrap 4 alpha也从Less转到Sass了.所以了解Sass还是非常有必要的. 基于快速开 ...