• Codeforces Round #319 (Div. 2)B. Modulo Sum DP


                                                             B. Modulo Sum
                                                                                                     time limit per test
                                                                                                     2 seconds
                                                                                                     memory limit per test
                                                                                                     256 megabytes
     

    You are given a sequence of numbers a1, a2, ..., an, and a number m.

    Check if it is possible to choose a non-empty subsequence aij such that the sum of numbers in this subsequence is divisible by m.

    Input

    The first line contains two numbers, n and m (1 ≤ n ≤ 106, 2 ≤ m ≤ 103) — the size of the original sequence and the number such that sum should be divisible by it.

    The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

    Output

    In the single line print either "YES" (without the quotes) if there exists the sought subsequence, or "NO" (without the quotes), if such subsequence doesn't exist.

    Sample test(s)
    input
    3 5
    1 2 3
    output
    YES
    Note

    In the first sample test you can choose numbers 2 and 3, the sum of which is divisible by 5.

    In the second sample test the single non-empty subsequence of numbers is a single number 5. Number 5 is not divisible by 6, that is, the sought subsequence doesn't exist.

    In the third sample test you need to choose two numbers 3 on the ends.

    In the fourth sample test you can take the whole subsequence.

    题意:给你n,m,n个数,让你从中找出任意数的和mod M==0

    题解:背包dp

    //1085422276
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<map>
    #include<bitset>
    #include<set>
    #include<vector>
    using namespace std ;
    typedef long long ll;
    #define mem(a) memset(a,0,sizeof(a))
    #define meminf(a) memset(a,127,sizeof(a));
    #define memfy(a) memset(a,-1,sizeof(a))
    #define TS printf("111111
    ");
    #define FOR(i,a,b) for( int i=a;i<=b;i++)
    #define FORJ(i,a,b) for(int i=a;i>=b;i--)
    #define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
    #define maxn 1000005
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //****************************************
    int hashs[maxn],a[maxn*3];
    bool  dp[3][maxn];
    int main()
    {
    
        mem(hashs);
        int flag=0;
        int n=read(),m=read();
        FOR(i,1,n)
        {
            scanf("%d",&a[i]);
           a[i]=a[i]%m;
           if(a[i]==0)a[i]=m;
        }dp[0][0]=true;
        dp[1][0]=true;
        for(int i=1;i<=n;i++)
        {
            for(int j=m;j>=0;j--)
            {
                if(dp[1][j])
                {
                    if(j+a[i]==m){
                        puts("YES");
                        return 0;
                    }
                    dp[0][(j+a[i])%m]=true;
                }
            }
            memcpy(dp[1],dp[0],sizeof(dp[0]));
        }
        cout<<"NO"<<endl;
        return 0;
    }
    代码君
  • 相关阅读:
    无穷有界数列,必有收敛子列(待证)
    有界闭区间内的连续函数必然有界
    数学分析提纲目录
    有限覆盖定理
    函数极限的柯西收敛准则
    数列的柯西收敛准则证明-----华东师大构造数列证明法
    数列柯西收敛准则的子列收敛证明法(取自中科大数分教材)
    用有限覆盖证明闭区间上的连续函数,必然一致连续
    数据库-模糊查询-like关键字-练习
    数据库-基础查询-练习
  • 原文地址:https://www.cnblogs.com/zxhl/p/4801249.html
Copyright © 2020-2023  润新知