关于代码常数问题
  • 板块学术版
  • 楼主剑雪清寒
  • 当前回复6
  • 已保存回复6
  • 发布时间2022/11/5 20:38
  • 上次更新2023/10/27 04:09:19
查看原帖
关于代码常数问题
214728
剑雪清寒楼主2022/11/5 20:38

我在摆烂做水题时有这样一题P5087

我的代码如下

#include <bits/stdc++.h>
using namespace std;
inline long long read() {
    long long x;bool f;char ch;
    for(f=0;!isdigit(ch=getchar());f=ch=='-');
    for(x=ch-48;isdigit(ch=getchar());x=x*10+ch-48);
    return f?-x:x;
}
inline void print(long long x,char las) {
    if(!x) {
        putchar(48),putchar(las);
        return ;
    }
    if(x<0) putchar('-'),x=-x;
    int ls[20],k=0;
    while(x) ls[++k]=x%10,x/=10;
    while(k) putchar(ls[k--]+48);
    putchar(las);
    return ;
}
long long f[301];int n=read(),k=read();
const long long p=1e9+7;
int main() {
    f[0]=1;
    for(int i=1,a=read();i<=n;(++i) >n ? 1 : a=read()) for(int j=k;j;j--) (f[j]+=f[j-1]*a)%=p;
    print(f[k],'\n');
    return 0;
}

跑出来的结果是207mslink

某提交记录的代码被我修改后如下

#include<bits/stdc++.h>
#define int long long
using namespace std;
int dp[305],n,k,mod=1e9+7;
signed main()
{
	cin>>n>>k;
	dp[0]=1;
	for(int i=1;i<=n;i++){
	    int a;cin>>a;
		for(int j=k;j;j--){
			dp[j]=(dp[j]+dp[j-1]*a)%mod;
		}
	}
	cout<<dp[k];
	return 0;
}

结果是1.16slink

我的代码在将输入输出改为cin cout后变为大致一致的形态却稳定在200ms至400ms,而第二份代码却始终1s+,甚至原提交中代码在拟古跑了个2.50s

求问为什么差别如此之大

2022/11/5 20:38
加载中...