对拍

终于算是学习了一下对拍了,下面以快速幂作为例子简单说一下对拍的流程。

baoli.cpp

比如写一个 \(O(n)\) 的暴力求幂程序:

#include<bits/stdc++.h>

#define G ch=getchar()

typedef long long ll;

namespace IO
{
    char buf[1<<15],*fs,*ft;
    inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1, G;
        while (!isdigit(ch) && ch^'-') G;
        if (ch=='-') f=-1, G;
        while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), G;
        x*=f;
    }

    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::read;
using IO::write;

int main()
{
    ll a, b, mod, ans=1; read(a), read(b), read(mod);
    for (ll i=1; i<=b; ++i) ans=ans*a%mod;
    write(ans,'\n');
    IO::flush();
    return 0;
}

std.cpp

然后总会想去搞一下正解,写一个正解程序:

#include<bits/stdc++.h>

#define G ch=getchar()

typedef long long ll;

namespace IO
{
    char buf[1<<15],*fs,*ft;
    inline char getc() { return (ft==fs&&(ft=(fs=buf)+fread(buf,1,1<<15,stdin),ft==fs))?0:*fs++; }
    template<typename T>inline void read(T &x)
    {
        x=0;
        T f=1, G;
        while (!isdigit(ch) && ch^'-') G;
        if (ch=='-') f=-1, G;
        while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), G;
        x*=f;
    }

    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::read;
using IO::write;

inline ll Quick_power(ll a,ll b,ll mod)
{
    ll ans=1;
    while (b)
    {
        if (b&1) ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main()
{
    ll a, b, mod;
    read(a), read(b), read(mod);
    write(Quick_power(a,b,mod),'\n');
    IO::flush();
    return 0;
}

data.cpp

当然,最重要的就是对于数据的生成了:

#include<bits/stdc++.h>

typedef long long ll;
const ll mod=1e9+7;

namespace IO
{
    char Out[1<<24],*fe=Out;
    inline void flush() { fwrite(Out,1,fe-Out,stdout); fe=Out; }
    template<typename T>inline void write(T x,char str)
    {
        if (!x) *fe++=48;
        if (x<0) *fe++='-', x=-x;
        T num=0, ch[20];
        while (x) ch[++num]=x%10+48, x/=10;
        while (num) *fe++=ch[num--];
        *fe++=str;
    }
}

using IO::write;

int main()
{
    srand(time(0));
    ll a=rand()%mod+1, b=rand()%mod+1, c=rand()%mod+1;
    write(a,' '), write(b,' '), write(c,'\n');
    IO::flush();
    return 0;
}

改开 long long 一定要开啊。

exe.cpp

下面就该写对拍程序了。

下面有两种方法:

第一种:

写一个 exe.cpp :

#include<bits/stdc++.h>

int main()
{
    int t=200;
    for ( ; ; )
    {
        --t;
        system("data.exe > data.txt");
        system("baoli.exe < data.txt > baoli.txt");
        system("std.exe < data.txt > std.txt");
        if (system("fc std.txt baoli.txt")) break;
    }
    if (!t) puts("no error");
    else puts("error");
    system("pause");
    return 0;
}

第二种方法:

写个 txt ,然后把它改成 bat :

:again
data.exe > data.in
baoli.exe < data.in > baoli.out
std.exe < data.in > std.out
fc std.out baoli.out
if not errorlevel 1 goto again
pause

第二种比第一种快很多。

参考资料:资料

转载于:https://www.cnblogs.com/G-hsm/p/dui_pai.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值