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. Why is chkconfig no longer available in Ubuntu?

    Question: I can not use chkconfig tools in Ubuntu 12.10 It's a very useful tools to configure the se ...

  2. C++11 并发指南四(<future> 详解三 std::future & std::shared_future)(转)

    上一讲<C++11 并发指南四(<future> 详解二 std::packaged_task 介绍)>主要介绍了 <future> 头文件中的 std::pack ...

  3. windows ffmpeg 推送摄像头数据到rtmp服务

    文本主要讲述windows系统下如何利用ffmpeg获取摄像机流并推送到rtmp服务,命令的用法前文 中有讲到过,这次是通过代码来实现.实现该项功能的基本流程如下: 图1 ffmpeg推流流程图 较前 ...

  4. android等待旋转圆圈动画

    先创建一个动画的xml文件例如以下 <? xml version="1.0" encoding="utf-8"?> <animation-li ...

  5. 自己定义UITextField

    目的是实现例如以下的效果: UITextField的leftView是自己定义的UIView,当中: 1.包括一个居中显示的icon.而且上,左,下各有1px的间隙 2.左上和左下是圆角,右边没有圆角 ...

  6. idea刷新项目、清除项目缓存

    点击File -> Invalidate caches ,点击之后在弹出框中点击确认,之后软件就自动重启了

  7. 【网络协议】TCP的流量控制机制

    一般来说,我们总是希望传输数据的更快一些,但假设发送方把数据发送的非常快.而接收方来不及接收,这就可能造成数据的丢失.流量控制就是让发送方的发送速率不要太快.让接收方来得及接收. 对于成块数据流,TC ...

  8. caffe搭建--ubuntu标准平台的搭建

    http://caffe.berkeleyvision.org/install_apt.html Ubuntu Installation General dependencies sudo apt-g ...

  9. sublime常用的插件

    Sublime Text常用插件 1.Package Control 快捷键ctrl+~调出Sublime Text控制台,然后输入以下代码(Sublime Text3)安装Package Contr ...

  10. ETL Automation完整安装方法_(元数据存放在mysql数据库)

    安装前介质准备: DBI-1.636.tar.gz DBD-mysql-4.037.tar.gz ETL.tar mysql-5.6.12-linux-glibc2.5-x86_64.tar.gz P ...