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 ...
随机推荐
- win7 32位配置apache+wsgi+django环境
1下载xampp,里面有apache,mysql,phpmyadmin, 2 下载wsgi,http://download.csdn.net/download/copter/9192361 将对应的模 ...
- 编写Web Serviceclient訪问www.webxml.com.cn提供的服务
好久没更新博客了,近期各种忙. 之前做Web Service课程的作业,当中有一个实验.实验内容如题所看到的. 以下简单说下怎样编写Web Serviceclient訪问webxml.com.cn里的 ...
- Build FTP Server on Windows
1. Use the self-ftp component service with windows control panel / program / start or close windows ...
- java资源分享、面试题资料、分布式大数据
马士兵大数据_架构师(1) 链接:http://pan.baidu.com/s/1qYTW1m0 密码:lxjd spring Cloud 链接:http://pan.baidu.com/s/1bzG ...
- ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]
源文 http://blog.csdn.net/xiaoting451292510/article/details/8562570 分类: 版本管理 2013-02-01 14:44 26057人阅读 ...
- 最长连续序列(Longest Consecutive Sequence)
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- PHP ORM操作MySQL数据库
ORM----Oriented Relationship Mapper,即用面向对象的方式来操作数据库.归根结底,还是对于SQL语句的封装. 首先,我们的数据库有如下一张表: 我们希望能够对这张表,利 ...
- Anyoffice -HTML5大赛 悦心(基于H5开发安卓音乐app)-项目总结
项目在线演示地址:http://rose111.applinzi.com/ github 地址:欢迎star https://github.com/midoxinxin/YueX-Music 悦心,一 ...
- Laravel 5.4建站06--API 认证系统 Passport
介绍 在 Laravel 中,实现基于传统表单的登陆和授权已经非常简单,但是如何满足 API 场景下的授权需求呢?在 API 场景里通常通过令牌来实现用户授权,而非维护请求之间的 Session 状态 ...
- 解压 boot.img
./split_bootimg.pl boot.img Page size: 2048 (0x00000800) Kernel size: 7062084 (0x006bc244) Ramdisk s ...