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

 

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倍的差别啊啊啊啊啊

相关文章
相关标签/搜索