打表打表

算法

你总会遇见很多题目,看着有点逻辑,但是数学推导非常难受。看着有点规律,但是样例太少无法确定。或者你很肯定的能给出暴力做法,但是卡你时间复杂度。

这些种种情况,我们都可以用到打表!打表一般用到的就是暴力循环,暴力判断,dfs bfs。(这里将不定时更新作者遇到的有趣的打表题)

什么是打表?

我们先说说狭义的打表(仅代表个人理解)

打表就是将所有输入情况的答案保存在代码中,输入数据后直接输出就可以了

打表法具有快速,易行(可以写暴力枚举程序)的特点,缺点是代码可能太大,或者情况覆盖不完

对于不会超时,数据规模适合打表,为了简洁你也可以打表。

比如这道题

这道题n<=20完全可以打表

只要你得出每个数字需要多少个火柴,问题就简单了。而不管几位的数字都是

1
int a[10]={6,2,5,5,4,5,6,3,7,6}

这就是0-9需要的火柴数量。往后以此类推,10需要一个1和一个0。11需要两个1,也就是说我们一位位拆分即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<bits/stdc++.h>
using namespace std;
int main(){
freopen("ans.txt","w",stdout);
int a[10]={6,2,5,5,4,5,6,3,7,6};
int i,j,temp=0,num=0,k,in[2020],n;
in[0]=6;
for(i=1;i<=2000;i++){
k=i;
temp=0;
while(k){
temp+=a[k%10];
k/=10;
}
in[i]=temp;
}
n=0;
Again:
num=0;
for(i=0;i<=999;i++){
for(j=0;j<=999;j++){
if(n==in[i]+in[j]+in[i+j]+4) num++;
}
}
printf("%d,",num);
if(n<24){n++;goto Again;}
return 0;
}

然后得到答案你就可以这样,面向答案编程

1
2
3
4
5
6
7
8
9
#include<iostream>
using namespace std;
int ans[]={0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,8,9,6,9,29,39,38,65,88,128};
int n;
int main(){
cin>>n;
cout<<ans[n]<<'\n';
return 0;
}

也许这道题你还没有领略到打表的方便之处。

试试这道题

数位dp题,你也许会觉得难。但是没关系,我们提供更暴力更简单的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define For(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int N=1e6+5;
int ans,a,b,sum[2005]={0, 202174, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 155315, 136131, 138503, 138214, 138252, 138252, 138214, 138503, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138503, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 0, 0, 0, 138214, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 0, 0, 0, 138252, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 0, 0, 0, 138252, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 0, 0, 0, 138214, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 0, 0, 0, 138503, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 0, 0, 0, 136131, 155315, 155315, 136131, 138503, 138214, 138252, 138252, 0, 0, 0, 155315, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
inline void check(int x){
if(x<=9){ans++;return;}
int a=x,b=-5;
while(a){
if(abs(a%10-b)<2)return;
b=a%10;a=a/10;
}
ans++;
}
int main(){
cin>>a>>b;
if(b-a<=1000000){
For(i,a,b)check(i);
cout<<ans;
return 0;
}
int p=ceil(a*1.0/1000000),q=floor(b*1.0/1000000);
For(i,1,2000)sum[i]+=sum[i-1];
ans+=sum[q]-sum[p];
if(!ans)ans++;
p=p*1000000,q=q*1000000;
if(a!=p)
For(i,a,p-1)check(i);
if(b!=q)
For(i,q+1,b)check(i);
cout<<ans;
return 0;

这里引用了five20的代码。由于过于暴力我们就不多解释了。这一长串数组就是打表打出来的。然后直接暴力筛选。

打表真的有用吗?

有的朋友。也许我们不能遇见上面这些及其粗暴的方法能写的题目。但是往往我们可以找到很多答案来寻找规律。这就是我理解的广义的打表。

可以看看这道题

题意:构造一个矩阵,使相邻两个数之和两两不重复。

刚拿到这道题目的你是不是有点摸不着头脑?

这很明显你不能很暴力的来遍历拿到一个矩阵,所以我们需要找到某种特殊的方式,让我们按照这个方式构造出的矩阵一定相邻两个数之和不重复。

这时候我们不妨打表。注释写在代码上了。

原理就是尝试填充一个 n x m的矩阵,使得矩阵中相邻元素的和都是唯一的。如果找到这样的填充方式,它会输出该矩阵。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
int a[1005][1005];
int n,m;
int tot=0;
bool vis[10000];
bool check()
{
set<int>s;
for(int i=1;i<=n-1;i++)
{
for(int j=1;j<=m-1;j++)
{
if(s.count(a[i][j]+a[i+1][j])) // 检查当前元素和下方元素的和
{
return false;
}
if(s.count(a[i][j]+a[i][j+1])) // 检查当前元素和右方元素的和
{
return false;
}
s.insert(a[i][j]+a[i][j+1]); // 插入当前元素和右方元素的和
s.insert(a[i][j]+a[i+1][j]); // 插入当前元素和下方元素的和
}
}
// 检查最后一列的元素和下方元素的和
for(int i=1;i<=n-1;i++)
{
if(s.count(a[i][m]+a[i+1][m]))return false;
s.insert(a[i][m]+a[i+1][m]);
}
// 检查最后一行的元素和右方元素的和
for(int i=1;i<=m-1;i++)
{
if(s.count(a[n][i]+a[n][i+1]))return false;
s.insert(a[n][i]+a[n][i+1]);
}
return true;
}
void dfs(int dep)
{
tot++;
if(dep==n*m+1)
{
if(check())
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cout <<a[i][j]<<" ";
}
cout<<endl;
}
}
return;
}
for(int i=1;i<=n*m;i++)
{
if(vis[i]) continue; // 如果数字i已经被使用,跳过
vis[i]=1; // 标记数字i为已使用
int hang;
if(tot%m==0)hang=tot/m;
else hang=tot/m+1;
int lie=tot%m;
if(lie==0)lie=m;
a[hang][lie]=i; // 将数字i放入矩阵的当前位置
dfs(dep+1); // 递归填充下一个位置
a[hang][lie]=-1; // 回溯:恢复矩阵的当前位置
tot--; // 回溯:恢复tot
vis[i]=0; // 回溯:恢复数字i的标记
}
}

}
int main()
{
int t;
cin >>t;
while(t--)
{
cin >>n>>m;
dfs(1);
}
}

打表出来我们发现一个规律

如果是奇数行那不动,偶数行循环右移一位即可(不要问我循环右移是什么)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define asd(i,a,b) for(int i=a;i<=b;i++)
#define des(i,a,b) for(int i=a;i>=b;i--)
const int N=2e5+7,inf=2e18,mod=998244353;

void solve(){
int n,m;cin>>n>>m;
cout<<"Yes"<<endl;
if(m&1){
int cnt=0;
asd(i,1,n){
if(i&1){
asd(j,1,m)cout<<++cnt<<" ";
}else{
int tmp=++cnt;

asd(j,2,m)cout<<++cnt<<" ";
cout<<tmp<<" ";
}
cout<<endl;
}
}else{
int cnt=0;
asd(i,1,n){
asd(j,1,m)cout<<++cnt<<" ";
cout<<endl;
}
}

}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
return 0;
}

附上代码。