现在的位置: 首页 > 综合 > 正文

Codeforces Round #275 (Div. 2)A,B,C

2019年02月15日 ⁄ 综合 ⁄ 共 5119字 ⁄ 字号 评论关闭
A. Counterexample
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Your friend has recently learned about coprime numbers. A pair of numbers
{a, b}
is called coprime if the maximum number that divides both
a and b is equal to one.

Your friend often comes up with different statements. He has recently supposed that if the pair
(a, b) is coprime and the pair
(b, c)
is coprime, then the pair
(a, c)
is coprime.

You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers
(a, b, c), for which the statement is false, and the numbers meet the condition
l ≤ a < b < c ≤ r.

More specifically, you need to find three numbers (a, b, c), such that
l ≤ a < b < c ≤ r, pairs
(a, b) and
(b, c)
are coprime, and pair (a, c) is not coprime.

Input

The single line contains two positive space-separated integers
l
, r (1 ≤ l ≤ r ≤ 1018;
r - l ≤ 50).

Output

Print three positive space-separated integers a,
b, c — three distinct numbers
(a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.

If the counterexample does not exist, print the single number -1.

Sample test(s)
Input
2 4
Output
2 3 4
Input
10 11
Output
-1
Input
900000000000000009 900000000000000029
Output
900000000000000009 900000000000000010 900000000000000021
Note

In the first sample pair (2, 4) is not coprime and pairs
(2, 3) and (3, 4) are.

In the second sample you cannot form a group of three distinct integers, so the answer is -1.

In the third sample it is easy to see that numbers 900000000000000009 and
900000000000000021 are divisible by three.

注意到r - l <= 50,所以直接暴力

#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  

using namespace std;

__int64 gcd(__int64 a, __int64 b)
{
	if (b == 0)
	{
		return a;
	}
	return gcd(b, a % b);
}

int main()
{
	__int64 l, r;
	while (~scanf("%I64d%I64d", &l, &r))
	{
		int a, b, c;
		bool flag = false;
		for (__int64 i = l; i <= r; i++)
		{
			for (__int64 j = i + 1; j <=r ;j ++)
			{
				for (__int64 k = j + 1; k <= r; k++)
				{
					if(gcd(i,j ) == 1 && gcd(j, k) == 1 && gcd(i, k)!= 1)
					{
						printf("%I64d %I64d %I64d\n", i, j, k);
						flag = true;
						break;
					}
				}
				if(flag)
				{
					break;
				}
			}
			if (flag)
			{
				break;
			}
		}
		if(!flag)
		{
			printf("-1\n");
		}
	}
	return 0;
} 

B. Friends and Presents
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You have two friends. You want to present each of them several positive integers. You want to present
cnt1 numbers to the first friend and
cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.

In addition, the first friend does not like the numbers that are divisible without remainder by prime number
x. The second one does not like the numbers that are divisible without remainder by prime number
y. Of course, you're not going to present your friends numbers they don't like.

Your task is to find such minimum number v, that you can form presents using numbers from a set
1, 2, ..., v. Of course you may choose not to present some numbers at all.

A positive integer number greater than 1 is called
prime
if it has no positive divisors other than 1 and itself.

Input

The only line contains four positive integers cnt1,
cnt2,
x
, y (1 ≤ cnt1, cnt2 < 109;
cnt1 + cnt2 ≤ 109;
2 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbers
x, y are prime.

Output

Print a single integer — the answer to the problem.

Sample test(s)
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
Note

In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers
{2} to the second friend. Note that if you give set
{1, 3, 5} to the first friend, then we cannot give any of the numbers
1, 3,
5
to the second friend.

In the second sample you give the set of numbers {3} to the first friend, and the set of numbers
{1, 2, 4} to the second friend. Thus, the answer to the problem is
4.

B题比C题难,先搞了这题,比赛中过了但是二测FST了

贴一下我的错误代码,请指教

#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  

using namespace std;

int main()
{
	int cnt1, cnt2, x, y;
	while (~scanf("%d%d%d%d", &cnt1, &cnt2, &x, &y))
	{
		int l = 1;
		int r = 1000000000;
		int mid;
		int s1, s2, s3;
		int ans;
		int z = x * y;
		while (l <= r)
		{
			mid = (l + r) >> 1;
			s1 = mid / x;
			s2 = mid / y;
			s3 = mid / z;
			s1 -= s3;
			s2 -= s3;
			int t1 = cnt1;
			int t2 = cnt2;
			int t = mid;
			mid -= s3;
			if (t1 > s2)
			{
				t1 -= s2;
				mid -= s2;
			}
			else
			{
				mid -= s2;
				t1 = 0;
			}
			if (t2 > s1)
			{
				t2 -= s1;
				mid -= s1;
			}
			else
			{
				mid -= s1;
				t2 = 0;
			}
			if(mid >= t1 + t2)
			{
				r = t - 1;
				ans = t;
			}
			else
			{
				l = t + 1;
			}
		}
		printf("%d\n", ans);
	}
	return 0;
} 

C. Diverse Permutation
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Permutation p is an ordered set of integers
p1,   p2,   ...,   pn, consisting of
n distinct positive integers not larger than
n. We'll denote as
n
the length of permutation p1,   p2,   ...,   pn.

Your task is to find such permutation p of length
n, that the group of numbers
|p1 - p2|, |p2 - p3|, ..., |pn - 1 - pn|

has exactly k distinct elements.

Input

The single line of the input contains two space-separated positive integers
n
, k (1 ≤ k < n ≤ 105).

Output

Print n integers forming the permutation. If there are multiple answers, print any of them.

Sample test(s)
Input
3 2
Output
1 3 2
Input
3 1
Output
1 2 3
Input
5 2
Output
1 3 2 4 5
Note

By |x| we denote the absolute value of number
x.

大水题,要有k个不同的绝对值之差,就要至少k+1个数,所以我们值动用后面的k+1个数,前面的照样输出,然后只要极端配对就行  比如 23456,配成 26354

#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  

using namespace std;

int a[100010];

int main()
{
	int n, k;
	while (~scanf("%d%d", &n, &k))
	{
		int i, j;
		for (i = 1; i <= n - k - 1; i++)
		{
			if(i > 1)
				printf(" %d", i);
			else
			{
				printf("%d", i);
			}
		}
		
		if(n - k <= 0)
		{
			printf("\n");
			continue;
		}
		for (i = n - k, j = n; i < j; i++, j--)
		{
			if(i == 1)
				printf("%d %d", i, j);
			else
			{
				printf(" %d %d", i, j);
			}
		}
		if(( k + 1 ) % 2)
		{
			printf(" %d", i);
		}
		printf("\n");
	}
	return 0;
}

抱歉!评论已关闭.