HDU 5187 zhx's contest(快速幂+快速乘法)
2015-03-15 19:37
375 查看
题意:
数字1~n,按某种顺序排列,且满足下列某一个条件:(1)a1~ai递增,ai~an递减
(2)a1~ai递减,ai~an递增。
问有多少种不同的排列。
思路:
首先是全部递减或全部递增各一种,共2种。另外就是满足上列两个条件的情况了,
要想满足条件(1)那就只能把最大的n放在i位置,
共有C(1,n-1)+C(2,n-1)+……+C(n-2,n-1)即2^(n-1)-2;
条件(2)与(1)相同,所以共有(2^(n-1)-2)*2+2=2^n-2
上面的思路是参考别人的,比赛的时候没有这样找规律,而是写了一个暴力程序来找出规律的。
注意:
这题的结果可能会很大,所以要写用java,或者 快速乘法。AC代码
[code]import java.math.*; import java.util.*; public class Main { static BigInteger n, Mod; static BigInteger two = BigInteger.valueOf(2); static BigInteger modpow(BigInteger a, BigInteger k) { BigInteger c = BigInteger.ONE; while(k.longValue() > 0) { if((k.longValue() & 1) == 1) c = c.multiply(a).mod(Mod); a = a.multiply(a).mod(Mod); k = k.divide(two); } return c; } static BigInteger solve(BigInteger val) { return ( modpow(two, val).add(Mod).subtract(two) ).mod(Mod); } public static void main(String []args) { Scanner cin = new Scanner(System.in); while(cin.hasNext()) { n = cin.nextBigInteger(); Mod = cin.nextBigInteger(); if(n.compareTo(BigInteger.ONE) == 0) { System.out.println(n.mod(Mod)); }else { System.out.println(solve(n)); } } } }
相关文章推荐
- HDU 5187-zhx's contest(快速乘法+快速幂取模)
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU - 5187 - zhx's contest (快速幂+快速乘)
- hdu 5187 zhx's contest(快速幂矩阵6)
- Hdu 5187 zhx's contest(数学)
- HDU 5187 zhx's contest(防爆__int64 )
- HDU - 5187 - zhx's contest (高速幂+高速乘)
- hdu 5186 zhx's submissions 5187 zhx's contest快速幂小优化
- hdu 5187 zhx's contest ( 数学+java)
- HDU 5187 zhx's contest(防爆__int64 )
- hdu5187_zhx's contest(快速幂+快速乘法)
- HDU 5187 zhx's contest(快速乘法)
- hdu 5187 zhx's contest 快速幂,快速乘
- zhx's contest (hdu 5187 快速幂+快速乘法)
- HDU - 5187 - zhx's contest
- hdu 5187 zhx and contest(快速幂、快速乘法)
- HDU 5187 zhx's contest(快速幂乘法)
- hdu 5187 zhx's contest(快速幂,快速乘法,排列组合)
- BestCoder Round #33(zhx's contest-快速乘法)
- hdu 5171 GTY's birthday gift (矩阵快速幂求类斐波那契数列)