• TLE


    TLE - Time Limit Exceeded

    no tags 

    Given integers N (1 ≤ N ≤ 50) and M (1 ≤ M ≤ 15), compute the number of sequences a1, ..., aN such that:

    • 0 ≤ ai < 2M
    • ai is not divisible by ci (0 < ci ≤ 2M)
    • ai & ai+1 = 0 (that is, ai and ai+1 have no common bits in their binary representation)

    Input

    The first line contains the number of test cases, T (1 ≤ T ≤ 10). For each test case, the first line contains the integers N and M, and the second line contains the integers c1, ..., cN.

    Output

    For each test case, output a single integer: the number of sequences described above, modulo 1,000,000,000.

    Example

    Input:
    1
    2 2
    3 2
    
    Output:
    1
    

    The only possible sequence is 2, 1.

    分析:考虑相邻a[i]&a[i+1]=0;

       初始化状态转移为dp[i][j]=dp[i-1][j^((1<<m)-1)];

       dp[i][j]表示第i步为j的方案数;

       其次,若j&k=j,则dp[i][j]也应包含dp[i][k],这个可以推回去与一下;

       dp[i][j]=Σdp[i][k],j&k=j,这个即为高维前缀和;

       剩下不被整除特判一下即可;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000000
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    const int N=2e2+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,a[51],dp[51][1<<15];
    int main()
    {
        int i,j;
        scanf("%d",&t);
        while(t--)
        {
            memset(dp,0,sizeof(dp));
            scanf("%d%d",&n,&m);
            rep(i,1,n)scanf("%d",&a[i]);
            rep(i,0,(1<<m)-1)if(i%a[1]!=0)dp[1][i]++;
            rep(i,2,n)
            {
                rep(j,0,(1<<m)-1)dp[i][j]=dp[i-1][j^((1<<m)-1)];
                rep(j,0,m-1)
                {
                    rep(k,0,(1<<m)-1)
                    {
                        if((~k)&(1<<j))(dp[i][k]+=dp[i][k^(1<<j)])%=mod;
                    }
                }
                for(j=0;j<(1<<m);j+=a[i])dp[i][j]=0;
            }
            ll ret=0;
            rep(i,0,(1<<m)-1)(ret+=dp[n][i])%=mod;
            printf("%lld
    ",ret);
        }
        return 0;
    }
  • 相关阅读:
    poj 2425 AChessGame(博弈)
    poj2975 Nim 胜利的方案数
    hdu 5724 SG+状态压缩
    hdu 5274 Dylans loves tree(LCA + 线段树)
    hdu 5266 pog loves szh III(lca + 线段树)
    hdu 4031 attack 线段树区间更新
    51 nod 1188 最大公约数之和 V2
    51nod 1040 最大公约数之和(欧拉函数)
    51nod 1035:最长的循环节
    Nim游戏(组合游戏Combinatorial Games)
  • 原文地址:https://www.cnblogs.com/dyzll/p/6536813.html
Copyright © 2020-2023  润新知