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比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值