zoukankan      html  css  js  c++  java
  • CF div2 319 B

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

    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
    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
    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>=m时肯定可以,那么当n<m时可以利用dp来做

    dp[i][j]表示用前i-1个数的sum值得到j(这里j=(sum%m)),显然状态转移方程为 dp[i+1][(j+a[i])%m] = 1,或者 dp[i+1][j];

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <queue>
     5 #include <vector>
     6 #include <stack>
     7 
     8 using namespace std;
     9 
    10 const int M = 10005;
    11 const int maxn = 1100000;
    12 typedef long long ll;
    13 
    14 vector<int>G[maxn];
    15 queue<int>Q;
    16 stack<int>st;
    17 
    18 priority_queue<int>q;
    19 
    20 
    21 int l[maxn],r[maxn];
    22 int a[maxn];
    23 int dp[1005][1005];
    24 int main()
    25 {
    26 
    27     int n;
    28     int ans = 0;
    29     int m;
    30     scanf("%d%d",&n,&m);
    31     for(int i=1;i<=n;i++){
    32         scanf("%d",&a[i]);
    33 
    34     }
    35 
    36     if(n>=m){
    37         printf("YES
    ");
    38     }
    39     else {
    40 
    41         for(int i=1;i<=n;i++){
    42                 dp[i][a[i]%m] = 1;
    43             for(int j=0;j<m;j++){
    44                 if(dp[i-1][j]){
    45                     dp[i][(j+a[i])%m] = 1;
    46                     dp[i][j] = 1;
    47                 }
    48             }
    49         }
    50         if(dp[n][0])printf("YES
    ");
    51         else printf("NO
    ");
    52     }
    53 
    54 
    55 
    56     return 0;
    57 }
    View Code
  • 相关阅读:
    【工作总结】工作三年半的不归路,希望新人借鉴
    【OpenWRT】【RT5350】【三】MakeFile文件编写规则和OpenWRT驱动开发步骤
    【OpenWRT】【RT5350】【二】烧写OpenWrt到RT5350开发板
    【OpenWRT】【RT5350】【一】OpenWrt开发环境搭建
    2013总结
    [原创]cocos2dx加载网络图片&异步加载图片
    json 对c++类的序列化(自动生成代码)
    [奇思幻想] 开发过程中的一些设想记录中(持续更新....)
    GNU Makefile编写
    c语言到汇编的学习
  • 原文地址:https://www.cnblogs.com/lmlyzxiao/p/4814785.html
Copyright © 2011-2022 走看看