2015-03-15 11:46:09
总结:
.... 最近状态实在是太差了,一直在刷水题 Orz ....
比赛中智商下线... A题 wa数次才过,B题高精度过pretest,后来TLE了.... 最近要多做做比赛恢复一下状态了。
A:手速题.... 打太快导致粗心的错误... (全局变量清空和>=的问题)
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
View Code
B:本来是个水快速幂,答案就是2^n - 2,特判一下n=1的情况。然而,坑爹的是,因为数据范围而需要高精度 / 快速乘法。(当然也可以用java)
下面来普及一下快速乘法,(下图摘自noip吧):

由于类似快速幂,所以以上被称为快速乘法。计算x * y % p时,如果 x,y,x*y%p 均不超过范围,但是x * y会超过范围。
这时候,快速乘法就派上用场了,把y化成其二进制形式,就变成:y = 2^k1 + 2^k2 + ... + 2^km,而x * y = x * (2^k1 + 2^k2 + ... + 2^km)
看到这里,由于分配律,乘法也可以二进制拆分。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
View Code
java版本:
1 import java.util.Scanner;
2 import java.math.BigInteger;
3
4 public class Main {
5 public static void main(String[] args){
6 Scanner in = new Scanner(System.in);
7 BigInteger n,p,ans;
8 BigInteger two = new BigInteger("2");
9 while(in.hasNext()){
10 n = in.nextBigInteger();
11 p = in.nextBigInteger();
12 ans = Q_pow(two,n,p).add(p).subtract(two).mod(p);
13 System.out.println(ans);
14 }
15 in.close();
16 }
17 static BigInteger Q_pow(BigInteger x,BigInteger y,BigInteger p){
18 BigInteger two = new BigInteger("2");
19 BigInteger res = new BigInteger("1");
20 BigInteger zero = BigInteger.ZERO;
21 while(y.compareTo(zero) > 0){
22 if(y.mod(two).compareTo(zero) != 0){
23 res = res.multiply(x).mod(p);
24 }
25 x = x.multiply(x).mod(p);
26 y = y.divide(two);
27 }
28 return res;
29 }
30 } View Code
C:背包是比较明显的... 题解给了状压DP。
然后看了别人的做法,大部分是O(30*10000)的 “暴力” DP。dp[i]表示前i个单位的时间能获得的最大分数。
逐个考虑每个题目,dp[i] = max(dp[i],dp[i - t[i]] + v[i])。
当然直接从1到n扫会有问题,我们应该把问题按照 l - t (即最早可能的起点)排序,这样可以使得晚开始的问题可以继承早开始的问题更新的dp值。
这样写很容易T,要剪枝,(1)处理出当前最大的可能时间,(2)实时更新答案,比如当前ans=10,那么时间>10的情况就不用考虑了。
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
View Code
转载于:https://www.cnblogs.com/naturepengchen/articles/4340061.html
版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!