Despite his bad reputation, Captain Flint is a friendly person (at least, friendly to animals). Now Captain Flint is searching worthy sailors to join his new crew (solely for peaceful purposes). A sailor is considered as worthy if he can solve Flint’s task.
Recently, out of blue Captain Flint has been interested in math and even defined a new class of integers. Let’s define a positive integer x as nearly prime if it can be represented as p⋅q, where 1 < p < q 1 1<p<q
Captain Flint guessed an integer n and asked you: can you represent it as the sum of 4 different positive integers where at least 3 3 3 of them should be nearly prime.
Uncle Bogdan easily solved the task and joined the crew. Can you do the same?
The first line contains a single integer t ( 1 ≤ t ≤ 1000 ) (1≤t≤1000) (1≤t≤1000) — the number of test cases.
Next t lines contain test cases — one per line. The first and only line of each test case contains the single integer n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) (1≤n≤2⋅10^5) (1≤n≤2⋅105)— the number Flint guessed.
For each test case print:
You can print each character of Y E S YES YES or N O NO NO in any case.
i n p u t input input
7
7
23
31
36
44
100
258
O u t p u t Output Output
NO
NO
YES
14 10 6 1
YES
5 6 10 15
YES
6 7 10 21
YES
2 10 33 55
YES
10 21 221 6
N o t e Note Note
In the first and second test cases, it can be proven that there are no four different positive integers such that at least three of them are nearly prime.
In the third test case, n = 31 = 2 ⋅ 7 + 2 ⋅ 5 + 2 ⋅ 3 + 1 : n=31=2⋅7+2⋅5+2⋅3+1: n=31=2⋅7+2⋅5+2⋅3+1: integers 14 , 10 , 6 14, 10, 6 14,10,6 are nearly prime.
In the fourth test case, n = 36 = 5 + 2 ⋅ 3 + 2 ⋅ 5 + 3 ⋅ 5 : n=36=5+2⋅3+2⋅5+3⋅5: n=36=5+2⋅3+2⋅5+3⋅5: integers 6 , 10 , 15 6, 10, 15 6,10,15 are nearly prime.
In the fifth test case, n = 44 = 2 ⋅ 3 + 7 + 2 ⋅ 5 + 3 ⋅ 7 : n=44=2⋅3+7+2⋅5+3⋅7: n=44=2⋅3+7+2⋅5+3⋅7: integers 6 , 10 , 21 6, 10, 21 6,10,21 are nearly prime.
In the sixth test case, n = 100 = 2 + 2 ⋅ 5 + 3 ⋅ 11 + 5 ⋅ 11 : n=100=2+2⋅5+3⋅11+5⋅11: n=100=2+2⋅5+3⋅11+5⋅11: integers 10 , 33 , 55 10, 33, 55 10,33,55 are nearly prime.
In the seventh test case, n = 258 = 2 ⋅ 5 + 3 ⋅ 7 + 13 ⋅ 17 + 2 ⋅ 3 : n=258=2⋅5+3⋅7+13⋅17+2⋅3: n=258=2⋅5+3⋅7+13⋅17+2⋅3: integers 10 , 21 , 221 , 6 10, 21, 221, 6 10,21,221,6 are nearly prime.
根据题意,我们可以得到:nearly prime(下文称近质数)
质数: 2 、 3 、 5 、 7 … … 2、3、5、7…… 2、3、5、7……
2 × 3 = 6 2×3=6 2×3=6 3 × 5 = 15 3×5=15 3×5=15 5 × 7 = 35 5×7=35 5×7=35
2 × 5 = 10 2×5=10 2×5=10 3 × 7 = 21 3×7=21 3×7=21
2 × 7 = 14 2×7=14 2×7=14
则nearly prime(近质数)从小到大依次为 6 、 10 、 14 、 15 … … 6、10、14、15…… 6、10、14、15……
题目要求输入一个 n n n,要求满足有 4 4 4个数之和(其中 3 3 3个数为nearly prime)为 n n n,那么我们依次取nearly prime 从小到大3个分别为 6 、 10 、 14 6、10、14 6、10、14。所以, n n n至少要大于 30 = 6 + 10 + 14 30=6+10+14 30=6+10+14,才能满足题意输出 Y E S YES YES
即 n ≤ 30 n ≤ 30 n≤30时,输出 N O NO NO
当 n > 30 n>30 n>30时,题目要求 4 4 4个数不能重复,所以不能存在, 6 6 6 6 6 6 10 10 10 14 14 14, 6 6 6 10 10 10 10 10 10 14 14 14, 6 6 6 10 10 10 14 14 14 14 14 14,这 3 3 3种情况。所以需要把这三种情况单独列出来,也就是 n = 36 , 40 , 44 n = 36,40,44 n=36,40,44时。
此时可以通过用 15 15 15 ( 15 (15 (15也是nealy prime ) ) )代替 14 14 14来改变前三个数的总和,达成 4 4 4个数不相同的目的。
分析完毕 —— 上代码:
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
//nearly prime 从小到大分别是6、10、14、15、21......
//(前三个数取尽可能小,最后一个数无所谓)
if (n <= 30)
{
printf("NO\n"); //所以n至少要大于30 = 6+10+14,才有可能是YES
continue;
}
else
{
printf("YES\n");
//要求四个数不相同所以当有2个6,或者2个10,或者2个14的时候不满足
//此时可以通过用15替代14来改变前三个数的总和。
if (n == 36 || n == 40 || n == 44)
printf("6 10 15 %d\n", n - 31);//第4个数是n减去前三个数的总和
else
printf("6 10 14 %d\n", n - 30);//第4个数是n减去前三个数的总和
//如果需要跟题目给出例子一样输出:可以将if的条件改成如下:
/*
if (n == 36)
printf("5 6 10 15\n");
else if (n == 40)
printf("6 10 15 9\n");
else if (n == 44)
printf("6 7 10 21\n");
else
printf("6 10 14 %d\n", n - 30);
*/
}
}
return 0;
}
Captain Flint and his crew keep heading to a savage shore of Byteland for several months already, drinking rum and telling stories. In such moments uncle Bogdan often remembers his nephew Denis. Today, he has told a story about how Denis helped him to come up with an interesting problem and asked the crew to solve it.
In the beginning, uncle Bogdan wrote on a board a positive integer x x x consisting of n n n digits. After that, he wiped out x x x and wrote integer k k k instead, which was the concatenation of binary representations of digits x x x consists of (without leading zeroes). For example, let x = 729 x=729 x=729, then k = 111101001 k=111101001 k=111101001 ( ( (since 7 = 111 7=111 7=111, 2 = 10 2=10 2=10, 9 = 1001 9=1001 9=1001 ) ) ).
After some time, uncle Bogdan understood that he doesn’t know what to do with k k k and asked Denis to help. Denis decided to wipe last n n n digits of k k k and named the new number as r r r.
As a result, Denis proposed to find such integer x x x of length n n n that r r r ( ( (as number ) ) ) is maximum possible. If there are multiple valid x x x then Denis is interested in the minimum one.
All crew members, including captain Flint himself, easily solved the task. All, except cabin boy Kostya, who was too drunk to think straight. But what about you?
Note: in this task, we compare integers ( x (x (x or k ) k) k) as numbers ( ( (despite what representations they are written in ) ) ), so 729 < 1999 729<1999 729<1999 or 111 < 1000 111<1000 111<1000.
The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 ) (1≤t≤1000) (1≤t≤1000) — the number of test cases.
Next t lines contain test cases — one per test case. The one and only line of each test case contains the single integer n n n ( 1 ≤ n ≤ 105 ) (1≤n≤105) (1≤n≤105) — the length of the integer x x x you need to find.
It’s guaranteed that the sum of n n n from all test cases doesn’t exceed 2 ⋅ 1 0 5 2⋅10^5 2⋅105.
For each test case, print the minimum integer x of length n such that obtained by Denis number r is maximum possible.
i n p u t input input
2
1
3
o u t p u t output output
8
998
N o t e Note Note
In the second test case (with n = 3 n=3 n=3), if uncle Bogdan had x = 998 x=998 x=998 then k = 100110011000 k=100110011000 k=100110011000. Denis ( ( (by wiping last n = 3 n=3 n=3 digits ) ) ) will obtain r = 100110011 r=100110011 r=100110011.
It can be proved that the 100110011 100110011 100110011 is the maximum possible r r r Denis can obtain and 998 998 998 is the minimum x x x to obtain it.
对于长度 n n n的正整数 x x x,定义一个 k k k,是 n n n中的每一位数的二进制组成,假设 x = 729 x=729 x=729,那么 7 ( 2 ) = 111 7_{(2)}=111 7(2)=111, 2 ( 2 ) = 10 2_{(2)}=10 2(2)=10, 9 ( 2 ) = 1001 9_{(2)}=1001 9(2)=1001,所以 k = 111101001 k=111101001 k=111101001
则 r = 111101 r=111101 r=111101
题目要求:给定一个 n n n,求出最小的 x x x,使得对应的r最大。
要使得 r r r 最大,那么未被抹除的 k k k 前面几个数字,自然是越大越好,所以全部取 9 9 9。
那么我们讨论的就是抹除掉的那几位。
9 ( 2 ) = 1001 9_{(2)}=1001 9(2)=1001、 8 ( 2 ) = 1000 8_{(2)}=1000 8(2)=1000、 7 2 = 111 7_{2}=111 72=111
7 7 7只有 3 3 3位,很明晰会使得 r r r 减小。所以只能考虑 8 8 8和 9 9 9;
既然被抹去了, 8 8 8 和 9 9 9,都无所谓了。
要求最小的 x x x,那么自然选 8 8 8了。
分析完毕,上代码:
#include
#include
#include
#include
#include
typedef long long ll;
using namespace std;
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
int T, i;
cin >> n;
T = n / 4;
if (n % 4)
T++;
for (i = 1; i < n - T; ++i)
printf("9");
for (i = 1; i <= T; ++i)
printf("8");
cout << endl;
}
return 0;
}
话说这代码怎么看起来,这道题那么简单呢?我怎么做了那么久呢?值得深思,好吧,菜是原罪!!!