最大子段和

本文介绍了一种模运算的优化方法,即在计算过程中对中间结果进行取模操作,避免最终结果过大导致的数据溢出问题。这种方法特别适用于大整数运算场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

求模的  都可以在 被求模数的  得到过程中 对 被求模数的 不玩整体  进行求模 , 并且得到的模值 不会改变 . 这样的话 可以解决被 求模的数 过大 计算机无法保存的情况 . 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 #include<limits.h>
15 using namespace std;
16 int main()
17 {
18     __int64 n,a[50005],maxn,tmax;
19     while(scanf("%d",&n)!=EOF)
20     {
21         for(int i=0;i<n;i++)
22             scanf("%I64d",&a[i]);
23         maxn=INT_MIN;
24         tmax=0;
25         for(int i=0;i<n;i++)
26         {
27             if(tmax<0)
28             {
29                 tmax=0;
30                 tmax=a[i];
31             }
32             else
33                 tmax+=a[i];
34             if(maxn<tmax)
35                 maxn=tmax;
36         }
37         printf("%I64d\n",maxn);
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/A-FM/p/5577903.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值