您的位置:首页 > 编程语言 > Java开发

hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]

2015-03-14 22:38 471 查看
传送门

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


[align=left]Problem Description[/align]
As one of the most powerful brushes, zhx is required to give his juniors n


problems.
zhx thinks the i

th




problem's difficulty is i


. He wants to arrange these problems in a beautiful way.
zhx defines a sequence {a

i

}


beautiful if there is an i


that matches two 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 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


.

[align=left]Input[/align]
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≤10

18




)

[align=left]Output[/align]
For each test case, output a single line indicating the answer.

[align=left]Sample Input[/align]

2 233
3 5

[align=left]Sample Output[/align]

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

[align=left]Source[/align]
BestCoder Round #33

[align=left]Recommend[/align]
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掉。


131271942015-03-14 22:34:13Accepted5187109MS1664K1179 BG++czy
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <string>

#define ll long long
int const N = 205;
int const M = 205;
int const inf = 1000000000;
ll const mod = 1000000007;

using namespace std;

ll n,p;
ll ans;

ll quickmul(ll x,ll m)
{
ll re=0;
while(m){
if(m&1){
re=(re+x)%p;
}
m/=2;
x=(x+x)%p;
}
return re;
}

ll quickpow(ll x,ll m)
{
ll re=1;
while(m)
{
if(m&1){
re=quickmul(re,x);
}
m/=2;
x=quickmul(x,x);
}
return re;
}

void ini()
{

}

void solve()
{
if(n==1){
if(p!=1)
ans=1;
else
ans=0;
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的程序:

131310892015-03-15 10:47:30Accepted5187421MS9640K858 BJavaczy
//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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: