從一到數論題引起的思考(原來日常的習慣對程序的運行時間居然有如此的的影響)

 

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

先貼一貼戰果c++

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;
}

 

好我們在看四十幾分的spa

#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;
}
 

嗯,好像與上面差很少啊,只不過一個用函數一個不用函數啊,哇,怎麼少了一半啊時間。。。。。code

 

我們先別慌,在看ac代碼blog

#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;
}

這不是如出一轍啊。。。。。。ci

咦 cot*=i%1000000不是跟cot=cot*i,cot%=1000000同樣嗎,怎麼會這樣,評測機bug了???it

然而事實就是如此:io

怎麼可能少了12倍,爲何啊啊啊啊啊啊啊啊啊啊啊!!!!!1class

由此可知,同是暴力,寫題習慣竟會make so large difference。。。。20倍的差別啊啊啊啊啊

相關文章
相關標籤/搜索