[Updating]ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined)

本文提供了三道编程题目的解决方案,包括模拟杀人者问题、夏洛克和他的女朋友问题以及莫莉的化学物质问题。通过结构化的代码实现,展示了如何处理字符串比较、素数判断和子序列求和等算法技巧。

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

Problem A-A Serial Killer

模拟

//Author: Lixiang
#include<stdio.h>
#include<string.h>
struct A_Serial_Killer{
	char now[2][11];
	int N;
	void init(){
		scanf("%s%s",now[0],now[1]);
		scanf("%d",&N);
	}
	bool cmp(char a[],char b[]){
		int l1=strlen(a),l2=strlen(b);
		if(l1!=l2)return 0;
		for(int i=0;i<l1;i++)
			if(a[i]!=b[i])return 0;
		return 1;
	}
	void work(){
		char death[11],replace[11];
		printf("%s %s\n",now[0],now[1]);
		for(int i=1;i<=N;i++){
			scanf("%s%s",death,replace);
			if(cmp(now[0],death))strcpy(now[0],replace);
			else strcpy(now[1],replace);
			printf("%s %s\n",now[0],now[1]);
		}
	}
}sol;
int main(){
	sol.init();
	sol.work();
	return 0;
}


Problem B-Sherlock and his girlfriend

合数为2,质数为1 即可

//Author: Lixiang
#include<stdio.h>
const int maxn=100002;
struct Sherlock_and_his_girlfriend{
	bool hash[maxn];
	int N;
	void init(){
		scanf("%d",&N);
		N++;
		for(int i=2;i<=N;i++){
			if(hash[i])continue;
			if(i>10000)continue;
			for(int j=i*i;j<=N;j+=i)
				hash[j]=1;
		}
	}
	void work(){
		if(N<4){
			puts("1");
			for(int i=2;i<=N;i++)
				printf("1 ");
		}
		else{
			puts("2");
			for(int i=2;i<=N;i++){
				if(hash[i])printf("2 ");
				else printf("1 ");
			}
		}
	}
}sol;
int main(){
	sol.init();
	sol.work();
	return 0;
}


Problem C-Molly's Chemicals

利用Map解决子序列之和为一定值的个数

//Author: Lixiang
#include<stdio.h>
#include<map>
using namespace std;
const int maxn=100001;
template <class T>
T abs(T a){
	return a>0?a:-a;
}
struct Mollys_chemical{
	long long data[maxn],ssum[maxn],K,lim,und;
	int N;
	void init(){
		long long sum=0,sum2=0;
		scanf("%d%I64d",&N,&K);
		for(int i=1;i<=N;i++){
			scanf("%I64d",&data[i]);
			ssum[i]=ssum[i-1]+data[i];
			sum+=data[i];
			lim=max(lim,sum);
			if(sum<0)sum=0;
			sum2+=data[i];
			und=min(sum2,und);
			if(sum2>0)sum2=0;
		}
	}
	long long count(long long S){
		long long sum=0,count=0;
		map <long long,long long> sumToSeqCnt;
		sumToSeqCnt[0]=1;
		for(int i=1;i<=N;++i){
			sumToSeqCnt[ssum[i]]++;
			count+=sumToSeqCnt[ssum[i]-S];
		}
		return count;
	}
	void work(){
		long long ans=0;
		if(K==1)ans=count(1);
		else{
			if(K==-1)ans=count(-1)+count(1);
			else{
				long long ss=max(-und,lim);
				for(long long S=1;abs(S)<=ss;S*=K)
					ans+=count(S);
			}
		}
		printf("%I64d\n",ans);
	}
}sol;
int main(){
	sol.init();
	sol.work();
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值