Codeforces Round #697 (Div. 3) 寒假训练题解

A. Odd Divisor

You are given an integer n. Check if n has an odd divisor, greater than one (does there exist such a number x (x>1) that n is divisible by x and x is odd).

For example, if n=6, then there is x=3. If n=4, then such a number does not exist.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

Each test case contains one integer n (2≤n≤1014).

Please note, that the input for some test cases won’t fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language.

Output
For each test case, output on a separate line:

“YES” if n has an odd divisor, greater than one;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

Example

input
6
2
3
4
5
998244353
1099511627776
output
NO
YES
NO
YES
YES
NO

解题思路: 如果输入的数是奇数,则直接输出YES,若是偶数,则再对其不断进行除2操作,直到除出一个奇数为止,输出YES,如果一直都是偶数,则输出NO。

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<set>
typedef long long ll;
const int N=100005;
using namespace std;
ll n;
int main(){
	int t;
	cin>>t;
	while(t--){
		int flag=0;
		cin>>n;
		if(n%2!=0){
			cout<<"YES"<<endl;
			continue;
		} 
		while(n){
			n/=2;
			if(n%2!=0&&n>1){
				cout<<"YES"<<endl;
				flag=1;
				break;
			}
		}
		if(flag==0) cout<<"NO"<<endl;
	}
	return 0;
}

B. New Year’s Number

Polycarp remembered the 2020-th year, and he is happy with the arrival of the new 2021-th year. To remember such a wonderful moment, Polycarp wants to represent the number n as the sum of a certain number of 2020 and a certain number of 2021.

For example, if:

n=4041, then the number n can be represented as the sum 2020+2021;
n=4042, then the number n can be represented as the sum 2021+2021;
n=8081, then the number n can be represented as the sum 2020+2020+2020+2021;
n=8079, then the number n cannot be represented as the sum of the numbers 2020 and 2021.
Help Polycarp to find out whether the number n can be represented as the sum of a certain number of numbers 2020 and a certain number of numbers 2021.

Input

The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

Each test case contains one integer n (1≤n≤106) — the number that Polycarp wants to represent as the sum of the numbers 2020 and 2021.

Output
For each test case, output on a separate line:

“YES” if the number n is representable as the sum of a certain number of 2020 and a certain number of 2021;
“NO” otherwise.
You can output “YES” and “NO” in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).

Example

input
5
1
4041
4042
8081
8079
output
NO
YES
YES
YES
NO

解题思路:将用输入数%2020,/2020就是2020的个数,余下的数就是2021的个数,比较大小。

#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<set>
typedef long long ll;
const int N=100005;
using namespace std;
ll n;
int main(){
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		int cnt = n%2020,ans = n/2020;
		if(cnt<=ans) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
	return 0;
}

C. Ball in Berland

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
At the school where Vasya is studying, preparations are underway for the graduation ceremony. One of the planned performances is a ball, which will be attended by pairs of boys and girls.

Each class must present two couples to the ball. In Vasya’s class, a boys and b girls wish to participate. But not all boys and not all girls are ready to dance in pairs.

Formally, you know k possible one-boy-one-girl pairs. You need to choose two of these pairs so that no person is in more than one pair.

For example, if a=3, b=4, k=4 and the couples (1,2), (1,3), (2,2), (3,4) are ready to dance together (in each pair, the boy’s number comes first, then the girl’s number), then the following combinations of two pairs are possible (not all possible options are listed below):

(1,3) and (2,2);
(3,4) and (1,3);
But the following combinations are not possible:

(1,3) and (1,2) — the first boy enters two pairs;
(1,2) and (2,2) — the second girl enters two pairs;
Find the number of ways to select two pairs that match the condition above. Two ways are considered different if they consist of different pairs.

Input
The first line contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The first line of each test case contains three integers a, b and k (1≤a,b,k≤2⋅105) — the number of boys and girls in the class and the number of couples ready to dance together.

The second line of each test case contains k integers a1,a2,…ak. (1≤ai≤a), where ai is the number of the boy in the pair with the number i.

The third line of each test case contains k integers b1,b2,…bk. (1≤bi≤b), where bi is the number of the girl in the pair with the number i.

It is guaranteed that the sums of a, b, and k over all test cases do not exceed 2⋅105.

It is guaranteed that each pair is specified at most once in one test case.

Output
For each test case, on a separate line print one integer — the number of ways to choose two pairs that match the condition above.

Example

input
3
3 4 4
1 1 2 3
2 3 2 4
1 1 1
1
1
2 2 4
1 1 2 2
1 2 1 2
output
4
0
2

解题思路: 就是输入两行数,上下一一对应为一组,每两组为一对,每队的相同位置数字不能相同,本质就是组合数,类似板子题。

#include<iostream>
#include<cmath>
#include<algorithm>
#include<string.h>
using namespace std;
struct stu {
	int a;
	int b;
};
bool cmp(stu a,stu b){
	return a.a<b.a;
}
int main(){
    int t;
    cin>>t;
    while(t--){
		int a,b,k;
		cin>>a>>b>>k;
		int x[a+1],y[b+1];
		memset(x,0,sizeof x),memset(y,0,sizeof y);
		stu s[k];
		for(int i=0;i<k;i++) cin>>s[i].a,x[s[i].a]++;
		for(int i=0;i<k;i++) cin>>s[i].b,y[s[i].b]++;
		sort(s,s+k,cmp);
		long long ans=0;
		for(int i=0;i<k;i++){
			x[s[i].a]--;
			y[s[i].b]--;
			ans+=k-x[s[i].a]-y[s[i].b]-i-1;
		}
		cout<<ans<<endl;
	}
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值