整数快速幂hdu(1852)
来源:互联网 发布:海文考研网络课程 编辑:程序博客网 时间:2024/06/05 06:01
hdu1852
Beijing 2008
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 502 Accepted Submission(s): 172
Problem Description
As we all know, the next Olympic Games will be held in Beijing in 2008. So the year 2008 seems a little special somehow. You are looking forward to it, too, aren't you? Unfortunately there still are months to go. Take it easy. Luckily you meet me. I have a problem for you to solve. Enjoy your time.
Now given a positive integer N, get the sum S of all positive integer divisors of 2008N. Oh no, the result may be much larger than you can think. But it is OK to determine the rest of the division of S by K. The result is kept as M.
Pay attention! M is not the answer we want. If you can get 2008M, that will be wonderful. If it is larger than K, leave it modulo K to the output. See the example for N = 1,K = 10000: The positive integer divisors of 20081 are 1、2、4、8、251、502、1004、2008,S = 3780, M = 3780, 2008M % K = 5776.
Now given a positive integer N, get the sum S of all positive integer divisors of 2008N. Oh no, the result may be much larger than you can think. But it is OK to determine the rest of the division of S by K. The result is kept as M.
Pay attention! M is not the answer we want. If you can get 2008M, that will be wonderful. If it is larger than K, leave it modulo K to the output. See the example for N = 1,K = 10000: The positive integer divisors of 20081 are 1、2、4、8、251、502、1004、2008,S = 3780, M = 3780, 2008M % K = 5776.
Input
The input consists of several test cases. Each test case contains a line with two integers N and K (1 ≤ N ≤ 10000000, 500 ≤ K ≤ 10000). N = K = 0 ends the input file and should not be processed.
Output
For each test case, in a separate line, please output the result.
Sample Input
1 100000 0
// 这题主要求S
// 结论: S = (251^(n+1)-1) * (2^(3n+1)-1) / 250
// 是两个等比数列和相乘
//
// 推理:
// 2008 = 2^3 * 251
// 所以 2008^N 有 3N 个 2 和 N 个251
// 所有仅由2组成的因子有
// 2^0 2^1 2^2 ... 2^(3N)
// 设集合 C = {2^0, 2^1, 2^2 ...,2^(3N)};
// SUM(C) = 2^(3n+1)-1
// 跟251组合产生的因子有
// 251^0 * C
// 251^1 * C
// ...
// 251^N * C
// 所有因子和为:
// S = (251^(n+1)-1))/250 * (2^(3n+1)-1)
// 计算S%K:
// S 很大, 不能保存在普通的数据类型中, 需要直接计算S%K
// 因为S有个分母250, 设 S = X/250
// 则S%K = (X/250)%K = (X%(250*K))/250
// 变成先求余数再除法的形式
#include"stdio.h"#include"string.h"__int64 pow(__int64 a,__int64 k,__int64 m){ __int64 b=1; while(k>=1) { if(k&1) b=b*a%m; a=a*a%m; k=k/2; } return b;}int main(){ __int64 n,k,ans,m,p1,p2,s; while(scanf("%I64d%I64d",&n,&k),k||n) { p1=pow(2,3*n+1,k*250)-1; p2=(pow(251,n+1,k*250)-1); s=(p1*p2)%(250*k); m=s/250; //printf("%I64d %I64d %I64d\n",p1,p2,m); ans=pow(2008,m,k); printf("%I64d\n",ans); }}
0 0
- 整数快速幂hdu(1852)
- 快速幂(整数)
- hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)
- HDU 4704 Sum (费马小定理+快速幂+整数和分解+欧拉降幂)
- 快速幂(整数+矩阵):hdu 2817+poj3233+poj1995+poj3070
- HDU 4704 SUM 整数快速幂+费马小定理
- 快速幂模板(整数+矩阵)
- 快速幂模板(整数+矩阵)
- 整数快速幂
- poj1995- 整数快速幂
- B - 整数快速幂
- 整数快速幂
- 整数快速幂【模板】
- 整数快速幂
- 整数快速幂
- 整数快速幂
- 整数快速幂-归纳法
- HDU 3240 Counting Binary Trees(组合数学-卡特兰数,数论-整数快速幂,数论-求逆元)
- Uni2D 入门+Uni2D 入门 -- Atlas
- Shiro 的CryptFormat源码
- QoS令牌桶工作原理
- Uni2D 入门 -- Animation Clip 和 Animation API
- [连载]C#程序设计(11)--- 接口
- 整数快速幂hdu(1852)
- Uni2D 入门 -- Skeletal Animation + Asset Table
- ACTF2014exploit100 餐前甜点
- [字符串]CH Round#30 T1 语言识别
- C++ primer读书笔记7-复制控制
- 【JAVA学习】hashcode()和equals()方法剖析
- Java设计模式之从[Dota的武器创建]分析生成器(Builder)模式
- USACO-cha1-sec1.1 Greedy Gift Givers
- 内网IP和公网IP的区别