P13551 ももいろの鍵题解
闲话
很水的构造题,但是没有场切(太蒟蒻力)。
正文
solution
一次在 CF 上看到了 Constructive Algorithms 的 tag,于是在 oiwiki 上了解了一下。
很容易发现这题满足构造题的特点:输入规模大(
所以这是道构造题,然后呢?
特殊性质
可以观察下特殊性质,正解往往可以从特殊性质上推出,这题也不例外。
这题的特殊性质是
有什么用呢?
首先,题面出现了按位与,考虑位运算。在二进制中,第
那么
由于二进制里是逢二进一,所以一个前
有什么启示呢?
不难发现,对于
正解
受特殊性质启发,我们可以先求出第一个大于等于
code
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,bmi,bmi2,fma")
#include<bits/stdc++.h>
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
#define getchar() (p1==p2 and (p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int read(){
int x=0,f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-')f=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
x=(x<<1)+(x<<3)+(c^'0');
c=getchar();
}
return x*f;
}
inline void write(int x){
if(x<0){
putchar('-');
x=-x;
}
if(x>9)write(x/10);
putchar(x%10+'0');
}
int n,t;
void solve(int x){
if(x==0){
printf("1 0\n");
return;
}
if(x<0){
return;
}
int tmp=log2(x);
int maxn=(1<<(tmp+1))-1;
for(int i=maxn-x;i<=maxn/2;i++){
printf("2 %d %d\n",i,maxn-i);
}
solve(maxn-x-1);
}
signed main(){
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
t=read();
while(t--){
n=read();
write(n/2+1);
putchar('\n');
solve(n);
}
return 0;
}
还是很水的喵~