终于算是学习了一下对拍了,下面以快速幂作为例子简单说一下对拍的流程。
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
第二种比第一种快很多。
参考资料:资料 。