• codeforces #319 B


    B - Modulo Sum
    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    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 ≤ 1062 ≤ 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 Input

    Input
    3 5
    1 2 3
    Output
    YES
    Input
    1 6
    5
    Output
    NO
    Input
    4 6
    3 1 1 3
    Output
    YES
    Input
    6 6
    5 5 5 5 5 5
    Output
    YES

    Hint

    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.

    背包还是理解的不够透彻..

    因为每次都是用那个一维形式的.

    这道题的做法类似01背包.

    此外还可以有一个优化...

    当n>m的时候...根绝抽屉原理..一定为yes..

    复杂度可以从o(nm)优到 o(m^2)

     1 /*************************************************************************
     2     > File Name: code/#319/BB.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年09月15日 星期二 21时31分12秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #include<cctype>
    21 #define y1 hust111qqz
    22 #define yn hez111qqz
    23 #define j1 cute111qqz
    24 #define ms(a,x) memset(a,x,sizeof(a))
    25 #define lr dying111qqz
    26 using namespace std;
    27 #define For(i, n) for (int i=0;i<int(n);++i)  
    28 typedef long long LL;
    29 typedef double DB;
    30 const int inf = 0x3f3f3f3f;
    31 const int N=1E3+7;
    32 int n,m;
    33 int a[N];
    34 int dp[N][5];
    35 int main()
    36 {
    37   #ifndef  ONLINE_JUDGE 
    38 
    39   #endif
    40     cin>>n>>m;
    41     if (n>m)
    42     {
    43     puts("YES");
    44     return 0;
    45     }
    46     for (int i = 1 ; i <= n ; i++)
    47     {
    48     int x;
    49     scanf("%d",&x);
    50     a[i] = x % m;
    51     }
    52     int x = 0 ;
    53     for ( int i = 1 ; i <= n ; i++)
    54     {
    55     
    56     dp[a[i]][1-x] = 1;
    57     for ( int j = 1 ; j < m ; j++)
    58     {
    59         if(dp[j][x])
    60         {
    61         
    62         dp[(j+a[i])%m][1-x] = 1;
    63         dp[j][1-x] = 1;
    64         }
    65     }
    66     x = 1-x;
    67     if (dp[0][x])
    68     {
    69         puts("YES");
    70         return 0;
    71     }
    72     
    73     }
    74     puts("NO");
    75     return 0;
    76 
    77 
    78 
    79   
    80   
    81  #ifndef ONLINE_JUDGE  
    82   fclose(stdin);
    83   #endif
    84     return 0;
    85 }
    View Code
  • 相关阅读:
    最新恶意IE跳窗广告IS_0518(_IS_ISC.DLL)的删除方法
    禁止UDP端口引起DNS错误导致邮局无法外发的故障
    微软某个补丁引起DCOMCNFG无法对后安装的COM进行权限设置的处理
    J2ME中使用calendar类获得当前时间
    今天实在忍不住,把极速星空的密码给破了
    准备开发一个PSP专用的网站http://www.mypsp.com.cn
    网络工程师30个经典的电脑使用技巧
    XBOX更换160G大硬盘的笑话
    PPC软件字体太小的调整
    解决IISASP调用XmlHTTP出现msxml3.dll (0x80070005) 拒绝访问的错误
  • 原文地址:https://www.cnblogs.com/111qqz/p/4812759.html
Copyright © 2020-2023  润新知