HDU5187 zhx's contest
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2388 Accepted Submission(s): 755
zhx thinks the ith 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 module p.
For each case, there are two integers n and p separated by a space in a line. (1≤n,p≤1018)
3 5
1
In 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
贪心?
答案是2^n-2
只是用来练快速乘的
/*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const int mxn=;
LL n,p;
LL ksmul(LL a,LL b){
a%=p;b%=p;LL res=;
while(b){
if(b&){res+=a;if(res>p)res-=p;}
a=(a<<)%p;
b>>=;
}
return res;
}
LL ksm(LL x,LL k){
LL res=;
while(k){
if(k&)res=ksmul(res,x);
x=ksmul(x,x);
k>>=;
}
return res;
}
int main(){
while(scanf("%lld%lld",&n,&p)!=EOF){
if(n==){
if(p==)printf("0\n");
else printf("1\n");
continue;
}
LL ans=ksm(,n);ans-=;
if(ans<)ans=((ans%p)+p)%p;
printf("%lld\n",ans);
}
return ;
}
HDU5187 zhx's contest的更多相关文章
- zhx and contest (枚举 + dfs)
zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- zhx's contest (矩阵快速幂 + 数学推论)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- HDOJ 5188 zhx and contest 贪婪+01背包
zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- hdu 5188 zhx and contest [ 排序 + 背包 ]
传送门 zhx and contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]
传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- hdu 5187 zhx's contest (快速幂+快速乘)
zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 【HDU5187】zhx's contest
[问题描述] 作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足 ...
- HDU5187 zhx's contest(计数问题)
主题链接: http://acm.hdu.edu.cn/showproblem.php?pid=5187 题意: 从1~n,有多少种排列 使得 a1~ai 满足单调递增或者单调递减. ai~an 满足 ...
- HDU 5188 zhx and contest(带限制条件的 01背包)
Problem Description As one of the most powerful brushes in the world, zhx usually takes part in all ...
随机推荐
- form submit 的callback方法
参考:http://hayageek.com/jquery-ajax-form-submit/ form的submit方法返回数据处理. 普通的form: $("#ajaxform" ...
- 数据分析处理库Pandas——数据透视表
数据 按指定的行列值显示 求和 按行求和 按列求和 数据 求平均 备注:按性别计算每个等级船票的平均价格. 备注:每个等级船舱中每种性别获救的平均值,也就是获救的比例. 备注:每种性别未成年人获救的平 ...
- 学习Pytbon第十天 函数2 内置方法和匿名函数
print( all([1,-5,3]) )#如果可迭代对象里所有元素都为真则返回真.0不为真print( any([1,2]) )#如果数据里面任意一个数据为真返回则为真a= ascii([1,2, ...
- 如何在CentOS 7.1中安装VMware Workstation
VMware Workstation在windows环境中大家都会安装,最近公司有个需求是需要在CentOS环境中安装VMware Workstation,上官网查了下还真有Linux版本的,于是就开 ...
- MySQL触发器和更新操作
一.触发器概念 触发器(trigger):监视某种情况,并触发某种操作,它是提供给程序员和数据分析员来保证数据完整性的一种方法,它是与表事件相关的特殊的存储过程,它的执行不是由程序调用,也不是手工启动 ...
- K-均值聚类——电影类型
K-均值聚类 K-均值算法试图将一系列样本分割成K个不同的类簇(其中K是模型的输入参数),其形式化的目标函数称为类簇内的方差和(within cluster sum of squared errors ...
- 20145202马超《java程序设计》第一周学习总结
这两天的学习让我对java有了初步的了解. 1.java是SUN公司推出的面相网络的编程语言. 特点:完全面向对象,与平台无关,跨平台性(例如c++只能在windows上执行,然而java并没有这些限 ...
- hover 改变另一个标签的属性
- jQuery监控动画执行完毕事件
$('#yourElement').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend' ...
- 《Cracking the Coding Interview》——第18章:难题——题目8
2014-04-29 03:10 题目:给定一个长字符串S和一个词典T,进行多模式匹配,统计S中T单词出现的总个数. 解法:这是要考察面试者能不能写个AC自动机吗?对面试题来说太难了吧?我不会,所以只 ...