题解 P8377 [PFOI Round1] 暴龙的火锅
更新日志
[2022/7/16] 更新了核心代码的复杂度
简要题意
定义
另外,定义
现在给定
基本思路
我们看到题目要求的数据范围:
对于
100\% 的数据,满足1\le T\le 10^5,\ 1\le n\le 10^6 。
所以,我们首先想到的应该是一道找规律或者找通项公式的题,使用
因为:
我们过列举发现对于
spa[24] = {0, 1, 1, 2, 3, 5, 8, 4, 3, 7, 1, 8, 0, 8, 8, 7, 6, 4, 1, 5, 6, 2, 8, 1};
而且,我们发现
\text{std}_1 :
#include <bits/stdc++.h>
using namespace std;
int n, data;
const int spa[] = {0, 1, 1, 2, 3, 5, 8, 4, 3, 7, 1, 8, 0, 8, 8, 7, 6, 4, 1, 5, 6, 2, 8, 1, 0};
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d", &data);
int a = data % 24, ans = spa[a];
while(a--) ans = (ans + spa[a]) % 9;
printf("%d\n", ans);
}
return 0;
}
当然,我们也可以用 while 循环了。
\text{std}_2 :
#include <cstdio>
//#define scanf scanf_s
using namespace std;
int n, data;
//const int spa[] = {0, 1, 1, 2, 3, 5, 8, 4, 3, 7, 1, 8, 0, 8, 8, 7, 6, 4, 1, 5, 6, 2, 8, 1, 0};
const int s[] = {0, 1, 2, 4, 7, 3, 2, 6, 0, 7, 8, 7, 7, 6, 5, 3, 0, 4, 5, 1, 7, 0, 8, 0, 0};
int main() {
scanf("%d", &n);
while (n--) {
scanf("%d", &data);
printf("%d\n", s[data % 24]);//核心代码O(1)
}
return 0;
}