zoukankan      html  css  js  c++  java
  • HDU 4901 多校4 经典计数DP

    RT

    最近不想写博客,累积了一周多的题目,今天趁着周日放假,全部补上吧

    dp[i][j]表示前i个数,操作后的值为j的总个数

    注意取或不取,有种完全背包的意味。因为数字小于1024,所以异或的结果也绝对不会超过1024,在循环第二维的时候到1024就行了,不要循环多了,反而会错,循环多了 异或值会超,结果爆了数组,产生了奇怪的结果,一开始我就这么莫名其妙的错,还不知道是为什么,就是爆了数组 他也不会提醒你的

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #define LL __int64
    using namespace std;
    const LL M=1000000000+7;
    const int N=1050;
    const int maxn=1010;
    LL dp1[maxn][N];
    LL dp2[maxn][N];
    LL s1[maxn][N],s2[maxn][N];
    int A[maxn];
    int main()
    {
        int t,n;
        scanf("%d",&t);
        while (t--)
        {
            scanf("%d",&n);
            for (int i=1;i<=n;i++){
                scanf("%d",&A[i]);
            }
            for (int i=0;i<=n+1;i++){
                for (int j=0;j<=N;j++){
                    dp1[i][j]=dp2[i][j]=0;
                    s1[i][j]=s2[i][j]=0;
                }
            }
            dp1[0][0]=1;
            for (int i=1;i<n;i++){
                //dp1[i][A[i]]=1;
                for (int j=0;j<=1024;j++){
                    dp1[i][j]+=dp1[i-1][j];
                    dp1[i][j^A[i]]+=dp1[i-1][j];
                    if (dp1[i][j]>=M) dp1[i][j]%=M;
                    if (dp1[i][j^A[i]]>=M) dp1[i][j^A[i]]%=M;
                }
                for (int j=0;j<=1024;j++){
                   s1[i][j^A[i]]=dp1[i-1][j];
                   s1[i][j^A[i]]%=M;
                }
            }
            LL ans=0;
            for (int i=n;i>1;i--){
                dp2[i][A[i]]=1;
                for (int j=1024;j>=0;j--){
                    dp2[i][j&A[i]]+=dp2[i+1][j];
                    dp2[i][j]+=dp2[i+1][j];
                    if (dp2[i][j&A[i]]>=M) dp2[i][j&A[i]]%=M;
                    if (dp2[i][j]>=M) dp2[i][j]%=M;
                }
            }
            for (int i=1;i<n;i++){
                for (int j=0;j<=1024;j++){
                    ans+=s1[i][j]*dp2[i+1][j];
                    ans%=M;
                }
            }
            printf("%I64d
    ",ans%M);
        }
        return 0;
    }
  • 相关阅读:
    ACM 2的N次方
    文件默认打开方式 转
    java 的 一点记录
    zhuan 漫谈C语言及如何学习C语言
    eclipse
    code::blocks
    心态决定命运_no excuses, suck it up, obey your teacher
    uml_2_application and viso application
    paint conflict with lingoes
    stm learning record
  • 原文地址:https://www.cnblogs.com/kkrisen/p/3902750.html
Copyright © 2011-2022 走看看