Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)
Problem Description
Considera positive integer X,and let S be the sum of all positive integer divisors of2004^X. Your job is to determine S modulo 29 (the rest of the division of S by29).
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 29is equal to 6.
Input
Theinput consists of several test cases. Each test case contains a line with theinteger X (1 <= X <= 10000000).
A test case of X = 0 indicates the end of input, and should not be processed.
Output
Foreach test case, in a separate line, please output the result of S modulo 29.
Sample Input
1
10000
0
Sample Output
6
10
/***************************
解题思路:
参考大神的:http://www.cnblogs.com/372465774y/archive/2012/10/22/2733977.html
体重主要用到除数和函数。
除数和函数 :F(n) 求n的约数的和 ( 约数大于等于1 小于n )
除数和函数是一个积性函数,满足性质 :当m , n 互质时, f(m*n) = f(m) * f(n)
如果 p 是一个素数,则 f(p^n) = 1 + p + p^2 +p^3 +p^4 + .... + p^(n-1) + p^n = (p^(n+1) -1)/p-1 (等比数列求和)
则题目中 f(2004^n) = f(2^(2*n)) * f(3^n) * f(167^n)
= (2^(2*n+1) -1) * (3^(n+1) -1)/2 *(167^(n+1) -1)/166
用到乘法逆元:(同余性质)
a^k/d = a^k*(d-1) d-1 即为d的逆元。 3的逆元为15
167 的逆元为18
然后还要用到 快速幂模:转换为位运算,这题要用这个,一般的会超时,具体看代码吧。
*************************/
Code:
#include<stdio.h>
using namespace std;
int Mod(int a,int b)// 快速幂模函数
{
int t = 1;
while(b)
{
if(b&1)
t = t*a%29;
b>>=1;
a = a*a%29;
}
return t;
}
int main()
{ int n,a,b,c;
while(scanf("%d",&n)&&n)
{
a=(Mod(2,2*n+1)-1);
b=(Mod(3,n+1)-1)*15;
c=(Mod(22,n+1)-1)*18;
printf("%d\n",a*b*c%29);
}
return 0;
}
Hdu 1452 Happy 2004(除数和函数,快速幂乘(模),乘法逆元)的更多相关文章
- HDU 1061.Rightmost Digit-规律题 or 快速幂取模
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【数论】【组合数】【快速幂】【乘法逆元】洛谷 P2265 路边的水沟
从左上角到右下角,共经过n+m个节点,从其中选择n各节点向右(或者m各节点向下),所以答案就是C(n+m,n)或者C(n+m,m),组合数暴力算即可,但是要取模,所以用了乘法逆元. #include& ...
- hdu 3307 Description has only two Sentences (欧拉函数+快速幂)
Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- hdu 1097 A hard puzzle 快速幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...
- XMU 1615 刘备闯三国之三顾茅庐(三) 【欧拉函数+快速幂+欧拉定理】
1615: 刘备闯三国之三顾茅庐(三) Time Limit: 1000 MS Memory Limit: 128 MBSubmit: 45 Solved: 8[Submit][Status][W ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- hdu 2065 "红色病毒"问题(快速幂求模)
n=1 --> ans = 2 = 1*2 = 2^0(2^0+1) n=2 --> ans = 6 = 2*3 = 2^1(2^1+1) n=3 --> ans = 20 ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- 《Java语言实现快速幂取模》
快速幂取模算法的引入是从大数的小数取模的朴素算法的局限性所提出的,在朴素的方法中我们计算一个数比如5^1003%31是非常消耗我们的计算资源的,在整个计算过程中最麻烦的就是我们的5^1003这个过程 ...
随机推荐
- HW3.14
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Installing your app on your Windows RT device
Alright… so my app is almost finished and I want to install it for real on my Surface tablet. How do ...
- fzu2109--Mountain Number(数位dp)
Problem Description One integer number x is called "Mountain Number" if: (1) x>0 and x ...
- WPF布局系统[转]
转自:http://www.cnblogs.com/niyw/archive/2010/10/31/1863908.html前言 前段时间忙了一阵子Google Earth,这周又忙了一阵子架构师论文 ...
- PHP数组操作汇总
php 操作数组 (合并,拆分,追加,查找,删除等) - Just Code - ITeye技术网站 PHP操作数组的一些函数介绍 -- 简明现代魔法 PHP数组元素操作实例 -- 简明现代魔法 儿童 ...
- SPOJ4206Fast Maximum Matching(hopcroft-karp)
题目请戳这里 题目大意:裸的二分匹配. 题目分析:数据比较强,用来测模版的.这题用hungry跑着会比较吃力,所以用hopcroft-karp算法.这个算法较hungry高效是因为每次bfs找到一个增 ...
- Windows Azure功能更新: SDK 2.1发布,Traffic Manager集成
最近,Windows Azure又进行了更新 Windows Azure SDK 2.0发布没多久,2.1版(for .NET)就在今天发布了.2.1版本在管理功能上进行了重大改进,包括Visual ...
- python 语句,条件,循环
#coding:utf-8__author__ = 'Administrator'#想要在同一行打印,print后面加','print 12,print 34'''从模块导入函数import some ...
- C#下的 Emgu CV
Emgu CV下载地址 http://sourceforge.net/projects/emgucv/files/ 找最新的下就行了,傻瓜式安装,选择目录后自动完成安装,然后提示安装VS2008和VS ...
- [C#]工具类—FTP上传下载
public class FtpHelper { /// <summary> /// ftp方式上传 /// </summary> public static int Uplo ...