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 51 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\n");
#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;
}

代码君

转载于:https://www.cnblogs.com/zxhl/p/4801249.html

Codeforces Round #319 (Div. 2)B. Modulo Sum DP相关推荐

  1. Codeforces Round #743 (Div. 2) E. Paint 区间dp + 暴力

    传送门 文章目录 题意: 思路: 题意: 给你一个有nnn个像素的图像,每个像素都有一个颜色aia_iai​,保证每种颜色的图像不会超过202020个.你现在每次可以选择一个颜色,并选择一段连续的像素 ...

  2. Codeforces Round #709 (Div. 1) C. Skyline Photo dp + 单调栈优化

    传送门 文章目录 题意: 思路: 题意: 思路: 首先一个非常明显的dpdpdp式子就是f[i]=max(f[j]+val(j+1,i))f[i]=max(f[j]+val(j+1,i))f[i]=m ...

  3. Codeforces Round #627 (Div. 3) E. Sleeping Schedule dp

    传送门 文章目录 题意: 思路: 题意: 给你一天hhh小时,初始时间是000,每天可以使时间+ai+a_i+ai​或者+ai−1+a_i-1+ai​−1,问最多可以让多少天的时间在[l,r][l,r ...

  4. Codeforces Round #717 (Div. 2) D(倍增dp)

    Codeforces Round #717 (Div. 2) D 题意:n个数 q个询问,每一个询问有l和r,问你l到r这段区间中最少能分成几段,每一段中的数都是互质的. 思路:首先预处理出每一个点向 ...

  5. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  6. Codeforces Codeforces Round #319 (Div. 2) A. Multiplication Table 水题

    A. Multiplication Table Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/57 ...

  7. Tinkoff Internship Warmup Round 2018 and Codeforces Round #475 (Div. 2) C.Alternating Sum(等比数列求和)

    题目链接;点击打开链接 题目大意:给出一个数列s,其中的元素为s1,s2,s3......sn.元素要么是1(用'+'表示),要么是-1(用'-'表示) 这个数列以k个数为一个周期,且n能被k整除. ...

  8. Codeforces Round #121 (Div. 1) A. Dynasty Puzzles DP

    链接: http://codeforces.com/contest/191/problem/A 题意: 给出n个字符串,两个字符串如果前一个的尾与后一个的首相同,那么可以相连,最后得到的字符串要满足首 ...

  9. 思维dp ---- Codeforces Round #711 (Div. 2) - C. Planar Reflections[dp/记忆化搜索]

    题目链接 题目大意: 就是给你n个平面和一个寿命为k的衰变粒子.开始粒子从左向右飞行,粒子每经过一个平面就会产生一个副本粒子,这个副本粒子比原粒子的寿命少1,即为k-1,并且飞行方向是原粒子的反方向. ...

最新文章

  1. MySQL 性能优化之高阶神技
  2. 5分钟 NLP系列—— 11 个词嵌入模型总结
  3. pycharm中import呈现灰色原因
  4. WIN7 IE8的桌面图标解决了(简单有效)
  5. mysql查询优化之一:mysql查询优化常用方式
  6. 树莓派 蓝牙音响_你应该拥有一个树莓派
  7. rocketmq一个topic多个group_SpringBoot和RocketMQ的简单实例
  8. SQL数据库层面操作(DDL)
  9. 分布式时序数据库InfluxDB
  10. 【转】Java程序员最常用的8个Java日志框架
  11. 活久见: maven pom 竟然都会崩溃!
  12. DASCTF Sept X 浙江工业大学秋季挑战赛wp
  13. 95后程序员辞职大厂卖柚子,3个月爆卖3200万,走完父亲30年的路
  14. 【100 种语言速成】第 1 节:Python
  15. oracle ebcdic 转换,使用sqlldr导入EBCDIC格式数据并新增Oracle字符集
  16. 【python脚本】linux中编写运行python脚本
  17. 【Learning】虚树题目汇总
  18. 网络游戏服务器编程01 大纲
  19. 计算机毕业论文会计,会计毕业论文6000字
  20. 一代宗师杨禹廷与吴氏太极拳

热门文章

  1. python strip_Python strip()方法
  2. java小数换成字符实现加法_第一、二次笔记总结
  3. 接口规范 12. 自动删除相关接口
  4. python3内置函数_python3--内置函数
  5. FPGA学习笔记---Modelsim使用技巧总结
  6. Redis入门,Redis的安装
  7. 带时限作业排序(贪心)
  8. 最爱的城市(dfs)
  9. 郑州轻工业学院OJ-杨辉三角
  10. matplotlib -work4