赞
踩
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently Vova found n candy wrappers. He remembers that he bought x candies during the first day, 2x candies during the second day, 4x candies during the third day, …, 2k−1x candies during the k-th day. But there is an issue: Vova remembers neither x nor k but he is sure that x and k are positive integers and k>1.
Vova will be satisfied if you tell him any positive integer x so there is an integer k>1 that x+2x+4x+⋯+2k−1x=n. It is guaranteed that at least one solution exists. Note that k>1.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (3≤n≤109) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer x and integer k>1 that x+2x+4x+⋯+2k−1x=n.
Output
Print one integer — any positive integer value of x so there is an integer k>1 that x+2x+4x+⋯+2k−1x=n.
Example
inputCopy
7
3
6
7
21
28
999999999
999999984
outputCopy
1
2
1
7
4
333333333
333333328
Note
In the first test case of the example, one of the possible answers is x=1,k=2. Then 1⋅1+2⋅1 equals n=3.
In the second test case of the example, one of the possible answers is x=2,k=2. Then 1⋅2+2⋅2 equals n=6.
In the third test case of the example, one of the possible answers is x=1,k=3. Then 1⋅1+2⋅1+4⋅1 equals n=7.
In the fourth test case of the example, one of the possible answers is x=7,k=2. Then 1⋅7+2⋅7 equals n=21.
In the fifth test case of the example, one of the possible answers is x=4,k=3. Then 1⋅4+2⋅4+4⋅4 equals n=28.
#include<bits/stdc++.h>
using namespace std;
double dp(int k,int x);
int main()
{
int n,a,x=1,k;
cin>>n;
int m[n];
for(int i=0;i<n;i++)
cin>>m[i];
int gb=0,nn=1;
while(gb<=n)
{
for(k=2;k<109&nn!=0;k++)
for(;x<99&nn!=0;x++)
{
if(dp(k,x)==m[gb])
{
cout<<x<<endl;
nn=0;
}
}
nn=1;
gb++;
}
//cout<<dp(5,1)<<endl;
return 0;
}
double dp(int k,int x)
{
double sum;
if(k<4){
if(k1)
sum=1*x;
if(k2)
sum=3x;
if(k==3)
sum=6x;
}else{
sum=(2*k-1)*x+dp(k-1,x);}
return sum;
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a positive integer n, it is guaranteed that n is even (i.e. divisible by 2).
You want to construct the array a of length n such that:
The first n2 elements of a are even (divisible by 2);
the second n2 elements of a are odd (not divisible by 2);
all elements of a are distinct and positive;
the sum of the first half equals to the sum of the second half (∑i=1n2ai=∑i=n2+1nai).
If there are multiple answers, you can print any. It is not guaranteed that the answer exists.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (2≤n≤2⋅105) — the length of the array. It is guaranteed that that n is even (i.e. divisible by 2).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).
Output
For each test case, print the answer — “NO” (without quotes), if there is no suitable answer for the given test case or “YES” in the first line and any suitable array a1,a2,…,an (1≤ai≤109) satisfying conditions from the problem statement on the second line.
Example
inputCopy
5
2
4
6
8
10
outputCopy
NO
YES
2 4 1 5
NO
YES
2 4 6 8 1 3 5 11
NO
#include<bits/stdc++.h>
using namespace std;
int t,n;
int main(){
cin>>t;
while(t–){
cin>>n;
if(!((n/2)%2)){
cout<<“YES\n”;
for(int i=1;i<=n/2;i++)cout<<i2<<’ ';
for(int i=1;i<n/2;i++)cout<<i2-1<<’ ‘;
cout<<n+n/2-1<<’\n’;
} else cout<<“NO\n”;
}
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recall that the sequence b is a a subsequence of the sequence a if b can be derived from a by removing zero or more elements without changing the order of the remaining elements. For example, if a=[1,2,1,3,1,2,1], then possible subsequences are: [1,1,1,1], [3] and [1,2,1,3,1,2,1], but not [3,2,3] and [1,1,1,1,2].
You are given a sequence a consisting of n positive and negative elements (there is no zeros in the sequence).
Your task is to choose maximum by size (length) alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite from the sign of the current element, like positive-negative-positive and so on or negative-positive-negative and so on). Among all such subsequences, you have to choose one which has the maximum sum of elements.
In other words, if the maximum length of alternating subsequence is k then your task is to find the maximum sum of elements of some alternating subsequence of length k.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of the test case contains one integer n (1≤n≤2⋅105) — the number of elements in a. The second line of the test case contains n integers a1,a2,…,an (−109≤ai≤109,ai≠0), where ai is the i-th element of a.
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105 (∑n≤2⋅105).
Output
For each test case, print the answer — the maximum sum of the maximum by size (length) alternating subsequence of a.
Example
inputCopy
4
5
1 2 3 -1 -2
4
-1 -2 -1 -3
10
-2 8 3 8 -4 -15 5 -2 -3 1
6
1 -1000000000 1 -1000000000 1 -1000000000
outputCopy
2
-1
6
-2999999997
Note
In the first test case of the example, one of the possible answers is [1,2,3–,−1–––,−2].
In the second test case of the example, one of the possible answers is [−1,−2,−1–––,−3].
In the third test case of the example, one of the possible answers is [−2–––,8,3,8–,−4–––,−15,5–,−2–––,−3,1–].
In the fourth test case of the example, one of the possible answers is [1–,−1000000000–––––––––––––,1–,−1000000000–––––––––––––,1–,−1000000000–––––––––––––].
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main()
{ ll t,n,s,p,c;
cin>>t;
while (t–){
cin>>n;s=0;
cin>>p;n–;
while (n–){
cin>>c;
if(p*c>0){if(c>p)p=c; }
else {s+=p;p=c;}
}
cout<<s+p<<endl;
}
return 0;
}
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array a consisting of n integers (it is guaranteed that n is even, i.e. divisible by 2). All ai does not exceed some integer k.
Your task is to replace the minimum number of elements (replacement is the following operation: choose some index i from 1 to n and replace ai with some integer in range [1;k]) to satisfy the following conditions:
after all replacements, all ai are positive integers not greater than k;
for all i from 1 to n2 the following equation is true: ai+an−i+1=x, where x should be the same for all n2 pairs of elements.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and k (2≤n≤2⋅105,1≤k≤2⋅105) — the length of a and the maximum possible value of some ai correspondingly. It is guratanteed that n is even (i.e. divisible by 2). The second line of the test case contains n integers a1,a2,…,an (1≤ai≤k), where ai is the i-th element of a.
It is guaranteed that the sum of n (as well as the sum of k) over all test cases does not exceed 2⋅105 (∑n≤2⋅105, ∑k≤2⋅105).
Output
For each test case, print the answer — the minimum number of elements you have to replace in a to satisfy the conditions from the problem statement.
Example
inputCopy
4
4 2
1 2 1 2
4 3
1 2 2 1
8 7
6 1 1 7 6 3 4 6
6 6
5 2 6 1 3 4
outputCopy
0
1
4
2
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int n,k;
cin>>n>>k;
int a[n];
for(int i =0;i<n;i++)
{
cin>>a[i];
}
int s[2*k+2];
memset(s, 0 , sizeof(s));
for(int i =0;i<n/2;i++)
{
int x = a[i];
int y = a[n-1-i];
if(x>y)
swap(x,y);
s[2]+=2;
s[x+1]-=2;
s[x+1]+=1;
s[x+y]-=1;
s[x+y+1]+=1;
s[y+k+1]-=1;
s[y+k+1]+=2;
}
int ans = n;
for(int i=2;i<=2*k;i++)
{
s[i]+=s[i-1];
ans = min(ans, s[i]);
}
cout<<ans<<endl;
}
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。