SP34011 JGTLE - Jalil Got TLE
题面
给您一个程序,请降低他的复杂度。
#include <stdio.h>
int main() {
int t;
scanf("%d", &t);
for(int tc = 1; tc <= t; ++tc) {
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
long long result = 0;
for(int i = 1; i <= a; ++i) {
for(int j = 1; j <= b; ++j) {
for(int k = 1; k <= c; ++k) {
result += j * k;
}
}
}
printf("Case %d: %lld\n", tc, result);
}
return 0;
}
前置知识
- 加法、乘法
- 等差数列求和
思路
这道题其实就是求:
\[\sum_{i=1}^{a}{\sum_{j=1}^{b}{\sum_{k=1}^{c}{jk}}}
\]
一步一步化简(主要是不想打那么多的\(LATEX\)),答案是:
\[(b(b+1) \div 2 \times c(1+c) \div 2) \times a
\]
注意 \(a\) 一定要最后 \(\times\),要不然可能会溢出。
代码
#include <stdio.h>
#define int long long
signed main() {
int t;
scanf("%lld", &t);
for(int tc = 1; tc <= t; ++tc) {
int a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
int result = 0;
result=(1+b)*b/2*(1+c)*c/2;
result*=a;
printf("Case %lld: %lld\n", tc, result);
}
return 0;
}

浙公网安备 33010602011771号