首页 > 其他 > 详细

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

时间:2019-02-03 16:17:52      阅读:174      评论:0      收藏:0      [点我收藏+]

 

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!