HDU 5187 zhx's contest(思维,快速幂,快速乘)
来源:互联网 发布:淘宝网我是卖家 编辑:程序博客网 时间:2024/06/06 05:56
zhx's contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2541 Accepted Submission(s): 783
Problem Description
As one of the most powerful brushes, zhx is required to give his juniors n 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
Source
BestCoder Round #33
Recommend
hujie
这题我们可以借助DP的思想推出公式, [ 1 ~ N ] 所有满足条件的排列就等于 [ 1 ~ N -1 ] 所有满足条件的排列插进去一个 N,其中单调递增和单调递减排列我们可以将N插进三个位置 例如: 原排列为 [ 1 , 2 , 3 ] ,我们可以将4插入将其修改成 [ 1 , 2 , 3 , 4] 、[ 1 , 2 , 4 , 3]、[ 4 , 1 , 2, 3 ],而增减序列和减增序列只能将N插入两个位置 ( [ 1 , 3 , 2 ] 改为 [ 1 , 3 , 4 ,2]、[ 1 , 4 , 3 , 2] ),显而易见状态转移方程为dp[ i ] = dp[ i - 1 ] * 2 + 2
然而这题的数据范围太大,并不允许我们dp求解,但我们可以借助dp打表推公式。。。。
显而易见公式为 2 ^ n -2,然而这题数据范围太大,直接用快速幂会爆 long long ,所以还需加一步快速乘。
PS:数学不好真心伤不起啊。。忙活半天才把公式找出来。。。。。。。
#include<bits/stdc++.h>using namespace std;long long n,Mod;long long mul(long long a,long long b){ long long res=0; while(b) { if(b&1) res=(a+res)%Mod; b>>=1; a=(a+a)%Mod; } return res;}long long xx(long long a,long long n){ long long ret=1; long long tmp=a%Mod; while(n) { if(n&1) ret=mul(ret,tmp)%Mod; tmp=mul(tmp,tmp)%Mod; n>>=1; } return ret;}int main(){ while(scanf("%I64d%I64d",&n,&Mod)==2) { printf("%I64d\n",(xx(2,n)%Mod-2+Mod)%Mod); ///2^n-2可能为负数,所以要加一个Mod }}
0 0
- Hdu 5187 zhx's contest【思维+快速幂+快速乘】
- HDU - 5187 - zhx's contest (快速幂+快速乘)
- HDU 5187 zhx's contest(思维,快速幂,快速乘)
- hdu 5187 zhx's contest 快速幂,快速乘
- hdu 5187 zhx's contest(快速幂+快速乘)
- zhx's contest----快速幂+快速乘
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU 5187 zhx's contest(快速幂+快速乘法)
- HDU 5187zhx's contest(快速幂乘法)
- [HDU 5187][组合数求和][快速幂+乘]zhx's contest
- HDU 5187 zhx's contest(快速乘法)
- [练习][HDU5187]快速幂加强(快速幂+快速乘) zhx's contest
- hdu 5187 zhx's contest(快速幂矩阵6)
- hdu 5187 zhx's contest(快速幂,快速乘法,排列组合)
- hdu 5186 zhx's submissions 5187 zhx's contest快速幂小优化
- HDU 5187-zhx's contest(快速乘法+快速幂取模)
- HDU 5187 zh's contest(快速幂 + 快速乘)
- 数学(快速幂+快速加法 hdu5187-zhx's contest)
- Queue Reconstruction by Height
- linux命令详解之df命令
- 嵌入式Linux C语言开发工具
- Android IPC机制-小试牛刀
- 欢迎使用CSDN-markdown编辑器
- HDU 5187 zhx's contest(思维,快速幂,快速乘)
- OpenStack_Networking
- Scorller的使用详解一
- 算法导论 4.1-5 最大子数组问题 线性时间算法 C++简单实现
- 利用mybatis-generator自动生成代码
- ACM-动态规划16-踩方格
- 初识Java
- Python入门
- Tengine(二) 之 nginx.conf