练习cf1881B. Three Threadlets
题目如下
B. Three Threadlets
time limit per test2 seconds
memory limit per test256 megabytes
Once upon a time, bartender Decim found three threadlets and a pair of scissors.
In one operation, Decim chooses any threadlet and cuts it into two threadlets, whose lengths are positive integers and their sum is equal to the length of the threadlet being cut.
For example, he can cut a threadlet of length 5 into threadlets of lengths 2 and 3, but he cannot cut it into threadlets of lengths 2.5 and 2.5, or lengths 0 and 5, or lengths 3 and 4.
Decim can perform at most three operations. He is allowed to cut the threadlets obtained from previous cuts. Will he be able to make all the threadlets of equal length?
Input
The first line contains an integer 𝑡 (1≤𝑡≤104) — the number of test cases. Then follows the description of each test case.
In a single line of each test case, there are three integers 𝑎, 𝑏, 𝑐 (1≤𝑎,𝑏,𝑐≤109) — the lengths of the threadlets.
Output
For each test case, output "YES" if it is possible to make all the threadlets of equal length by performing at most three operations, otherwise output "NO".
You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
题目大意
现有三根绳子,最多切割三次,每切割一次绳子总数加一,只能按照5切成2和3这样的形式,不能切成两个2.5的形式,是否能把三根绳子切成相同长度的k跟
题目分析
判断能否把三根绳子切成相同长度k根,最多切3次,那么最多只会有6根绳子,只会出现3,4,5,6根四种情况,若要把绳子切成相同长度的k根,那么就假设切成k根,总长度为初始的三根相加,那么k根绳子每根的长度只会是sum/k,注意这里只能是整除的,绳子的总数固定在[3,6]那么k的值也固定,若无法整分则不可能分出来k根,直接结束进行下一次尝试;
点击查看代码
long long sum = (long long)a + b + c;
if(sum % k != 0){
return 0;
}
点击查看代码
llong long unit = sum / k;
long long arr[3] = {a, b, c};
int ops = 0;
int total = 0;
for(int i = 0; i < 3; i++){
if(arr[i] < unit){
return 0;
}
int full = arr[i] / unit;
int remain = arr[i] % unit;
if (full == 0){
continue;
}
if (remain == 0){
ops += full - 1;
}else{
ops += full;
}
total += full;
}
if(total == k && ops <= 3){
return 1;
}else{
return 0;
}
完整代码
点击查看代码
#include <stdio.h>
int check(long long a, long long b, long long c, int k) {
long long sum = a + b + c;
if(sum % k != 0){
return 0;
}
long long unit = sum / k;
long long arr[3] = {a, b, c};
int ops = 0;
int total = 0;
for(int i = 0; i < 3; i++){
if(arr[i] < unit){
return 0;
}
int full = arr[i] / unit;
int remain = arr[i] % unit;
if (full == 0){
continue;
}
if (remain == 0){
ops += full - 1;
}else{
ops += full;
}
total += full;
}
if(total == k && ops <= 3){
return 1;
}else{
return 0;
}
}
int main(){
int t;
scanf("%d", &t);
while(t--){
long long a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
int ok = 0;
for(int k = 3; k <= 6; k++){
if(check(a, b, c, k)){
ok = 1;
break;
}
}
if(ok){
printf("YES\n");
}else{
printf("NO\n");
}
}
return 0;
}

浙公网安备 33010602011771号