考试的时候,各种各样的玄学操作层出不穷,各种卡常时有发生,这个时候就需要用程序执行的时间来强制性的结束程序,以防止超过时间限制,(当然答案也很有可能不对)
所以,代码如下:
printf("time used=%.3fs\n",double(clock()-s)/CLOCKS_PER_SEC);
当然还有头文件
#include<time.h>
就拿模逆元当中的操作来看吧:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<time.h>
using namespace std;
typedef long long ll;
ll qpow(ll a,ll b,ll c)
{
ll ans=1;
while(b)
{
if(b&1)
ans=ans*a%c;
a=a*a%c;
b>>=1;
}
return ans;
}
int main()
{
freopen("51.in","r",stdin);
freopen("51.out","w",stdout);
double s=clock();
ll n,p;
scanf("%d %d",&n,&p);
for(int i=1;i<=n;i++)
{
if(double(clock()-s)/CLOCKS_PER_SEC>=0.90)//这便是玄学的结束程序
break;
printf("%lld\n",qpow(i,p-2,p));
}
printf("time used=%.3fs\n",double(clock()-s)/CLOCKS_PER_SEC);
return 0;
}
记住这里的 if(double(clock()-s)/CLOCKS_PER_SEC>=0.90)
这里的0.90可以根据实际需要考虑,不要设的太大,因为有误差,所以最好比限制时间小10%左右...
顺便盗得一手好图: