hdu 1452 Happy 2004 (快速幂+取模乘法逆元)

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

题意:找2014^x%29的值。

快速幂和取模乘法逆元。乘法逆元:x=(1/b)%m. 求x的值。x*b=k*m+1,即k去最小正整数时,x也为整数。即b能被k*m+1整除。

 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 int f1(int x,int y)
 5 {
 6     int ans=1;
 7     while (y)
 8     {
 9         if (y&1) ans=ans*x%29;
10         x=x*x%29;
11         y>>=1;
12     }
13     return ans;
14 }
15 int f2(int x)
16 {
17     int i=1;
18     while (i)
19     {
20         if ((29*i+1)%x==0) break;
21         i++;
22     }
23     return (29*i+1)/x;
24 }
25 int main()
26 {
27     int x,a,b,c;
28     while (~scanf("%d",&x))
29     {
30         if (!x) break;
31         a=(f1(2,2*x+1)-1)%29;
32         c=(((f1(3,x+1)-1)%29)*f2(2))%29;
33         b=(((f1(22,x+1)-1)%29)*f2(21))%29;
34         printf("%d\n",(a*b*c)%29);
35     }
36     return 0;
37 }
时间: 2024-12-24 10:41:10

hdu 1452 Happy 2004 (快速幂+取模乘法逆元)的相关文章

hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)

http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序,问funny函数调用了多少次. 我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2].对应的值表示也可为a^1*b^0%p,a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p.即a,b的指数从n=3以后与fib数列

HDU - 3003 - Pupu (快速幂取模!)

Pupu Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1133    Accepted Submission(s): 445 Problem Description There is an island called PiLiPaLa.In the island there is a wild animal living in it

HDU 4945 2048(dp+快速幂取模)

题目大意:给你一个序列让你求出有多少种组合可以得到2048.结果要对998244353取余. 解题思路:求出不能满足条件的方案数,然后用总的减去不满足的然后乘上其他无关的组合方式,比如3,5这些数字是在构成2048的过程中无用的,所以乘上这些组合出来的情况. dp[i][j]表示取到第i个2^i的数,其最大的和在j*2^i至(j+1)*2^i-1的方案数. 所以有dp[i][j] += ((dp[i-1][k]+dp[i-1][k+1])*use[i][j-k/2])%mod.表示在i位置时最大

HDU 5363 Key Set(快速幂取模)

Key Set Problem Description soda has a set $S$ with $n$ integers $\{1, 2, \dots, n\}$. A set is called key set if the sum of integers in the set is an even number. He wants to know how many nonempty subsets of $S$ are key set. Input There are multipl

HDU 5363 Key Set【快速幂取模】

Key Set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1886    Accepted Submission(s): 990 Problem Description soda has a set S with n integers {1,2,-,n}. A set is called key set if the sum

HDU 4965 Fast Matrix Calculation (矩阵快速幂取模----矩阵相乘满足结合律)

http://acm.hdu.edu.cn/showproblem.php?pid=4965 利用相乘的可结合性先算B*A,得到6*6的矩阵,利用矩阵快速幂取模即可水过. 1 #include<iostream> 2 #include<stdio.h> 3 #include<iostream> 4 #include<stdio.h> 5 #define N 1010 6 #define M 1010 7 #define K 6 8 using namespa

HDU 4365 正方形格子涂色中心对称轴对称的涂法有多少种-思维-(矩阵坐标关系&amp;快速幂取模)

题意:n*n的格子,涂色,有k种颜料,必须满足旋转任意个90度和翻转之后图片的样子不变,现在已经有m个格子涂过色了,问还有多少种涂法满足上述条件. 分析: 满足上述对称条件,那么涂色的种类问题我们可以放在正方形的一个角来做,因为一个角确定了其他角的颜色也就确定了. 以左上角的下半角为例.共有1+2+....+(n+1)/2个格子,然后记录涂过色的格子对应到这个三角形里的格子数目,用tot来记录,即每输入一个涂过色的格子的坐标我们就在这个三角形里找与之对应的坐标,用vis[][]数组标记是否已经计

HDU - 1097 - A hard puzzle (快速幂取模)

A hard puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 32633    Accepted Submission(s): 11672 Problem Description lcy gives a hard puzzle to feng5166,lwg,JGShining and Ignatius: gave a a

HDU 5363 元素为1~n的集合有多少个子集的元素和为偶数-思维-(快速幂取模)

题意:一个集合有元素1~n,求他的子集满足这样的条件:子集的所有元素的和是偶数,问有多少个这样的子集 分析: 一个排列组合问题.元素和为偶数,那么奇数肯定要调偶数个,偶数就无所谓了,所以偶数有2^(n/2)种选法,再乘以奇数有(C((n+1)/2,0)+C((n+1)/2,2).....)种选法,再减一,除去空集,注意,上面取奇数的时候用的是(n+1)/2(这里是向下取整的除法),是综合n为偶数和n为奇数两种情况. 组合数性质:C(n,1)+C(n,3)+....=C(n,2)+C(n,4)+.