2020 BIT冬训-模拟与暴力 G - NPY and arithmetic progression HDU - 5143
Problem Description
NPY is learning arithmetic progression in his math class. In mathematics, an arithmetic progression (AP) is a sequence of numbers
such that the difference between the consecutive terms is constant.(from wikipedia)
He thinks it's easy to understand,and he found a challenging problem from his talented math teacher:
You're given four integers, a1,a2,a3,a4, which are the numbers of 1,2,3,4 you have.Can you divide these numbers into some
He thinks it's easy to understand,and he found a challenging problem from his talented math teacher:
You're given four integers, a1,a2,a3,a4, which are the numbers of 1,2,3,4 you have.Can you divide these numbers into some
Arithmetic Progressions,whose lengths are equal to or greater than 3?(i.e.The number of AP can be one)
Attention: You must use every number exactly once.
Can you solve this problem?
Attention: You must use every number exactly once.
Can you solve this problem?
InputThe first line contains a integer T — the number of test cases (1≤T≤100000).
The next T lines,each contains 4 integers a1,a2,a3,a4(0≤a1,a2,a3,a4≤109).OutputFor each test case,
print "Yes"(without quotes) if the numbers can be divided properly,otherwise print "No"(without quotes).Sample Input
3 1 2 2 1 1 0 0 0 3 0 0 0
Sample Output
Yes No Yes
Hint
In the first case,the numbers can be divided into {1,2,3} and {2,3,4}.
In the second case,the numbers can't be divided properly.
In the third case,the numbers can be divided into {1,1,1}.
这题是借鉴网上的思路。自己之前的思路发现结果是错的(而且也更麻烦,所以就烦死了nnd)…………………………
思路是由于一共只有{1,2,3},{2,3,4},{1,2,3,4}和111……,222……,333…….这6种等差数列。
且前三种如果有大于三个的数量就可以转变成后三种。
如三个{1,2,3}就可以变成111,222,333。
故只需要枚举前三种的数量为0,1,2时。1,2,3,4的数量都为0或者大于3即可。
最大情况即2个{1,2,3},{2,3,4},{1,2,3,4}。即4个1,6个2、3,4个4.
AC代码如下:
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; int t,a,b,c,d,temp1,temp2,temp3,temp4,flag; int main(){ scanf("%d",&t); while(t--){ flag =0; scanf("%d%d%d%d",&a,&b,&c,&d); if((a==0||a>=3)&&(b==0||b>=3)&&(c==0||c>=3)&&(d==0||d>=3)){ printf("Yes\n"); }else{ for(int i=0;i<=2;i++){ for(int j=0;j<=2;j++){ for(int k=0;k<=2;k++){ temp1=a-i-j; temp2=b-i-j-k; temp3=c-i-j-k; temp4=d-i-k; if((temp1==0||temp1>=3)&&(temp2==0||temp2>=3)&&(temp3==0||temp3>=3)&&(temp4==0||temp4>=3)){ flag =1; } } } } if(flag){ printf("Yes\n"); }else printf("No\n"); } } }

浙公网安备 33010602011771号