zhx's contest
来源:互联网 发布:安卓玩java游戏 编辑:程序博客网 时间:2024/06/06 04:46
Time Limit: 1000 ms / MemoryLimit: 65536 kb
Description
As one of the most powerfulbrushes, zhx is required to give his juniors $n$ problems.
zhx thinks the $i^{th}$ problem's difficulty is $i$. He wants to arrange theseproblems in a beautiful way.
zhx defines a sequence $\{a_{i}\}$ beautiful if there is an $i$ that matchestwo rules below:
1: $a_{1} .. a_{i}$ are monotone decreasing or monotone increasing.
2: $a_{i} .. a_{n}$ are monotone decreasing or monotone increasing.
He wants you to tell him that how many permutations of problems are there ifthe sequence of the problems' difficulty is beautiful.
zhx knows that the answer may be very huge, and you only need to tell him theanswer module $p$.
Input
Multiply test cases(less than $1000$).Seek $EOF$ as the end of the file.
For each case, there are two integers $n$ and $p$ separated by a space in aline. ($1 \leq n,p \leq 10^{18}$)
Output
For each test case, output asingle line indicating the answer.
Sample Input
2 233
3 5
Sample Output
2
1
Hint
In the firstcase, both sequence {1, 2} and {2, 1} are legal.
In the secondcase, 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
分析:
数字1到n的全排列问题
限制条件为:
1: a1..ai are monotone decreasing or monotone increasing.
2: ai..an are monotone decreasing or monotoneincreasing.
考虑:
a. 全递增/全递减
b. 先增→ai→再减(ai(max)=n
把n放在任意位置,除了n剩余的数都有两种排法:ai左边或者ai右边故:2^(n—1),其中包括了情况a的两种(n在第一个位置和n在最后一个位置 故减去后:2^(n-1)-2
c. 先减→ai→再增 ai(min)=1
除了ai剩余的数都有两种排法:ai左边或者ai右边故:2^(n—1),其中包括了情况a的两种,减去后:2^(n-1)-2
总:a+b+c=2+2^n-4=2^n-2 种
答案:(2^n-2 )%p
注意特殊情况,当n=1,输出1;当p=1,输出0
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long ll;ll mod;ll mul_mod(ll a, ll n) //快速乘取模{ll ans = 0;while (n){if (n & 1) ans = (ans + a) % mod;a = (a + a) % mod;n /= 2;}return ans;}ll pow_mod(ll a, ll n) //快速幂取模{ll ans = 1;while (n){if (n & 1) ans = mul_mod(ans, a);a = mul_mod(a, a);n >>= 1;}return ans;}int main(){long long n;while (~scanf("%lld %lld", &n, &mod)){if (n == 1){if (mod == 1) printf("0\n");else printf("1\n");}else printf("%lld\n", (pow_mod(2, n) - 2 + mod) % mod);}return 0;}
快速幂取模:http://blog.csdn.net/paradoxo/article/details/74936496
快速乘取模:http://blog.csdn.net/paradoxo/article/details/74936435
- zhx's contest
- zhx's contest HDU
- HDU 5187 zhx's contest
- HDU - 5187 zhx's contest
- hdu 5187-zhx's contest
- HDU - 5187 - zhx's contest
- HDOJ 5178 zhx's contest
- hdu 5187 zhx's contest ( 数学+java)
- BestCoder Round #33 1002 zhx's contest
- HDU5187 zhx's contest(计数问题)
- HDU 5187 zhx's contest(快速乘法)
- Hdu 5187 zhx's contest(数学)
- zhx's contest----快速幂+快速乘
- hdu 5186 zhx's submissions 5187 zhx's contest快速幂小优化
- HDU 5187 zhx's contest(防爆__int64 )
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU - 5187 - zhx's contest (快速幂+快速乘)
- 数学(快速幂+快速加法 hdu5187-zhx's contest)
- 翻页效果
- 第七章STL之仿函数
- HDOJ 1078 FatMouse and Cheese (DFS+DP)
- Spark Streaming中的操作函数
- maven(一)pom文件讲解
- zhx's contest
- JAVA企业面试题精选 Java基础 1-10
- 选课时间(题目已修改,注意读题)
- ThreadLocal源码分析
- JAVA中的三大集合框架
- OpenGL中的光照
- 文章标题
- Android系列之实现ViewFlipper图片动画滑动
- 51 《格鲁夫给经理人的第一课》 -豆瓣评分8.8