牛客小白月赛76——D
题目链接:D-MoonLight的运算问题_牛客小白月赛76(重现赛) (nowcoder.com)
错误思路:一开始就对 a[i] + x 和 a[i]] * x 讨论,判断 a[i] 的值了,题目让求x,应该讨论x,题目要求取模,直接判断 x % Mod 的话会出错;
正确思路:当 a[i] != 0 时,讨论X, 当x >= 2 时相乘肯定大于相加,且直接判断 x % Mod 的话会出错,x为k* Mod + x ,题目让求x最大,不是余数最大,所以当x >= 2后,直接相乘,找个变量标记一下就行了。
#include<bits/stdc++.h> using namespace std; using ull = unsigned long long; using ll = long long; using PII = pair<int,int>; #define endl "\n" #define pb push_back const int N=2e5+10; const int INF=0x3f3f3f3f; const int mod = 998244353; ll a[N]; void solve() { int n; cin >> n; ll x = 0; for(int i = 1; i <= n; i ++) cin >> a[i]; int f = 0; for(int i = 1; i <= n; i ++) { if(a[i]) { if(a[i] > 1 && f) x = x * a[i] %mod;//对a[i] 分类讨论 else { if(x + a[i] >= 2) f = 1;//此处(a[i] + x) 不能取模,因为判断的是x >= 2, 不是取模后的结果,比如a[i]=mod + 1; x = (x + a[i]) % mod; } } } cout << x << endl; } int main() { int T; cin>>T; while(T--) { solve(); } return 0; }

浙公网安备 33010602011771号