题解:P8790 [蓝桥杯 2022 国 C] 填空问题
bayiran
·
·
题解
试题 A
直接暴力会慢到爆炸(202202011200 项),考虑找规律,打出前 200 项的末位,发现以 60 项为循环,循环节为
1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5 6 1 7 8 5 3 8 1 9 0 9 9 8 7 5 2 7 9 6 5 1 6 7 3 0 3 3 6 9 5 4 9 3 2 5 7 2 9 1 0
斐波那契代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int re[250] = {0};
int f(int n){
if(re[n]!=0)return re[n];
if(n<=2)return re[n]=1;
else return re[n]=f(n-1)+f(n-2);
}
signed main(){
for(int i=1;i<=200;i++){
cout<<f(i)%10/*只用个位*/<<" ";
}
return 0;
}
```
(这个程序运行到八九十向会爆 ull,但找规律够了。)
## 试题 B
$2\times10^6$ 的数据量可以直接使用质数筛,读入后筛出指数有 $342693$ 个。
代码:
```cpp
#include<bits/stdc++.h>
using namespace std;
bool pd(int x){
for(int i=2;i*i<=x;i++){
if(!(x%i)) return 0;
}
return 1;
}
int main(){
long w;
long ans = 0;
for(int i=0;i<2000000;i++){
cin>>w;
ans += pd(w);
}
cout<<ans<<endl;
return 0;
}
```
(建议改用文件读写。)
---
将两个答案套入模板。
```cpp
#include<bits/stdc++.h>
using namespace std;
char pid;
int main() {
cin >> pid;
if(pid == 'A')
puts("26960268160");
else
puts("342693");
return 0;
}
```