HDU1852 Beijing 2008(快速幂+特殊公式)
Now given a positive integer N, get the sum S of all positive integer divisors of 2008 N. 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 2008 M, 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, 2008 M % K = 5776.
InputThe 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.
OutputFor each test case, in a separate line, please output the result.
Sample Input
1 10000
0 0
Sample Output
5776
题意:求M=2008^n的所有因子和%K;输出2008^M%K。
思路:后面部分就是普通快速幂了;问题关键在于解决前面部分。
因子和:由唯一分解,2008=(2^3)*(251^1)。所以2008^n=(2^3n)*(251^n),则因子有(3n+1)*(n+1)个;sum=(1+2+4+...2^3n)*(1+251+..+251*n)。
对其合并,得ans=(2^(3n+1)-1)*(251^(n+1)-1)/250。(等比数列)
解决分母250:ans%K,而ans有分母250,且gcd(250,K)不一定就为1,即不一定互素,所以不能用逆元解决。
公式:(a/b)%mod=a%(b*mod)/b%mod
(emmm,注意代码里两个等比数列求和都是取余250*k,尽管前面一个没有分母250.)
对比:这个公式使用于分母比较小的情况,而且不要求b,Mod互素,条件是a|b。而一般求组合数(n!)/(m!*(n-m)!)%Mod,由于分母较大,所以采用求逆元求解的方法。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
#define ll long long
ll q_pow(ll a,ll x,ll Mod)
{
ll res=;
while(x){
if(x&1LL) res=res*a%Mod;
a=a*a%Mod;
x>>=;
}return res;
}
int main()
{
ll n,k,sum;
while(~scanf("%lld%lld",&n,&k)){
if(n==&&k==) return ;
sum=(q_pow(,*n+,*k)-)*(q_pow(,n+,*k)-)%(*k)/;
printf("%lld\n",q_pow(,sum,k));
} return ;
}
HDU1852 Beijing 2008(快速幂+特殊公式)的更多相关文章
- hdu1852 Beijing 2008
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1852 题目大意: 求2008^n的所有因子和m对k取余,然后求2008^m对k取余. 解题思路: 首 ...
- 整数快速幂hdu(1852)
hdu1852 Beijing 2008 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others ...
- bzoj1008: [HNOI2008]越狱 数学公式+快速幂
bzoj1008: [HNOI2008]越狱 O(log N)---------------------------------------------------------------- ...
- BZOJ2326 [HNOI2011]数学作业(分块矩阵快速幂)
题意: 定义函数Concatenate (1 ..N)是将所有正整数 1, 2, …, N 顺序连接起来得到的数,如concatenate(1..5)是12345,求concatenate(1...n ...
- hdu 1852(快速幂模+有除法的时候取模的公式)
Beijing 2008 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Tota ...
- CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)
问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...
- HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description Sample Input 2 Sample Outp ...
- HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂
装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...
- POJ-1845 Sumdiv---因子和(快速幂+快速加法+因子和公式)
题目链接: https://cn.vjudge.net/problem/POJ-1845 题目大意: 求AB的因子和 解题思路: 先将A质因数分解,然后B次方的质因数指数就是乘上B即可 这里要mod9 ...
随机推荐
- Access自定义函数(人民币大写)
人民币大写函数:整数不超过13位. Public Function 人民币大写(A) As String Dim aa As String Dim bb As String Dim cc As Str ...
- AngularJS的Foreach循环示例
代码下载:https://files.cnblogs.com/files/xiandedanteng/angularJSForeach.rar 代码: <!DOCTYPE HTML PUBLIC ...
- Android的包管理机制浅析(二)
上篇刚好说到获取到了签名信息,以下进入安装过程,直接上源代码: private void installNewPackageLI(PackageParser.Package pkg, int pars ...
- 百度 BAE 项目部署
转载:http://www.cnblogs.com/shamoyuu/p/node_bae.html 百度有一个应用引擎,价格非常便宜,Java的tomcat每天4毛钱,node每天2毛钱,我以前在上 ...
- ftp的实现
ftp.h #define BUFSIZE 512#define CMDSIZE 64#define ARGSIZE 64#define PASSIVE_ON 0x1 struct ftpcmd{ c ...
- 小数运算需要注意什么? 接口和抽象类 WinForm窗体上两个panel,怎么实现一个panel固定漂浮在另一个panel之上
小数运算需要注意什么? 1. 生活中0.1+0.2=0.3, 计算机中可不是这样,为什么呢? 大家都知道计算机类型都是有数据范围的.整形int范围是 正负21亿左右,小数类型同样也是有范围的,但是即使 ...
- 摘自《Linux与unix shell编程指南》
shift运行后,$#随之减少:如果需要知道命令行中输入的最后一个参数(通常是一个文件名),可以有两种选择:使用命令 eval echo \$$#;使用shift命令:shift 'expr $# - ...
- surface 通过U盘 镜像恢复系统
1. 在恢复之前首先要解锁bitlocker(如果你的surface没有加锁就不需要这个步骤) 在另一台电脑上登录bitlocker锁绑定的微软账号,查询密钥,在需要的地方输入这个密钥(不经过这个操作 ...
- 用callgraph生成的两张函数调用关系图
参考这里,感觉很Cool吧. Linux-0.11函数调用关系图: QEMU函数调用关系图:
- Canvas学习笔记——动画中的三角学
示例1,跟随鼠标的键头: 需要掌握一个重要的公式,这个方法返回从 x 轴到点 (x,y) 之间的角度 Math.atan2(dy,dx); 关键代码: function Arrow() { thi ...