主题链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5187

题意:

从1~n,有多少种排列

使得 a1~ai 满足单调递增或者单调递减。

ai~an 满足单调递增或者递减。

非常明显的组合问题

从n个数种选出i个数 剩下的数要满足单调递增或者递减或者递减的规律那么方式唯一

ans = (C(N,0)+C(N,1)+......+C(N,N)) =2^N;

可是这样的情况下 单调递增和单调递减算了两遍  因此要减2

ans = 2^n - 2;

注意n = 1的情况 ,因为n比較大 ,要注意乘法溢出的情况

代码例如以下:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std; typedef long long LL; LL multi(LL a,LL b, LL c)
{
LL ans = 0;
while(b){
if(b&1){
ans= (ans+a)%c;
b--;
}
b>>=1;
a=(a+a)%c;
}
return ans;
} LL quick_mod(LL a,LL b,LL c)
{
LL ans = 1;
while(b){
if(b&1){
ans = multi(ans,a,c);
b--;
}
b>>=1;
a=multi(a,a,c);
}
return ans ;
}
int main()
{
LL n,p;
while(~scanf("%lld%lld",&n,&p)){
if(n==1){
printf("%d\n",1%p);
continue;
}
LL ans = 2;
ans = quick_mod(ans,n,p);
ans =(ans - 2 + p)%p;
printf("%I64d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU5187 zhx&#39;s contest(计数问题)的更多相关文章

  1. HDU - 5187 - zhx&#39;s contest (高速幂+高速乘)

    zhx's contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  2. HDU 5187 zhx&#39;s contest(防爆__int64 )

    Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...

  3. HDU5187 zhx's contest

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission ...

  4. Andrew Stankevich&#39;s Contest (1)

    Andrew Stankevich's Contest (1) 打一半出门了,回来才补完了...各种大数又不能上java..也是蛋疼无比 A:依据置换循环节非常easy得出要gcd(x, n) = 1 ...

  5. HDU - 5186 - zhx&#39;s submissions (精密塔尔苏斯)

    zhx's submissions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. HDU 5186 zhx&#39;s submissions (进制转换)

    Problem Description As one of the most powerful brushes, zhx submits a lot of code on many oj and mo ...

  7. HDU2481 Toy

    Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  8. 【HDU5187】zhx's contest

    [问题描述] 作为史上最强的刷子之一,zhx的老师让他给学弟(mei)们出n道题.zhx认为第i道题的难度就是i.他想要让这些题目排列起来很漂亮. zhx认为一个漂亮的序列{ai}下列两个条件均需满足 ...

  9. zhx and contest (枚举  + dfs)

    zhx and contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

随机推荐

  1. js 99乘法表

    哈哈哈,笑死我了,突然怀念学习时代,撸了一个乘法表 for(let a=1;a<10;a++){let str = ''; for(let b=1;b<10;b++){ str = str ...

  2. 多线程之线程通信条件Condition二

    接上一篇,实现Condition三个条件,有这样一个应用: 1. 有三个进程,第一个进程运行1次,第二个进程运行2次,第三个进程运行3次: 2. 先运行第二个进程,然后第一个,然后第三个: 3.  依 ...

  3. JAVA学习路线图---(JAVA1234) 分类: B1_JAVA 2013-10-05 10:22 502人阅读 评论(1) 收藏

    转自:http://blog.csdn.net/pplcheer/article/details/12276999 第一阶段-Java基础        这一阶段很重要,关系到你后面阶段的学习,所以务 ...

  4. [Node.js] Create a model to persist data in a Node.js LoopBack API

    In this lesson you will learn what a LoopBack model is, you will create a Product model using the Lo ...

  5. Android中的动画详解系列【1】——逐帧动画

    逐帧动画其实很简单,下面我们来看一个例子: <?xml version="1.0" encoding="utf-8"?> <animation ...

  6. css 父div如何包裹带有float属性的子div,float子div如何撑开父div

    来自网络摘抄 原始代码 <style> #div1{border:1px solid red;float:left;} #div2,#div3{float:right;border:1px ...

  7. 诊断并解决CentOS SSH连接慢的方法

    诊断并解决CentOS SSH连接慢的方法: http://os.51cto.com/art/201507/484743.htm

  8. (转自aierong原创技术随笔)sqlserver字符串拆分(split)方法汇总

    sqlserver字符串拆分(split)方法汇总   --方法0:动态SQL法declare @s varchar(100),@sql varchar(1000)set @s='1,2,3,4,5, ...

  9. 【codeforces 758B】Blown Garland

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  10. 微服务学习笔记(2)——使用Consul 实现 MagicOnion(GRpc) 服务注册和发现

    原文:微服务学习笔记(2)--使用Consul 实现 MagicOnion(GRpc) 服务注册和发现 1.下载打开Consul 笔者是windows下面开发的(也可以使用Docker). 官网下载w ...