2 条题解
-
0
暴力超时版:
#include <bits/stdc++.h> using namespace std; typedef unsigned long long LL; const int N = 1e8 + 10; LL f[N]; int ck(int x) { int res = 0; while (x > 0) res += x % 10, x /= 10; return res; } int main() { f[0] = 0; for (int i = 1; i <= N - 10; i++) { if (i % 10 == 0) f[i] = ck(i); else f[i] = f[i - 1] + 1; } for (int i = 1; i <= N - 10; i++) f[i] += f[i - 1]; int T; scanf("%d", &T); while (T--) { int x; scanf("%d", &x); printf("%llu\n", f[x]); } return 0; }标程:
#include <bits/stdc++.h> using namespace std; typedef long long LL; const int N = 1e8 + 10; LL d[9]; int main() { d[0] = 0; d[1] = 45; for (int i = 2, p = 10; i <= 8; i++, p *= 10) d[i] = d[i - 1] * 10 + 45 * p; int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); LL ans = 0; int p = 1, n1 = n, n2 = 0, t = 0; while (n1 > 0) { int x = n1 % 10; n1 /= 10; ans += x * (n2 + 1); ans += x * d[t] + (x - 1) * x / 2 * p; n2 = x * p + n2; p = p * 10; t++; } printf("%lld\n", ans); } return 0; } -
0
暴力超时版:
#include<bits/stdc++.h> using namespace std; typedef unsigned long long LL; const int N=1e8+10; LL f[N]; int ck(int x){int res=0;while(x>0)res+=x%10,x/=10;return res;} int main() { f[0]=0; for(int i=1;i<=N-10;i++) { if(i%10==0)f[i]=ck(i); else f[i]=f[i-1]+1; } for(int i=1;i<=N-10;i++)f[i]+=f[i-1]; int T;scanf("%d",&T); while(T--) { int x;scanf("%d",&x); printf("%llu\n",f[x]); } return 0; }
标程:#include<bits/stdc++.h> using namespace std; typedef long long LL; const int N=1e8+10; LL d[9]; int main() { d[0]=0;d[1]=45;for(int i=2,p=10;i<=8;i++,p*=10)d[i]=d[i-1]*10+45*p; int T;scanf("%d",&T); while(T--) { int n;scanf("%d",&n); LL ans=0; int p=1,n1=n,n2=0,t=0; while(n1>0) { int x=n1%10;n1/=10;ans+=x*(n2+1); ans+=x*d[t]+(x-1)*x/2*p; n2=x*p+n2; p=p*10; t++; } printf("%lld\n",ans); } return 0;}
</p>
- 1
信息
- ID
- 26
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 7
- 标签
- 递交数
- 323
- 已通过
- 85
- 上传者