hdu 1852 Beijing 2008(快速幂取模)
2016-06-08 18:19
197 查看
Beijing 2008
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total Submission(s): 744 Accepted Submission(s): 294
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.
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 10000
0 0
Sample Output
5776
题意:给你n和k,2008的n次方对k取余为m,求2008的m次方对k取余
思路:不要用库函数的pow很慢,用快速幂,只要log(n)的时间。
另外,这道题不能用乘法逆元去处理k,因为乘法逆元要求必须互素。
所以我们可以运用这个公式
x/d%k==x%(d*k)/d这样就可以直接得到答案了。注意取模就要全部都对(d*k)取了~
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL long long
LL pow_mod(LL a,LL n,int k)
{
LL ans=1;
while(n)
{
if(n&1) ans=ans*a%k;
a=a*a%k;
n>>=1;
}
return ans;
}
int main()
{
int n,k;
while(scanf("%d %d",&n,&k)&&(n+k))
{
LL ans=1;
LL temp=pow_mod(2,3*n+1,250*k)-1;
LL temp1=pow_mod(251,n+1,250*k)-1;
ans=temp*temp1%(250*k)/250;
ans=pow_mod(2008,ans,k);
printf("%lld\n",ans);
}
return 0;
}
相关文章推荐
- 【JSHint解读二】 jshint 配置解析
- Java内存模型介绍
- 使用C++实现SDK之WebBrowser容器
- 破解教程笔记
- 第15周阅读程序(6)
- 关于Spring集成Quartz的concurrent属性
- 手把手教你搭建Oracle 11G dataguard
- 阅读程序,体会用法(4)
- 7、避免使用终结方法
- Storm实时计算-新手学习(案例模仿)
- android学习3#--简单Activity创建
- Django 基础(一),项目创建、URL路由、数据库操作、模版
- python re正则表达式使用实例
- CF1C Ancient Berland Circus
- 第七周项目一友元函数、成员函数和一般函数的区别
- CastApp手机验证码倒计时
- thinkphp模板中使用方法
- 常见的几种最优化方法
- ActiveX控件实现安全的初始化和脚本
- 腾讯互动直播1.7横竖屏切换以及画面颠倒问题解决