从一到数论题引发的思考(原来平常的习惯对程序的运行时间竟然有如此的的影响)

https://www.luogu.org/problemnew/show/P1134

先贴一贴战果

14分代码

#include <bits/stdc++.h>
#define inf 0x7f7f
using namespace std;
long long n,m,ans,cot=1;
void jc(int x){
    cot=1;
    for(int i=1;i<=x;i++){
        cot*=i%10000000;
        while(cot%10==0) cot/=10;
    
    }

}
int main(){
ios::sync_with_stdio(0);
cin>>n;
jc(n);
while(cot%10==0) cot/=10;
cout<<cot%10;
return 0;
}

好咱们在看四十几分的

#include <bits/stdc++.h>
#define inf 0x7f7f
using namespace std;
long long n,m,ans,cot=1;
int main(){
ios::sync_with_stdio(0);
scanf("%d",&n);
    cot=1;
    for(long long i=1;i<=n;i++){
        while(cot%10==0) cot/=10 ;
        cot=cot*i%1000000;
    }
       
long long x=cot;
while(1){
     if(x%10!=0)
        {
       printf("%d",x%10);
        return 0;
        }
        x=x/10;
    }
return 0;
}
 

嗯,好像与上面差不多啊,只不过一个用函数一个不用函数啊,哇,怎么少了一半啊时间。。。。。

咱们先别慌,在看ac代码

#include <bits/stdc++.h>
#define inf 0x7f7f
using namespace std;
long long int n,m,ans,cot=1;

int main(){
ios::sync_with_stdio(0);
scanf("%d",&n);
    cot=1;
    for(long long i=1;i<=n;i++){
        cot=cot*i;
        while(cot%10==0) cot/=10    ;
        cot%=1000000;

    }
        
cout<<cot%10;

return 0;
}

这不是一模一样啊。。。。。。

咦 cot*=i%1000000不是跟cot=cot*i,cot%=1000000一样吗,怎么会这样,评测机bug了???

然而事实就是如此:

怎么可能少了12倍,为什么啊啊啊啊啊啊啊啊啊啊啊!!!!!1

由此可知,同是暴力,写题习惯竟会make so large difference。。。。20倍的差异啊啊啊啊啊

原绿色洒满天际
原文地址:https://www.cnblogs.com/sc-pyt-2021-theworld/p/10350482.html