HDU - 1452 - Happy 2004 - (因子和,极性函数,同余逆元)

2023-11-06 23:49

本文主要是介绍HDU - 1452 - Happy 2004 - (因子和,极性函数,同余逆元),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

转自:http://www.cnblogs.com/372465774y/archive/2012/10/22/2733977.html

Happy 2004

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 673    Accepted Submission(s): 481
Problem 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
 
Source
ACM暑期集训队练习赛(六)
 
Recommend
lcy
求约数的和
首先 约束和函数是积性函数(就是如果m,n互质,则 f(mn)=f(m)f(n))还有约数个数函数也是积性函数 这2个比较好证明 直接带入就可以
S(x)代表x的约数和
S(20)=S(4)*S(5)
如果p是素数 S(p^n)=1+p+p^2+...p^n=(p^(n+1)-1)/(p-1);
所以本题 S(2004^x)=(2^(2*x+1)-1)(3^(x+1)-1)/2*(167^(x+1)-1)/166而又同余性质  : 若 a=b(mod m) 则 a^k=b^k (mod m):
所以 167可以用 22代替,(对29 同余)
对于  a^k*b^h*...%m的题目 直接二进制快速运算
这里还有个 a^k/d % m
这就相当于 a^k*d-1%m
d-1 是 d的模m逆  就是  dd-1=1 mod m  ...1
这样的话               a^k/b=x mod m ...2由1,2根据同余性质 a^k*d-1=x mod m
所以本题就Ok了

#include <iostream>
#include <map>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int Mod(int a,int b)
{int t;for(t=1;b>0;b>>=1,a=(a*a)%29)if(b&1) t=(t*a)%29;return t;
}
int main()
{int n;int a,b,c;while(scanf("%d",&n),n){a=(Mod(2,2*n+1)-1);b=(Mod(3,n+1)-1)*15; 15是2的mod 29逆c=(Mod(22,n+1)-1)*18;18是 21的mod 29逆printf("%d\n",a*b*c%29);}return 0;
}


另外参考:

乘法逆元,

http://blog.csdn.net/yeguxin/article/details/46669831

http://blog.sina.com.cn/s/blog_79b832820100xx20.html
http://blog.csdn.net/luyuncheng/article/details/8017016

这篇关于HDU - 1452 - Happy 2004 - (因子和,极性函数,同余逆元)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/359818

相关文章

hdu4828(卡特兰数+逆元)

这题的前几个数据分别为1,2,5,14,32......................然后确定这是个卡特兰数列 下面来介绍下卡特兰数,它的递推式为f[i+1] = f[i]*(4*n - 6)/n,其中f[2] = f[3] =1;f[4] = 2;f[5] = 14;f[6] = 32.................................. 但是这题的n太大了,所以要用到逆元,

hdu4869(逆元+求组合数)

//输入n,m,n表示翻牌的次数,m表示牌的数目,求经过n次操作后共有几种状态#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#includ

hdu1171(母函数或多重背包)

题意:把物品分成两份,使得价值最接近 可以用背包,或者是母函数来解,母函数(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v)(1 + x^v+x^2v+.....+x^num*v) 其中指数为价值,每一项的数目为(该物品数+1)个 代码如下: #include<iostream>#include<algorithm>

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin