1
希望对你有帮助.|

zouyua

园龄:1年9个月粉丝:3关注:3

牛客小白月赛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;
}
复制代码

 

本文作者:zouyua

本文链接:https://www.cnblogs.com/ZouYua/p/17556099.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   zouyua  阅读(17)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起