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 i
th![](https://www.cnblogs.com/)
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![](https://www.cnblogs.com/)
are monotone decreasing or monotone increasing.
2: a
i
..a
n![](https://www.cnblogs.com/)
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
.
zhx thinks the i
zhx defines a sequence {a
1: a
2: a
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≤10
18![](https://www.cnblogs.com/)
)
For each case, there are two integers n
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 = 1Source
Recommend
哎,醉死了,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 |
1 #include <cstdio> 2 #include <cstring> 3 #include <stack> 4 #include <vector> 5 #include <algorithm> 6 #include <queue> 7 #include <map> 8 #include <string> 9 10 #define ll long long 11 int const N = 205; 12 int const M = 205; 13 int const inf = 1000000000; 14 ll const mod = 1000000007; 15 16 using namespace std; 17 18 ll n,p; 19 ll ans; 20 21 ll quickmul(ll x,ll m) 22 { 23 ll re=0; 24 while(m){ 25 if(m&1){ 26 re=(re+x)%p; 27 } 28 m/=2; 29 x=(x+x)%p; 30 } 31 return re; 32 } 33 34 ll quickpow(ll x,ll m) 35 { 36 ll re=1; 37 while(m) 38 { 39 if(m&1){ 40 re=quickmul(re,x); 41 } 42 m/=2; 43 x=quickmul(x,x); 44 } 45 return re; 46 } 47 48 void ini() 49 { 50 51 } 52 53 void solve() 54 { 55 if(n==1){ 56 if(p!=1) 57 ans=1; 58 else 59 ans=0; 60 return; 61 } 62 else{ 63 ans=quickpow(2LL,n)-2LL; 64 ans=(ans+p)%p; 65 } 66 } 67 68 void out() 69 { 70 printf("%I64d ",ans); 71 } 72 73 int main() 74 { 75 //freopen("data.in","r",stdin); 76 //scanf("%d",&T); 77 //for(cnt=1;cnt<=T;cnt++) 78 while(scanf("%I64d%I64d",&n,&p)!=EOF) 79 { 80 ini(); 81 solve(); 82 out(); 83 } 84 }
再贴一发Java的程序:
13131089 | 2015-03-15 10:47:30 | Accepted | 5187 | 421MS | 9640K | 858 B | Java | czy |
1 //import java.io.*; 2 import java.util.*; 3 import java.math.*; 4 5 public class Main { 6 static BigInteger quickpow (BigInteger x, long m, BigInteger p ) 7 { 8 BigInteger re = BigInteger.ONE; 9 while(m >= 1) 10 { 11 if(m % 2 == 1){ 12 re = re.multiply(x).mod(p); 13 } 14 x = x.multiply(x).mod(p); 15 m = m / 2; 16 } 17 return re; 18 } 19 20 public static void main(String[] args){ 21 Scanner in = new Scanner(System.in); 22 long n; 23 BigInteger p; 24 BigInteger ans; 25 while(in.hasNext()) 26 { 27 n = in.nextLong(); 28 p = in.nextBigInteger(); 29 if(n == 1){ 30 if(p.equals(BigInteger.ONE)){ 31 ans = BigInteger.ZERO; 32 } 33 else{ 34 ans = BigInteger.ONE; 35 } 36 } 37 else{ 38 ans = quickpow(BigInteger.valueOf(2),n,p).subtract(BigInteger.valueOf(2)); 39 ans = ans.add(p).mod(p); 40 } 41 System.out.println(ans); 42 } 43 } 44 }