• P1832 A+B Problem(再升级)


    P1832 A+B Problem(再升级)

    写高数太累了,干点别的事情.

    题目莫名其妙.

    除了初始化,还有终止条件.

    方案数问题,完全背包.这里第i个物品即第i个质数,需要得出刚好装满背包的方案总数.

    先来一波打表把可能用到的素数(1~1000)搜索出来:

    // 复制粘贴这个你就能成功WA了
    
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    bool isPrime(int n){
        for(int i = 2; i * i <= n; i++)
            if(!(n % i)) return false;
        return true;
    }
    
    int main(){
        for(int i = 2; i <= 1000; i++)
            if(isPrime(i))
                cout << i << ", ";
    
        return 0;
    }

    然后有递推式:dp[j] += dp[j - s[i]],s[i]为第i个素数(这里i从0开始,无关紧要).

    s[i]是递增数列,循环条件写s[i] <= n.

    初始化dp[0]为1,原因难以描述,但这样就处理好边界了.

    加上背包模板:

    // 复制粘贴这个你就又能成功WA了
    
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int s[200] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
                59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
                127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 
                191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 
                257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 
                331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 
                401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 
                467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 
                563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 
                631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 
                709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 
                797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 
                877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 
                967, 971, 977, 983, 991, 997};
    int n, dp[1010];
    
    int main(){
        cin >> n;
    
        dp[0] = 1;
        for(int i = 0; s[i] <= n; i++)
            for(int j = s[i]; j <= n; j++)
                dp[j] += dp[j - s[i]];
    
        cout << dp[n] << endl;
    
        return 0;
    }

    不开long long见了祖宗.

    // 复制粘贴这个就算开了long long你也会见祖宗
    
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int s[200] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
                59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
                127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 
                191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 
                257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 
                331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 
                401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 
                467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 
                563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 
                631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 
                709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 
                797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 
                877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 
                967, 971, 977, 983, 991, 997};
    long long n, dp[1010];
    
    int main(){
        cin >> n;
    
        dp[0] = 1;
        for(int i = 0; s[i] <= n; i++)
            for(int j = s[i]; j <= n; j++)
                dp[j] += dp[j - s[i]];
    
        cout << dp[n] << endl;
    
        return 0;
    }
    点击即可开long long

    该爆的还是会爆,因为n在998~1000的时候s[i] = 0,循环体没法跳出,然后就会裂开.

    加一个素数就好了,以后记得打表多打一点.

    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    int s[200] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53,
                59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 
                127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 
                191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 
                257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 
                331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 
                401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 
                467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 
                563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 
                631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 
                709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 
                797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 
                877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 
                967, 971, 977, 983, 991, 997, 1009};
    long long n, dp[1010];
    
    int main(){
        cin >> n;
    
        dp[0] = 1;
        for(int i = 0; s[i] <= n; i++)
            for(int j = s[i]; j <= n; j++)
                dp[j] += dp[j - s[i]];
    
        cout << dp[n] << endl;
    
        return 0;
    }
  • 相关阅读:
    PAT 甲级 1120 Friend Numbers (20 分)
    AcWing 894. 拆分-Nim游戏
    AcWing 891. Nim游戏
    AcWing 892. 台阶-Nim游戏
    AcWing 893. 集合-Nim游戏
    洛谷P1433 吃奶酪
    洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
    AcWing 125. 耍杂技的牛
    AcWing 104. 货仓选址 绝对值不等式
    AcWing 913. 排队打水 排序不等式贪心
  • 原文地址:https://www.cnblogs.com/Gaomez/p/14110327.html
Copyright © 2020-2023  润新知