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 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(快速幂+特殊公式)的更多相关文章

  1. hdu1852 Beijing 2008

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1852 题目大意: 求2008^n的所有因子和m对k取余,然后求2008^m对k取余. 解题思路: 首 ...

  2. 整数快速幂hdu(1852)

    hdu1852 Beijing 2008 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others ...

  3. bzoj1008: [HNOI2008]越狱 数学公式+快速幂

    bzoj1008: [HNOI2008]越狱      O(log N)---------------------------------------------------------------- ...

  4. BZOJ2326 [HNOI2011]数学作业(分块矩阵快速幂)

    题意: 定义函数Concatenate (1 ..N)是将所有正整数 1, 2, …, N 顺序连接起来得到的数,如concatenate(1..5)是12345,求concatenate(1...n ...

  5. hdu 1852(快速幂模+有除法的时候取模的公式)

    Beijing 2008 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/65535 K (Java/Others)Tota ...

  6. CCF 201312-4 有趣的数 (数位DP, 状压DP, 组合数学+暴力枚举, 推公式, 矩阵快速幂)

    问题描述 我们把一个数称为有趣的,当且仅当: 1. 它的数字只包含0, 1, 2, 3,且这四个数字都出现过至少一次. 2. 所有的0都出现在所有的1之前,而所有的2都出现在所有的3之前. 3. 最高 ...

  7. HDU 4704 Sum(隔板原理+组合数求和公式+费马小定理+快速幂)

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=4704 Problem Description   Sample Input 2 Sample Outp ...

  8. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...

  9. POJ-1845 Sumdiv---因子和(快速幂+快速加法+因子和公式)

    题目链接: https://cn.vjudge.net/problem/POJ-1845 题目大意: 求AB的因子和 解题思路: 先将A质因数分解,然后B次方的质因数指数就是乘上B即可 这里要mod9 ...

随机推荐

  1. iOS开发之Xcode8兼容适配iOS 10资料整理笔记

    1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化只是小打小闹,直至现在iOS 10开始真正的进行大改重构,这让开发者也体会到UserN ...

  2. Ffmpeg 实现文件切割

    文件切割是一项很常见的基本功能,通过Ffmpeg可以很容易实现这项功能. 首先介绍下基本原理,文件切割说白了就过滤掉文件的部分音视频包,按照什么规则过滤呢? 答案是时间戳.文件中每个视频及音频包都有时 ...

  3. Oracle pipe

    初次接触到Report ,看到了它背后复杂的SQL操作, 首先看到了一个Pipe,先了解下PIPE的用法: 关键字PIPELINED表明这是一个oracle管道函数,oracle管道函数的返回值类型必 ...

  4. TCP/IP协议组学习笔记

    TCP/IP协议族学习笔记: 一.基础概念: (1)TCP(Transmission Control Protocol) 传输控制协议. (2)IP(Internet Protocol)网际协议.IP ...

  5. [GUIDE] How to Setup Ubuntu 16.04 LTS Xenial Xerus for Compiling Android ROMs

    With a new version of Ubuntu comes an update to my guide for setting up a build environment to compi ...

  6. kubernetes之初始容器(init container)

    系列目录 理解初始容器 一个pod里可以运行多个容器,它也可以运行一个或者多个初始容器,初始容器先于应用容器运行,除了以下两点外,初始容器和普通容器没有什么两样: 它们总是run to complet ...

  7. unity 接触一个月的感受和心得

    unity scrollview 遇到的问题 一个scrollview作为翻页,这样的效果调整. 一页上面有三个scrollview,这三个scrollview上的items不受下层整个页面的scro ...

  8. EasyDarwin开源手机直播方案:EasyPusher手机直播推送,EasyDarwin流媒体服务器,EasyPlayer手机播放器

    在不断进行EasyDarwin开源流媒体服务器的功能和性能完善的同时,我们也配套实现了目前在安防和移动互联网行业比较火热的移动端手机直播方案,主要就是我们的 EasyPusher直播推送项目 和 Ea ...

  9. 什么是aop?-------转

    什么是AOP?  http://www.cnblogs.com/zhugenqiang/archive/2008/07/27/1252761.html#commentform(转) AOP(Aspec ...

  10. Exam 70-762 Developing SQL Databases

    这个考试还是很有用的,教了很多有用的东西,不错,虽然考试需要很多钱,不过值的尝试.虽然用了sql server 这么多年但是对于事务.多并发的优化还是处于小学生的水平,通过这次考试争取让自己提一个档次 ...