Codeforces Round #609 (Div. 2)

Codeforces Round #609 (Div. 2)

A

Equation
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Let’s call a positive integer composite if it has at least one divisor other than 1 and itself. For example:

the following numbers are composite: 1024, 4, 6, 9;
the following numbers are not composite: 13, 1, 2, 3, 37.
You are given a positive integer n. Find two composite integers a,b such that a−b=n.

It can be proven that solution always exists.

Input
The input contains one integer n (1≤n≤107): the given integer.

Output
Print two composite integers a,b (2≤a,b≤109,a−b=n).

It can be proven, that solution always exists.

If there are several possible solutions, you can print any.

Examples
input
1
output
9 8
input
512
output
4608 4096

最朴素的无脑做法,就是判断是不是素数,还有i-n是不是素数,一个个找

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
int prime(int a)
	{
	if(a<=1)
	    return 0;
	for(int i=2;i<=sqrt(a);i++)
		if(i!=a&&a%i==0)
			return 0;
	return 1;
	}
void hzh()
{
	ll n;
	scanf("%lld",&n);
	for(ll i=n+2;;i++)
	{
		if(prime(i)==0&&prime(i-n)==0)
		{
			printf("%lld %lld",i,i-n);
			return;
		}
	}
}
int main()
{
	/*
	int t;
	scanf("%d",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}

B


Modulo Equality
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a positive integer m and two integer sequence: a=[a1,a2,…,an] and b=[b1,b2,…,bn]. Both of these sequence have a length n.

Permutation is a sequence of n different positive integers from 1 to n. For example, these sequences are permutations: [1], [1,2], [2,1], [6,7,3,4,1,2,5]. These are not: [0], [1,1], [2,3].

You need to find the non-negative integer x, and increase all elements of ai by x, modulo m (i.e. you want to change ai to (ai+x)modm), so it would be possible to rearrange elements of a to make it equal b, among them you need to find the smallest possible x.

In other words, you need to find the smallest non-negative integer x, for which it is possible to find some permutation p=[p1,p2,…,pn], such that for all 1≤i≤n, (ai+x)modm=bpi, where ymodm — remainder of division of y by m.

For example, if m=3, a=[0,0,2,1],b=[2,0,1,1], you can choose x=1, and a will be equal to [1,1,0,2] and you can rearrange it to make it equal [2,0,1,1], which is equal to b.

Input
The first line contains two integers n,m (1≤n≤2000,1≤m≤109): number of elemens in arrays and m.

The second line contains n integers a1,a2,…,an (0≤ai<m).

The third line contains n integers b1,b2,…,bn (0≤bi<m).

It is guaranteed that there exists some non-negative integer x, such that it would be possible to find some permutation p1,p2,…,pn such that (ai+x)modm=bpi.

Output
Print one integer, the smallest non-negative integer x, such that it would be possible to find some permutation p1,p2,…,pn such that (ai+x)modm=bpi for all 1≤i≤n.

Examples
input
4 3
0 0 2 1
2 0 1 1
output
1
input
3 2
0 0 0
1 1 1
output
1
input
5 10
0 0 0 1 2
2 1 0 0 0
output
0

这题的主要思路就是暴力,不过不是暴力x,而是暴力a[0]对应b中的哪个元素,时间复杂度o(n)
但这题有一个巨坑的点,就是数组大小开2001过不去!!!开到2003就过去了,醉了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
ll a[2003],b[2003],c[2003];
void hzh()
{
	ll n,m,i,j,x,minx;
	scanf("%lld%lld",&n,&m);
	for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
	for(i=0;i<n;i++)
		scanf("%lld",&b[i]);
	sort(b,b+n);
	minx=m;
	for(i=0;i<n;i++)
	{
		x=(a[0]-b[i]+m)%m;
		for(j=0;j<n;j++)
			c[j]=(a[j]+x)%m;
		sort(c,c+n);
		for(j=0;j<n;j++)
		{
			if(c[j]!=b[j])
				break;
			if(j==n-1)
					minx=min(x,minx);
		}
	}
	printf("%lld",minx);
	return;
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}

C

*Long Beautiful Integer
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an integer x of n digits a1,a2,…,an, which make up its decimal notation in order from left to right.

Also, you are given a positive integer k<n.

Let’s call integer b1,b2,…,bm beautiful if bi=bi+k for each i, such that 1≤i≤m−k.

You need to find the smallest beautiful integer y, such that y≥x.

Input
The first line of input contains two integers n,k (2≤n≤200000,1≤k<n): the number of digits in x and k.

The next line of input contains n digits a1,a2,…,an (a1≠0, 0≤ai≤9): digits of x.

Output
In the first line print one integer m: the number of digits in y.

In the next line print m digits b1,b2,…,bm (b1≠0, 0≤bi≤9): digits of y.

Examples
input
3 2
353
output
3
353
input
4 2
1234
output
4
1313

又是一道巨坑题,坑点特别特别多
我的思路是:确定前m个元素,然后循环打印,打完为止
(我至今没想明白,为啥题目要求我们打一遍n,完全没有意义)
从第j=m+1个元素开始,去和它对应的j%m个元素对比大小。这里特别注意,因为我是从1开始读的,所以j%m的值可能为0,而我实际上想对比的是第m个元素,因此我用了个特判。
如果这个元素比他对应的元素大,那么单纯循环打印m个元素的值会比原值小,所以让第m个元素+1,那么循环打印的数一定比原数要大,但如果第m个元素为9,那么就要考虑进位。
如果这个元素比他对应的元素小,那么就直接循环打印。
如果这个元素比他对应的元素一样大,那么就判断下一个。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
char a[200005];
void solve()
{
	int n,m,i;
	scanf("%d%d",&n,&m);
	getchar();
	for(int i=1;i<=n;i++)
		scanf("%c",&a[i]);
	for(int i=m+1;i<=n;i++)
	{
		if(a[i%m==0?m:i%m]>a[i])
			break;
		if(a[i%m==0?m:i%m]<a[i])
		{
			int t=m;
			while(a[t]=='9')
			{
				a[t]='0';
				--t;
			}
			a[t]++;
			break;
		}
	}
	printf("%d\n",n);
	int k=0;
	while(k<n)
	{
	for(i=1;i<=m&&k<n;i++,k++)
		printf("%c",a[i]);
	}
}
int main()
{
    solve();
    return 0;
}

D


D. Domino for Young
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a Young diagram.

Given diagram is a histogram with n columns of lengths a1,a2,…,an (a1≥a2≥…≥an≥1).

Young diagram for a=[3,2,2,2,1].
Your goal is to find the largest number of non-overlapping dominos that you can draw inside of this histogram, a domino is a 1×2 or 2×1 rectangle.

Input
The first line of input contain one integer n (1≤n≤300000): the number of columns in the given histogram.

The next line of input contains n integers a1,a2,…,an (1≤ai≤300000,ai≥ai+1): the lengths of columns.

Output
Output one integer: the largest number of non-overlapping dominos that you can draw inside of the given Young diagram.

Example
inputCopy
5
3 2 2 2 1
outputCopy
4

如果an是偶数,直接就可以放an/2块;麻烦的是如果an是奇数的情况。
如果两个长度为奇数的列靠在一起,就可以在an/2的基础上再多一块,
靠在一起,距离为0,可以推得距离为偶数的时候,结论仍成立
也就是说:
如果n为奇数的长度为奇数的列有x个,如果n为偶数的长度为奇数的列有y个,那么可以多min(x,y)块。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; 
void hzh()
{
	ll n,a,sum=0,flag1=0,flag2=0;
    scanf("%lld",&n);
    for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a);
		sum+=a/2;
		if(a%2==1)
		{
			if(i%2==0)
				flag1++;
			else
				flag2++;
		}
	} 
	printf("%lld",sum+min(flag1,flag2));
}
int main()
{
	/*
	ll t;
	scanf("%lld",&t);
	while(t--)
	*/
    	hzh();
    return 0;
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值