BNUZ 日常训练2021.1.29

A - Odd Divisor

Problem Description
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

思路要点
因为奇数其本身就是它的一个奇因数,所以我们只用讨论偶数

AC题解

#include <stdio.h>
int main()
{
	long long t, n;
	scanf("%lld", &t);
	while(t --)
	{
		scanf("%lld", &n);
		while(n%2 == 0) n /= 2;//不断除2直到为奇数为止
		
		if(n == 1) 
			printf("NO\n");//若为1则该数没有除1以外的其它奇因数
		else
			printf("YES\n");//若不为1则n为该数的一个不为1的奇因数
    }
    return 0;
}

B - New Year’s Number

Problem Description
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和2021组合而成则其中至少有一个2020或2021

AC题解

#include <stdio.h>
int main()
{
	int t;
	scanf("%d", &t);
	while(t --)
	{
		int n, flag = 0;//flag记录是否已经输出YES
		scanf("%d", &n);
		while(n >= 0)
		{
			if (n%2020 == 0 || n%2021 == 0)//判断是否能被2020或2021整除
			{
				printf("YES\n");
				flag = 1;
				break;
			}
			n -= 2020;//如果不能则至少有一个2020,也可为2021
			if(flag) break;
		}
		if(!flag) printf("NO\n");//若上面没有输出则该数不止由2020和2021组成则输出NO
	}
	return 0;
 } 

C - Replacing Elements

Problem Description
You have an array a1,a2,…,an. All ai are positive integers.
In one step you can choose three distinct indices i, j, and k (i≠j; i≠k; j≠k) and assign the sum of aj and ak to ai, i. e. make ai=aj+ak.
Can you make all ai lower or equal to d using the operation above any number of times (possibly, zero)?

Input
The first line contains a single integer t (1≤t≤2000) — the number of test cases.
The first line of each test case contains two integers n and d (3≤n≤100; 1≤d≤100) — the number of elements in the array a and the value d.
The second line contains n integers a1,a2,…,an (1≤ai≤100) — the array a.

Output
For each test case, print YES, if it’s possible to make all elements ai less or equal than d using the operation above. Otherwise, print NO.
You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).

Example
Input
3
5 3
2 3 2 5 4
3 4
2 4 4
5 4
2 1 5 3 6

Output
NO
YES
YES

思路要点
最小两个数相加小于等于m即可

AC题解

#include <stdio.h>
int main()
{
	int t, a[1000];
	scanf("%d", &t);
	while(t --)
	{
		int n, m, flag = 1, w;
		scanf("%d%d", &n, &m);
		for (int i = 0; i < n; i ++)
		{
			scanf("%d", &a[i]);
			if(a[i] > m) flag = 0;
		}
		if(flag)
		{
			printf("YES\n");
			continue;
		}
		for(int i = 0; i < n; i ++)
			for (int j = 0; j < n - 1 - i; j ++)
				if(a[j] > a[j + 1])
				{
					w = a[j];
					a[j] = a[j + 1];
					a[j + 1] = w;
				}
		if(a[0] + a[1] <= m)
		{
			printf("YES\n");
		}
		else printf("NO\n");
	}
	return 0;
 } 

D - String LCM

Problem Description
Let’s define a multiplication operation between a string a and a positive integer x: a⋅x is the string that is a result of writing x copies of a one after another. For example, “abc” ⋅ 2 = “abcabc”, “a” ⋅ 5 = “aaaaa”.

A string a is divisible by another string b if there exists an integer x such that b⋅x=a. For example, “abababab” is divisible by “ab”, but is not divisible by “ababab” or “aa”.

LCM of two strings s and t (defined as LCM(s,t)) is the shortest non-empty string that is divisible by both s and t.

You are given two strings s and t. Find LCM(s,t) or report that it does not exist. It can be shown that if LCM(s,t) exists, it is unique.

Input
The first line contains one integer q (1≤q≤2000) — the number of test cases.
Each test case consists of two lines, containing strings s and t (1≤|s|,|t|≤20). Each character in each of these strings is either ‘a’ or ‘b’.

Output
For each test case, print LCM(s,t) if it exists; otherwise, print -1. It can be shown that if LCM(s,t) exists, it is unique.

Example
Input
3
baba
ba
aa
aaa
aba
ab

Output
baba
aaaaaa
-1

Note
In the first test case, “baba” = “baba” ⋅ 1 = “ba” ⋅ 2.
In the second test case, “aaaaaa” = “aa” ⋅ 3 = “aaa” ⋅ 2.

思路要点
判断长字符串是否能由短字符串构成

AC题解

#include <stdio.h>
#include <string.h>
int n, m, l;
char a[200], b[200], s[200], c[5000];
int f();
int main()
{
	int t;
	scanf("%d", &t);
	getchar();
	while(t --)
	{
		int flag = 0, t;
		gets(a);gets(b);
		if(strlen(a) > strlen(b))//保证a是短字符串,b是长字符串
		{
			strcpy(s, a);
			strcpy(a, b);
			strcpy(b, s);
		}
		n = strlen(a), m = strlen(b), l = 0;
		while(l <= n * m)
		{
			for(int i = 0; i < n; i ++)//复制a,构成最小公倍数
				c[l ++] = a[i];
				
			if(f())//判断c是否由b构成
			{
				flag = 1;
				break;
			}
		}
		
		if(flag)
			for(int i = 0; i < l; i ++) printf("%c", c[i]);
		else
			printf("-1");
		printf("\n");
	}
	return 0;
}

int f()
{
	int y = 0;
	if(l%m != 0) return 0;
	for(int i = 0;i < l; i ++)
	{
		if(b[y] != c[i]) return 0;
		y ++;
		if(y == m) y = 0;
	}
	return 1;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值