对拍

· · 个人记录

闲话

在考场上,正解只能过样例,暴力才能出奇迹。这种事情频频发生,所以要发挥好对拍的重要性。

步骤

就拿 P7960 [NOIP2021] 报数 做个例子。

首先写好咱们的正解,命名为 std.cpp (考场上肯定写规定的程序名):

#include<bits/stdc++.h>
using namespace std;
const int SX=2e7;
int t,jl[SX],nex[SX];
bool vis[SX];
void pre(){
    int cnt=0;
    for(int i=1;i<=SX;i++){
        int ls=i;
        bool flag=false;
        while(ls){
            int x=ls%10;
            ls/=10;
            if(x==7){
                flag=true;break;
            }
        }
        if(flag)cnt++,jl[cnt]=i;
    }
    for(int i=1;i<=cnt;i++){
        for(int j=1;j*jl[i]<=SX;j++){
            vis[j*jl[i]]=true;
        }
    }
    int last=0;
    for(int i=1;i<=SX;i++){
        if(!vis[i]){
            nex[last]=i;
            last=i;
        }
    }
}
void solve(){
    int x;
    scanf("%d",&x);
    if(vis[x])printf("-1\n");
    else printf("%d\n",nex[x]);
}
int main(){
    freopen("date.out","r",stdin);
    freopen("std.out","w",stdout);
    pre();
    scanf("%d",&t);
    while(t--)solve();
}

暴力可以出奇迹,命名为 baoli.cpp(拼音挺好用的): 这里搬的题解。

#include <bits/stdc++.h>
using namespace std;
inline int read()//读入优化
{
    int x = 0, f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        f = ch != '-';
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return f ? x : -x;
}
inline void write(int x)//输出优化
{
    if (x >= 10)
        write(x / 10);
    putchar(x % 10 + 48);
}
const int N = 1e7 + 100;
int T, x, ls;
int f[N], nx[N];
bool check(int x)//判断是否含有数字7
{
    while (x)
    {
        if (x % 10 == 7)
            return 1;
        x /= 10;
    }
    return 0;
}
void init()//预处理部分
{
    for (int i = 1; i <= N - 10; i++)
    {
        if (f[i])//如果被标记过,就跳过
            continue;
        if (check(i))//如果含有数字7,标记其倍数
        {
            for (int j = i; j <= N - 10; j += i)
                f[j] = 1;
            continue;
        }
        nx[ls] = i;//记录i
        ls = i;//更新ls
    }
}
int main()
{
    freopen("date.out","r",stdin);
    freopen("baoli.out","w",stdout);
    init();//先预处理
    T = read();
    while (T--)
    {
        x = read();
        if (f[x])//被标记了输出-1,否则输出nx
            puts("-1");
        else
            write(nx[x]), putchar('\n');
    }
    return 0;
}

最重要的一步,就是造数据,命名为 date.cpp(虽然这不是数据的英文,但只能将错就错):

#include<bits/stdc++.h>
using namespace std;
int main(){
    freopen("date.out","w",stdout);
    int t=rand()%10000+1;
    cout<<t<<endl;
    for(int i=1;i<=t;i++){
        int x=rand()%100000+1;
        cout<<x<<endl;
    }
    return 0;
}

好的,最后一步,拍它:

#include<bits/stdc++.h>
using namespace std;
int t=10000,tot;
int main(){
    system("g++ std.cpp -o std");
    system("g++ date.cpp -o date");
    system("g++ baoli.cpp -o baoli");
    while(t--){
        tot++;
        cout<<tot<<" ";
        system("./date;./std;./baoli");
        if(system("diff -w std.out baoli.out")){
            cout<<"WA"<<endl;return 0;
        }
        else cout<<"AC"<<endl;
    }
}

Detail