codeforces377B Modulo Sum(抽屉+dp)

博主反思自己在解决Codeforces 377B问题时,初看忽视了某些关键点。当n>=m时,利用抽屉原理直接得出结论。对于n<m的情况,通过学习他人题解,理解到需使用动态规划(DP)求解。博主分享了DP[i][j]的状态转移思路,并表示会进一步简化解题模板。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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个数里,和为j的数字有没有出现过,最后判断dp[n][c]是否等于1


这是自己直接套用的抽屉原理模板,应该可以改简单的,明天再看一下。

/* ***********************************************
Author        :Mosu
Created Time  :2015/10/10 18:30:45
File Name     :\Users\Mosu\Desktop\project\BC.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff

int neigb[1000009];
int dp[1005][1005];

long long S;
struct Remnant{
	int h,r;
}R[1000009];
bool cmp(const Remnant &a,const Remnant &b){
	if(a.r==b.r)
		return a.h<b.h;
	return a.r<b.r;
}
int main(){
	int c,n,k,h;
	while(scanf("%d%d",&n,&c)!=EOF){
		for(int i=0;i<n;i++)
			scanf("%d",&neigb[i]);
		if(n>c){
			k=-1;
			S=0;
			for(int i=0;i<n;i++){
				//scanf("%d",&neigb[i]);
				S+=neigb[i];
				R[i].r=S%c;
				R[i].h=i+1;
				if(k==-1&&R[i].r==0)
					k=i;
			}
			if(k==-1){
				sort(R,R+n,cmp);
				for(int i=0;i<n-1;i++){
					if(k==-1&&R[i].r==R[i+1].r){
						k=R[i].h;
						h=R[i+1].h;
					}
				}
				if(k==-1)
					printf("NO\n");
				else{
					printf("YES\n");
				}
			}
			else{
				printf("YES\n");			
			}
		}
	else{
		for(int i=0;i<=n;i++)
			dp[i][0]=1;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=c;j++)
				dp[i][j]=(dp[i-1][(j+neigb[i-1])%c]||dp[i-1][j]);
		if(dp[n][c])
			cout<<"YES"<<endl;
		else
			cout<<"NO"<<endl;
		}
	}
	return 0;
}



更新:

/* ***********************************************
Author        :Mosu
Created Time  :2015/10/10 18:30:45
File Name     :\Users\Mosu\Desktop\project\BC.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7ffffff

int dp[1009][1009];
int s[1000009];
int main()
{
	int n,m;
	while(scanf("%d%d",&n,&m)==2){
		int k=1;
		for(int i=0;i<n;i++){
			scanf("%d",&s[i]);
			if(s[i]%m==0)
				k=0;
		}
		if(n>m||k==0){
			printf("YES\n");
			continue;
		}
		sizeof(dp,0,sizeof(dp));
		for(int i=0;i<=n;i++)
			dp[i][0]=1;
		for(int i=1;i<=n;i++)
			for(int j=0;j<=m;j++)
				dp[i][j]=(dp[i-1][(j+s[i-1])%m]||dp[i-1][j]);
		if(dp[n][m])
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}



### 关于 Codeforces 1853B 的题解与实现 尽管当前未提供关于 Codeforces 1853B 的具体引用内容,但可以根据常见的竞赛编程问题模式以及相关算法知识来推测可能的解决方案。 #### 题目概述 通常情况下,Codeforces B 类题目涉及基础数据结构或简单算法的应用。假设该题目要求处理某种数组操作或者字符串匹配,则可以采用如下方法解决: #### 解决方案分析 如果题目涉及到数组查询或修改操作,一种常见的方式是利用前缀和技巧优化时间复杂度[^3]。例如,对于区间求和问题,可以通过预计算前缀和数组快速得到任意区间的总和。 以下是基于上述假设的一个 Python 实现示例: ```python def solve_1853B(): import sys input = sys.stdin.read data = input().split() n, q = map(int, data[0].split()) # 数组长度和询问次数 array = list(map(int, data[1].split())) # 初始数组 prefix_sum = [0] * (n + 1) for i in range(1, n + 1): prefix_sum[i] = prefix_sum[i - 1] + array[i - 1] results = [] for _ in range(q): l, r = map(int, data[2:].pop(0).split()) current_sum = prefix_sum[r] - prefix_sum[l - 1] results.append(current_sum % (10**9 + 7)) return results print(*solve_1853B(), sep='\n') ``` 此代码片段展示了如何通过构建 `prefix_sum` 来高效响应多次区间求和请求,并对结果取模 \(10^9+7\) 输出[^4]。 #### 进一步扩展思考 当面对更复杂的约束条件时,动态规划或其他高级技术可能会被引入到解答之中。然而,在没有确切了解本题细节之前,以上仅作为通用策略分享给用户参考。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值