这题一定要把计算方式绕出来,, 就是将分数、特征值表示出来,然后熟悉流程
而且题目有问题,根本就不用取绝对值
并且max不能取模!!!
而且最大和连续子序列不是读到负数就断开、而是一直加和,,和变成负数再变为0(←弱)
这题折腾半天太不应该了
码:
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long g,f,a[1000001],n,p,max1,i,x,ff,ans=-9999999999;
bool woc;
int main()
{ f=-10000000000;
scanf("%lld%lld",&n,&p);
for(i=1;i<=n;i++)
{
scanf("%lld",&x);
if(g<0)g=0;
g+=x;
f=max(f,g);
a[i]=f;
// cout<<a[i]<<" ";
}
max1=a[1];ans=max1;max1+=a[1];//max1 统计特征值
for(i=2;i<n;i++)
{//cout<<max1<<" ";
if(a[i]<0)
{ //f=0
}else
{
max1=max1+a[i];
if(max1>=ans)woc=1;
if(woc)max1%=p;
}
}
if(woc)cout<<max1;
else cout<<ans%p;
}