#include <ctime>
int start = clock();
if(clock() - start >= CLOCKS_PER_SEC * 0.9)
{
cout << ... << endl;
exit(0);
}
clock()函数很慢, 为了减少clock()的次数(控制在几百次可能),前面加上某个条件减少次数 如 i % 1000000 == 0
使用常量CLOCKS_PER_SEC
,因为不同系统下可能常量不同
不要忘了压缩路径 fa[x] = t;
int get_fa(int x)
{
if(fa[x] == x) return fa[x];
int t = get_fa(fa[x]);
return fa[x] = t;
}
用到的是[0, N - 1];
int hh = 0, tt = 0;
// 加入
q[tt ++ ] = i;
if (tt == N) tt = 0;
// 弹出
int t = q[hh ++ ];
if (hh == N) hh = 0;
先写非高精,再改为高精。
void add(LL a[], LL b[])
{
static LL c[M];
memset(c, 0, sizeof c);
for(int i = 0, t = 0; i < M; i ++)
{
t += a[i] + b[i];
c[i] = t % 10;
t /= 10;
}
memcpy(a, c, sizeof c);
}
void mul(LL a[], LL b)
{
static LL c[M];
memset(c, 0, sizeof c);
LL t = 0;
for(int i = 0; i < M; i ++)
{
t += a[i] * b;
c[i] = t % 10;
t /= 10;
}
memcpy(a, c, sizeof c);
}
int cmp(LL a[], LL b[])
{
for(int i = M - 1; i >= 0; i --)
if(a[i] > b[i]) return 1;
else if(a[i] < b[i]) return -1;
return 0;
}
void print(LL a[])
{
int k = M - 1;
while(k && !a[k]) k --;
while(k >= 0) printf("%lld", a[k --]);
puts("");
}
原文:https://www.cnblogs.com/RemnantDreammm/p/14827639.html