zhx's contest(快速幂+快速乘法)
来源:互联网 发布:sql select 字段类型 编辑:程序博客网 时间:2024/05/21 01:55
Problem Description
As one of the most powerful brushes, zhx is required to give his juniorsn problems.
zhx thinks theith problem's difficulty is i . He wants to arrange these problems in a beautiful way.
zhx defines a sequence{ai} beautiful if there is an i that matches two rules below:
1:a1..ai are monotone decreasing or monotone increasing.
2:ai..an are monotone decreasing or monotone increasing.
He wants you to tell him that how many permutations of problems are there if the sequence of the problems' difficulty is beautiful.
zhx knows that the answer may be very huge, and you only need to tell him the answer modulep .
zhx thinks the
zhx defines a sequence
1:
2:
He wants you to tell him that how many permutations of problems are there if the sequence of the problems' difficulty is beautiful.
zhx knows that the answer may be very huge, and you only need to tell him the answer module
Input
Multiply test cases(less than 1000 ). Seek EOF as the end of the file.
For each case, there are two integersn and p separated by a space in a line. (1≤n,p≤1018 )
For each case, there are two integers
Output
For each test case, output a single line indicating the answer.
Sample Input
2 2333 5
Sample Output
21HintIn the first case, both sequence {1, 2} and {2, 1} are legal.In the second case, sequence {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} are legal, so the answer is 6 mod 5 = 1#include<stdio.h>typedef long long ll;ll n,p;ll qmul(ll a,ll b){ ll ans=0; while(b) { if(b%2) ans=(ans+a)%p; a=(a+a)%p; b/=2; } return ans;}ll qpow(ll a,ll b){ ll ans=1; while(b) { if(b%2) ans=qmul(ans,a)%p; a=qmul(a,a); b/=2; } return ans;}int main(){ while(scanf("%lld%lld",&n,&p)!=EOF) { ll ans; ans=qpow(2,n)-2; if(p==1) { printf("%d\n",0); continue; } else if(n==1) { printf("%d\n",1); continue; } if(ans<0) ans+=p; printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU 5187 zhx's contest(快速幂+快速乘法)
- zhx's contest(快速幂+快速乘法)
- HDU 5187zhx's contest(快速幂乘法)
- HDU 5187 zhx's contest(快速乘法)
- hdu 5187 zhx's contest(快速幂,快速乘法,排列组合)
- zhx's contest----快速幂+快速乘
- HDU 5187-zhx's contest(快速乘法+快速幂取模)
- HDU - 5187 - zhx's contest (快速幂+快速乘)
- 数学(快速幂+快速加法 hdu5187-zhx's contest)
- hdu5187 zhx's contest题解(快速幂快速积)
- zhx's contest BestCoder Round #33(快速乘法+快速幂的原理)
- BestCoder Round #33(zhx's contest-快速乘法)
- hdu 5187 zhx and contest(快速幂、快速乘法)
- hdu 5187 zhx's contest 快速幂,快速乘
- Hdu 5187 zhx's contest【思维+快速幂+快速乘】
- [练习][HDU5187]快速幂加强(快速幂+快速乘) zhx's contest
- hdu 5187 zhx's contest(快速幂+快速乘)
- JavaSE基础知识学习—Final关键字
- C#Dispose的标准代码以及CloseHandle()函数的使用
- Python学习笔记(15)- 异常
- 几本对于笔试和面试有用的书(干货~)
- 12、mybatis获取自增主键的值
- zhx's contest(快速幂+快速乘法)
- mysql关于utf8_unicode_ci与utf8mb4_unicode_ci的区别
- maven + spring项目 在eclipse中没问题在idea却报错:could not autowired
- PAT
- MongoDB
- 单例模式
- 三星的片子不支持can
- dataTables-使用详细说明整理
- Android面试题