题目描述
You have been blessed as a child of Omkar. To express your gratitude, please solve this problem for Omkar!An array a of length nn is called complete if all elements are positive and don’t exceed 1000, and for all indices x,y,z (1≤x,y,z≤n), ax+ay≠az (not necessarily distinct).You are given one integer n. Please find any complete array of length n. It is guaranteed that under given constraints such array exists.
Input
Each test contains multiple test cases. The first line contains tt(1≤t≤1000) — the number of test cases. Description of the test cases follows.The only line of each test case contains one integer n (1≤n≤1000).It is guaranteed that the sum of n over all test cases does not exceed 1000.
Output
For each test case, print a complete array on a single line. All elements have to be integers between 1 and 1000and for all indices x,y,z (1≤x,y,z≤n) (not necessarily distinct), ax+ay≠az must hold.If multiple solutions exist, you may print any.
Example
input
2
5
4
output
1 5 3 77 12
384 384 44 44
题意思路:给一个t,t组输入,每组输入一个n,输出一个长度为n的数组,要求任意两个数的和在数组不出现,可以想到,如果全部都是一样的数,相加的结果是不会出现在数组内的。
代码
#include
#include
#include
#define LL long long
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=0; i<n; i++)
printf("1 ");
printf("\n");
}
return 0;
}
题目描述
In Omkar’s last class of math, he learned about the least common multiple, or LCM. LCM(a,b) is the smallest positive integer x which is divisible by both a and b.Omkar, having a laudably curious mind, immediately thought of a problem involving the LCM operation: given an integer n, find positive integers a and b such that a+b=n and LCM(a,b) is the minimum value possible.Can you help Omkar solve his ludicrously challenging math problem?
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤10). Description of the test cases follows.Each test case consists of a single integer nn (2≤n≤109).
Output
For each test case, output two positive integers aa and bb, such that a+b=n and LCM(a,b) is the minimum possible.
Example
input
3
4
6
9
output
2 2
3 3
3 6
题意思路:t组输入,每组输入一个n,求一组a,b,使a,b的LCM值最小且a+b=n。LCM值即为最小公倍数。求最小公倍数,和为n,最大最小公倍数为a为1,b便为n-1。若n有因数,则通过最小因数来求出最大因数,然后最大因数和n-最大因数即为解。
代码
#include
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
int n;
cin >> n;
int mx=1;
for(int i=2; i*i<=n; i++)
{
if(n%i==0)
{
mx=max(mx,max(i,n/i));
}
}
cout << mx << " " << n-mx << "\n";
}
return 0;
}
题目描述
Patrick likes to play baseball, but sometimes he will spend so many hours hitting home runs that his mind starts to get foggy! Patrick is sure that his scores across n sessions follow the identity permutation (ie. in the first game he scores 1 point, in the second game he scores 2 points and so on). However, when he checks back to his record, he sees that all the numbers are mixed up!Define a special exchange as the following: choose any subarray of the scores and permute elements such that no element of subarray gets to the same position as it was before the exchange. For example, performing a special exchange on [1,2,3] can yield [3,1,2] but it cannot yield [3,2,1] since the 2 is in the same position.Given a permutation of nn integers, please help Patrick find the minimum number of special exchanges needed to make the permutation sorted! It can be proved that under given constraints this number doesn’t exceed 1018.An array a is a subarray of an array b if aa can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤100). Description of the test cases follows.The first line of each test case contains integer n (1≤n≤2⋅105) — the length of the given permutation.The second line of each test case contains nn integers a1,a2,…,an (1≤ai≤n) — the initial permutation.It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
Output
For each test case, output one integer: the minimum number of special exchanges needed to sort the permutation.
Example
input
2
5
1 2 3 4 5
7
3 2 4 5 1 6 7
output
0
2
题意思路:t组输入,每组输入一个n,然后n个数(n个数的排列),通过操作:选择一个范围,改变范围内的数的位置,要求每一个数改变后位置与之前不相同,则此操作可行。求把这n个数归位需要几步。可以想到,如果全部是乱序,则一步直接还原成原来的就可以(1)。如果已经全部归位,则不需要操作(0)。还有一种就是既有在本位的,也有不在本位的,这种情况的话就需要先把所有数放到不在自己本位(为下一步操作考虑),已经在本位的则需要先放到别处,然后统一贵为,总共需要两步(2)。
还有一个需要注意的地方是,选择一个区间进行操作,如果是最左端或是最右端已经有排好的,先处理不选择这两部分的区间。
代码
#include
using namespace std;
int main()
{
int T;
cin >> T;
while(T--)
{
int n,a[200005];
cin >> n;
int tot=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
if(a[i]!=i)
tot++;
}
int tn=n;
for(int i=n; i>=1; i--)
{
if(a[i]==i)
tn--;
else
break;
}
for(int i=1; i<=n; i++)
{
if(a[i]==i)
tn--;
else
break;
}
if(tot==0)
printf("0\n");
else if(tot<tn)
printf("2\n");
else if(tot==tn)
printf("1\n");
}
return 0;
}