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.


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

分析:

// 这题主要求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);
}
}

整数快速幂hdu(1852)的更多相关文章

  1. HDU 2817 A sequence of numbers 整数快速幂

    A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

  3. 题解报告:hdu 1576 A/B(exgcd、乘法逆元+整数快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 Problem Description 要求(A/B)%9973,但由于A很大,我们只给出n(n ...

  4. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

  5. 组合数(Lucas定理) + 快速幂 --- HDU 5226 Tom and matrix

    Tom and matrix Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=5226 Mean: 题意很简单,略. analy ...

  6. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  7. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  8. POJ1995(整数快速幂)

    http://poj.org/problem?id=1995 题意:求(A1^B1 + A2^B2 + .....Ah^Bh)%M 直接快速幂,以前对快速幂了解不深刻,今天重新学了一遍so easy ...

  9. 快速幂 HDU 1061 Rightmost Digit *

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. R语言进阶之4:数据整形(reshape)

    一.通过重新构建数据进行整形 数据整形最直接的思路就把数据全部向量化,然后按要求用向量构建其他类型的数据.这样是不是会产生大量的中间变量.占用大量内存?没错.R语言的任何函数(包括赋值)操作都会有同样 ...

  2. 10分钟学会写Jquery插件

    最近很多网友说jquery插件是什么啊?怎么写的啊?我不会写啊?   一大堆的问题一时都不知道怎么回答他们,个人认为是网友们把问题复杂化了. 其实就是把一些常用.实用.通用的功能封装起来而以,简单的来 ...

  3. Qt信号槽的一些事

    注:此文是站在Qt5的角度说的,对于Qt4部分是不适用的. 1.先说Qt信号槽的几种连接方式和执行方式. 1)Qt信号槽给出了五种连接方式: Qt::AutoConnection 0 自动连接:默认的 ...

  4. Mysql各种存储引擎的特性以及如何选择存储引擎

    几个常用存储引擎的特点 下面我们重点介绍几种常用的存储引擎并对比各个存储引擎之间的区别和推荐使用方式. 特点 Myisam BDB Memory InnoDB Archive 存储限制 没有 没有 有 ...

  5. Spring 4 官方文档学习(十一)Web MVC 框架

    介绍Spring Web MVC 框架 Spring Web MVC的特性 其他MVC实现的可插拔性 DispatcherServlet 在WebApplicationContext中的特殊的bean ...

  6. 基于<最简单的基于FFMPEG+SDL的视频播放器 ver2 (采用SDL2.0)>的一些个人总结

    最近因为项目接近收尾阶段,所以变的没有之前那么忙了,所以最近重新拿起了之前的一些FFMPEG和SDL的相关流媒体播放器的例子在看. 同时自己也用FFMPEG2.01,SDL2.01结合MFC以及网上罗 ...

  7. R语言boxplot绘图函数

    boxplot 用于绘制箱线图,我们都知道boxplot 用于展示一组数据的总体分布,在R语言中,支持两种输入数据的方式 第一种:x , 这个参数指定用于绘制箱线图所用的数据,是一个向量 代码示例: ...

  8. iOS获取时间、日期

    //获取当前时间 NSDateFormatter *formatter = [[[NSDateFormatter alloc] init]autorelease]; [formatter setLoc ...

  9. htaccess文件中RewriteRule 规则参数介绍

    .htaccess 文件 <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d Rew ...

  10. linux vi编辑器中,如何通过快捷键上下翻页?

    需求说明: 之前在vi的时候,如果想看下一页,就直接按住 ↓ 这个箭头一直翻,现在觉得有些麻烦, 就找了下上,下翻页的快捷方式.在此记录下. 记录: 1.向下翻页快捷键(下一页):Ctrl + f 2 ...