链接 https://codeforces.com/contest/1348
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
ll ans1=0,ans2=0;
for(int i=1;i<=n/2-1;++i)
ans1+=(1<<i);
ans1+=(1<<n);
for(int i=n/2;i<=n-1;++i)
ans2+=(1<<i);
cout<<ans1-ans2<<"\n";
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=100+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t,n,k;
int a[maxn];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>k;
set<int> s;
for(int i=1;i<=n;++i)
cin>>a[i],s.insert(a[i]);
if(s.size()>k)
{
puts("-1");
continue;
}
else if(s.size()<k)
{
for(int i=1;i<=n;++i)
{
s.insert(i);
if(s.size()==k)
break;
}
}
vector<int> ans;
for(auto i : s)
ans.push_back(i);
for(int i=2;i<=n;++i)
{
int p=(int)ans.size()-k;
if(a[i]==ans[p])
ans.push_back(a[i]);
else
ans.push_back(ans[p]),--i;
}
cout<<(int)ans.size()<<"\n";
for(auto i : ans)
cout<<i<<" ";
cout<<"\n";
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t,n,k;
string s;
int main()
{
cin>>t;
while(t--)
{
cin>>n>>k>>s;
sort(s.begin(),s.end());
s="0"+s;
string ans="";
if(s[1]==s[k])
{
ans=s[1];
if(s[k+1]==s[n])
{
int cnt=n/k+(n%k?1:0)-1;
for(int i=1;i<=cnt;++i)
ans+=s[k+1];
}
else
{
for(int i=k+1;i<=n;++i)
ans+=s[i];
}
}
else
ans=s[k];
cout<<ans<<"\n";
}
return 0;
}
直接构造 1+2+4+8+16+x=n。最后将这些数排个序,然后输出差分数组
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
vector<int> ans;
for(int i=1;i<=n;i*=2)
{
ans.push_back(i);
n-=i;
}
if(n>0)
{
ans.push_back(n);
sort(ans.begin(),ans.end());
}
int n=(int)ans.size()-1;
cout<<n<<"\n";
for(int i=1;i<=n;++i)
cout<<ans[i]-ans[i-1]<<" ";
cout<<"\n";
}
return 0;
}
每次加入数组的数是 m i n ( n / 2 , a n s . b a c k ( ) ∗ 2 ) min(n/2,ans.back()∗2) min(n/2,ans.back()∗2)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
vector<int> ans;
ans.push_back(1);
n--;
while(1)
{
if(2*ans.back()>=n)
{
ans.push_back(n);
break;
}
ans.push_back(min(n/2,ans.back()*2));
n-=ans.back();
}
int n=(int)ans.size()-1;
cout<<n<<"\n";
for(int i=1;i<=n;++i)
cout<<ans[i]-ans[i-1]<<" ";
cout<<"\n";
}
return 0;
}
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5,maxm=1e5+5;
const int mod=1e9+7,inf=0x7f7f7f7f;
int t,n;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
vector<int> ans;
ans.push_back(1);
int cur=1;
n--;
while(cur*4<=n)
{
cur*=2;
n-=cur;
ans.push_back(cur);
}
if(cur*2>=n)
ans.push_back(n);
else
ans.push_back(n/2),ans.push_back(n-n/2);
int cnt=ans.size()-1;
cout<<cnt<<"\n";
for(int i=1;i<=cnt;++i)
cout<<ans[i]-ans[i-1]<<" ";
cout<<"\n";
}
return 0;
}