我是靠谱客的博主 霸气洋葱,最近开发中收集的这篇文章主要介绍BNUZ 日常训练2021.1.29A - Odd DivisorB - New Year’s NumberC - Replacing ElementsD - String LCM,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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("NOn");//若为1则该数没有除1以外的其它奇因数
		else
			printf("YESn");//若不为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("YESn");
				flag = 1;
				break;
			}
			n -= 2020;//如果不能则至少有一个2020,也可为2021
			if(flag) break;
		}
		if(!flag) printf("NOn");//若上面没有输出则该数不止由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("YESn");
			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("YESn");
		}
		else printf("NOn");
	}
	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;
}

最后

以上就是霸气洋葱为你收集整理的BNUZ 日常训练2021.1.29A - Odd DivisorB - New Year’s NumberC - Replacing ElementsD - String LCM的全部内容,希望文章能够帮你解决BNUZ 日常训练2021.1.29A - Odd DivisorB - New Year’s NumberC - Replacing ElementsD - String LCM所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部