传送门

zhx's contest

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 575    Accepted Submission(s): 181

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

problems.
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

.

 
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 a line. (1≤n,p≤1018

)

 
Output
For each test case, output a single line indicating the answer.
 
Sample Input
2 233
3 5
 
Sample Output
2
1

Hint

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

 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5189 5184 5181 5180 5177 
 
 
哎,醉死了,2个坑点,相乘会爆long long 要用快速幂,n=1时要特判p=1;
其他转官方题解了:http://bestcoder.hdu.edu.cn/
1002 zhx and contest
如果n=1  ,答案是1  ,否则答案是2 n −2  。
证明:a i   肯定是最小的或者最大的。考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的。
那么a i   是最小或者最大分别有2 n−1   种情况,而整个序列单调增或者单调减的情况被算了2次,所以要减2。
要注意的一点是因为p>2 31   ,所以要用快速乘法。用法与快速幂相同。如果直接乘会超过long long范围,从而wa掉。
13127194 2015-03-14 22:34:13 Accepted 5187 109MS 1664K 1179 B G++ czy
 #include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string> #define ll long long
int const N = ;
int const M = ;
int const inf = ;
ll const mod = ; using namespace std; ll n,p;
ll ans; ll quickmul(ll x,ll m)
{
ll re=;
while(m){
if(m&){
re=(re+x)%p;
}
m/=;
x=(x+x)%p;
}
return re;
} ll quickpow(ll x,ll m)
{
ll re=;
while(m)
{
if(m&){
re=quickmul(re,x);
}
m/=;
x=quickmul(x,x);
}
return re;
} void ini()
{ } void solve()
{
if(n==){
if(p!=)
ans=;
else
ans=;
return;
}
else{
ans=quickpow(2LL,n)-2LL;
ans=(ans+p)%p;
}
} void out()
{
printf("%I64d\n",ans);
} int main()
{
//freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(cnt=1;cnt<=T;cnt++)
while(scanf("%I64d%I64d",&n,&p)!=EOF)
{
ini();
solve();
out();
}
}

再贴一发Java的程序:

13131089 2015-03-15 10:47:30 Accepted 5187 421MS 9640K 858 B Java czy
 //import java.io.*;
import java.util.*;
import java.math.*; public class Main {
static BigInteger quickpow (BigInteger x, long m, BigInteger p )
{
BigInteger re = BigInteger.ONE;
while(m >= 1)
{
if(m % 2 == 1){
re = re.multiply(x).mod(p);
}
x = x.multiply(x).mod(p);
m = m / 2;
}
return re;
} public static void main(String[] args){
Scanner in = new Scanner(System.in);
long n;
BigInteger p;
BigInteger ans;
while(in.hasNext())
{
n = in.nextLong();
p = in.nextBigInteger();
if(n == 1){
if(p.equals(BigInteger.ONE)){
ans = BigInteger.ZERO;
}
else{
ans = BigInteger.ONE;
}
}
else{
ans = quickpow(BigInteger.valueOf(2),n,p).subtract(BigInteger.valueOf(2));
ans = ans.add(p).mod(p);
}
System.out.println(ans);
}
}
}

hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]的更多相关文章

  1. hdu 5187 zhx's contest

    题目分析如果n=1,答案是1,否则答案是2n−2. 证明:ai肯定是最小的或者最大的.考虑另外的数,如果它们的位置定了的话,那么整个序列是唯一的. 那么ai是最小或者最大分别有2n−1种情况,而整个序 ...

  2. HDU 5187 zhx's contest 快速幂,快速加

    题目链接: hdu: http://acm.hdu.edu.cn/showproblem.php?pid=5187 bc(中文): http://bestcoder.hdu.edu.cn/contes ...

  3. hdu 5187 zhx's contest (快速幂+快速乘)

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

  4. HDU - 5187 zhx's contest(快速幂+快速乘法)

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

  5. HDU 3032 multi-sg 打表找规律

    普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...

  6. HDU 4549 矩阵快速幂+快速幂+欧拉函数

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  7. 取模性质,快速幂,快速乘,gcd和最小公倍数

    一.取模运算 取模(取余)运算法则: 1. (a+b)%p=(a%p+b%p)%p; 2.(a-b)%p=(a%p-b%p)%p; 3.(a*b)%p=(a%p * b%p)%p; 4.(a^b)%p ...

  8. 【bzoj4870】[Shoi2017]组合数问题 dp+快速幂/矩阵乘法

    题目描述 输入 第一行有四个整数 n, p, k, r,所有整数含义见问题描述. 1 ≤ n ≤ 10^9, 0 ≤ r < k ≤ 50, 2 ≤ p ≤ 2^30 − 1 输出 一行一个整数 ...

  9. hdu 2604 Queuing dp找规律 然后矩阵快速幂。坑!!

    http://acm.hdu.edu.cn/showproblem.php?pid=2604 这题居然O(9 * L)的dp过不了,TLE,  更重要的是找出规律后,O(n)递推也过不了,TLE,一定 ...

随机推荐

  1. 开发原生安卓cordova插件(基础)

    cordova应用如果需要调用原生安卓接口,方法是使用cordova插件,cordova官方提供了主流原生功能的插件,但如果还不能满足需求,也可以自己开发cordova插件 以下介绍开发一个最简单的插 ...

  2. 从源码中无法看出函数所在的js脚本的解决方法

    通过设置断点调试使js脚本自动出现

  3. innerHTML与IE浏览器内存泄露问题

    使用 sIEve 扫描和筛选 如果大量使用 JavaScript 和 Ajax 技术开发 Web 2.0 应用程序,您很有可能会遇到浏览器的内存泄漏问题.如果您有一个单页应用程序或者一个页面要处理很多 ...

  4. 洛谷 P1351 联合权值

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  5. MongoDB最简单的入门教程之三 使用Java代码往MongoDB里插入数据

    前两篇教程我们介绍了如何搭建MongoDB的本地环境: MongoDB最简单的入门教程之一 环境搭建 以及如何用nodejs读取MongoDB里的记录: MongoDB最简单的入门教程之二 使用nod ...

  6. VMware 彻底删除虚拟机操作系统的方法

    方法一 首先,都需要点击左边的虚拟机列表,选中你要删除的操作系统 点击VMwae上方的虚拟机-管理-从硬盘删除. 方法二 右键左侧列表中要删除的系统-移除. 然后在硬盘上找到其所在文件夹,直接按SHI ...

  7. Graveyard LA3708

    白书第一章例题4 思维. 先固定一点不动,假设最后一共N个点,那么编号为0,1,...N-1, 0不动,原来的n个点分别占据i/n*N的位置(记为pos),移动到pos四舍五入的位置即可. 证明一:有 ...

  8. js数组常用方法整理

    学疏才浅,若有不对的地方,希望大家可以帮忙指正出来. 1. Array.push(),向数组的末尾添加一个或多个元素,并返回新的数组长度.原数组改变. 2. Array.pop(),删除并返回数组的最 ...

  9. JS常用字符串处理方法应用总结

    这篇文章主要总结了JS常用字符串的处理方法,需要的朋友可以参考下   1.indexOf()方法,从前往后查找字符串位置,大小写敏感,从0开始计数.同理,lastIndexOf() 方法从后往前,两个 ...

  10. 连接远程docker内的mysql(navicat)

    拉取mysql镜像 docker pull mysql:5.6 查看mysql镜像 docker images | grep mysql 启动mysql容器 docker run -p 3306:33 ...