A. Puzzle Pieces
You are given a special jigsaw puzzle consisting of n⋅m identical pieces. Every piece has three tabs and one blank, as pictured below.
The jigsaw puzzle is considered solved if the following conditions hold:
The pieces are arranged into a grid with n rows and m columns.
For any two pieces that share an edge in the grid, a tab of one piece fits perfectly into a blank of the other piece.
Through rotation and translation of the pieces, determine if it is possible to solve the jigsaw puzzle.
Input
The test consists of multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. Next t lines contain descriptions of test cases.
Each test case contains two integers n and m (1≤n,m≤105).
Output
For each test case output a single line containing “YES” if it is possible to solve the jigsaw puzzle, or “NO” otherwise. You can print each letter in any case (upper or lower).
Example
inputCopy
3
1 3
100000 100000
2 2
outputCopy
YES
NO
YES
Note
For the first test case, this is an example solution:
For the second test case, we can show that no solution exists.
For the third test case, this is an example solution:
题意:
你有上图所用的拼图,问是否可以衔接成 n x m 的图形。
思路:
实际上如果能够合成的话,那么只有两种情况:n=1∣∣m=1,n=2&&m=2。
很显然,如果只有一排,一定是可以的;接下来就考虑多排的情况,可以发现 2 x 2 的图形是构成多排的基础 ,但是仔细观察后发现,它不能再添加图形了。
代码:
#include<bits/stdc++.h>
using namespace std;
main()
{
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
if(n==1 || m==1 || (n==2 && m==2))
puts("YES");
else
puts("NO");
}
}
B. Card Constructions
A card pyramid of height 1 is constructed by resting two cards against each other. For h>1, a card pyramid of height h is constructed by placing a card pyramid of height h−1 onto a base. A base consists of h pyramids of height 1, and h−1 cards on top. For example, card pyramids of heights 1, 2, and 3 look as follows:
You start with n cards and build the tallest pyramid that you can. If there are some cards remaining, you build the tallest pyramid possible with the remaining cards. You repeat this process until it is impossible to build another pyramid. In the end, how many pyramids will you have constructed?
Input
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤1000) — the number of test cases. Next t lines contain descriptions of test cases.
Each test case contains a single integer n (1≤n≤109) — the number of cards.
It is guaranteed that the sum of n over all test cases does not exceed 109.
Output
For each test case output a single integer — the number of pyramids you will have constructed in the end.
Example
inputCopy
5
3
14
15
24
1
outputCopy
1
2
1
3
0
Note
In the first test, you construct a pyramid of height 1 with 2 cards. There is 1 card remaining, which is not enough to build a pyramid.
In the second test, you build two pyramids, each of height 2, with no cards remaining.
In the third test, you build one pyramid of height 3, with no cards remaining.
In the fourth test, you build one pyramid of height 3 with 9 cards remaining. Then you build a pyramid of height 2 with 2 cards remaining. Then you build a final pyramid of height 1 with no cards remaining.
In the fifth test, one card is not enough to build any pyramids.
题意
有如上图各种层数的纸牌塔,你有n张纸牌,并且你每次只能搭你当前可以搭的最大层数的塔。问你可以用这n张牌搭多少塔
思路
找规律题
因为每层塔需要的卡牌数是固定的,也有规律,因此我们可以先预处理出各层塔的高度。然后再算出n张牌能搭多少塔即可。
一层:2
两层:2+2+3=7
三层:7+2+6=15
四层:15+2+9=26
很容易发现对第 n 个堆,卡牌数量为 n∗(n+1)+n∗(n−1)/2
代码
#include<bits/stdc++.h>
using namespace std;
main()
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int ans=0;
while(n>=2)
{
for(int i=sqrt(n)+1;i>=1;i--)
{
int sum1=i*(i+1)+i*(i-1)/2;
int sum2=(i+1)*(i+2)+(i+1)*i/2;
if(n<sum2 && n>=sum1)
{
n-=sum1;
ans++;
break;
}
}
}
cout<<ans<<endl;
}
}
如有错误,请指正