hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]
2015-03-14 22:38
471 查看
传送门
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
[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
.
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
)
For each test case, output a single line indicating the answer.
2 233
3 5
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
BestCoder Round #33
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/
再贴一发Java的程序:
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掉。
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 = 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的程序:
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 and contest(快速幂、快速乘法)
- HDU 5187 zhx's contest(快速乘法)
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU 5187 zhx's contest(快速幂乘法)
- hdu 5187 zhx's contest(快速幂,快速乘法,排列组合)
- HDU 5187 zhx's contest 快速幂,快速加
- hdu 4365 Palindrome graph (规律题 + 快速幂取模 2012 Multi-University Training Contest 7 )
- Hdu 5187 zhx's contest【思维+快速幂+快速乘】
- zhx's contest (hdu 5187 快速幂+快速乘法)
- hdu 5186 zhx's submissions 5187 zhx's contest快速幂小优化
- zhx's contest HDU - 5187 快速乘,解决乘数过大
- hdu 5187 快速乘方 快速乘法
- HDU 5187 zhx's contest(快速幂+快速乘法)
- hdu 5187(快速幂+快速乘法)
- HDU 5187-zhx's contest(快速乘法+快速幂取模)
- HDU 5187 zhx's contest(思维,快速幂,快速乘)
- hdu(5187):求漂亮的序列。快速幂的快速乘法实现
- [HDU 5187][组合数求和][快速幂+乘]zhx's contest
- hdu 5187 zhx's contest(快速幂+快速乘)
- hdu 5187 zhx's contest 快速幂,快速乘