【連續子段和被某數整除】SOJ 2293: The Longest SubSequence
You will be given a sequence consists of many numbers, your task is to calculate the length of the longest consecutive subsequence , the sum of which can be divided by another given number.
Input
There are multiple test cases.
The first line of each case contains two integer N and M, (0< N <= 100000, 0 < M < 10000),then followed by a line consists of N integers a1,a2,...an,
( -100000000 <= a1,a2,...an <= 100000000 ).
Output
For each test case,Output the length of the longest consecutive subsequence , the sum of which can be divided by M.
Sample Input
2 3 1 6 3 3 2 3 6 2 5 1 3
Sample Output
1 2 0
題目重述:求長的連續子段使其子段和能被某數整除
分析:列舉所有子段和最壞情況要O(n^2)的時間 必然超時
考慮到若用一個sum求前n個數的和 sum%m的值如果在前面出現過 則說明這一段可以整除m
因此用一個vis陣列儲存餘數為x的出現下標 當vis[x]被訪問過 則說明i-vis[x]這一段能被整除
程式碼如下
#include <cstring> #include <cstdio> const int maxN=100000+50; int Sum; int vis[maxN]; int main() { int N,M,i,temp,id,ans; while(scanf("%d%d",&N,&M)==2) { memset(vis,-1,sizeof(vis)); Sum=0; vis[0]=0; ans=0; for (i=1;i<=N;i++) { scanf("%d",&temp); Sum+=temp; id=Sum%M; if (id<0) { id+=M; } if (vis[id]==-1) { vis[id]=i; } else { temp=i-vis[id]; if (ans<temp) { ans=temp; } } } printf("%d\n",ans); } return 0; }
相關推薦
【連續子段和被某數整除】SOJ 2293: The Longest SubSequence
You will be given a sequence consists of many numbers, your task is to calculate the length of the longest consecutive subsequence , the
【bzoj5089】最大連續子段和 分塊+單調棧
我們 如果 一條直線 時間復雜度 支持 led 包括 每一個 a + b 題目描述 給出一個長度為 n 的序列,要求支持如下兩種操作: A l r x :將 [l,r] 區間內的所有數加上 x ; Q l r : 詢問 [l,r] 區間的最大連續子段和。
最大連續子段和
img ans define open space sed 表示 max get 最大連續子段和sum表示以當前數a[i]結尾的最大子段和,如果sum<0,那麽它對後面就沒有積極作用,不如拋棄。所以sum+=a[i]維護最大值sum=max(sum,0) 1 #
BZOJ5089 最大連續子段和(分塊)
urn def num freopen ring div fine 正數 ear 假設所有操作都是對整個序列的。考慮每個子區間,區間和與其被加的值構成一次函數關系。最大子段和相當於多個子區間取最大值,答案顯然就在這些一次函數構成的下凸殼上。如果預處理出凸殼,只要在凸殼上
【HDU 1024】 Max Sum Plus Plus【動態規劃求最大M子段和詳解-好題 】
Now I think you have got an AC in Ignatius.L‘s "Max Sum" problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you a
[分治] 洛谷P1115 最大連續子段和(分治典例)
題目 題目描述 給出一段序列,選出其中連續且非空的一段使得這段和最大。 輸入輸出格式 輸入格式: 輸入檔案maxsum1.in的第一行是一個正整數N,表示了序列的長度。 第2行包含N個絕對值不大於10000的整數A[i],描述了這段序列。 輸出格式
Max Sum Plus Plus HDU1024 ( 動態規劃 多段連續子段和的最大值)
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Description Now I think you
HDU 1231 最大連續子序列(最大連續子段和)
Description 求最大連續子段和,並輸出此欄位的起始位置和終止位置的值 Input 多組用例,每組用例第一行為序列長度n,第二行n個整數表示該序列,以n=0結束輸入 Output 對每
51nod 1050 循環數組最大子段和【環形DP/最大子段和/正難則反】
pre 不但 spa 個數 ace lld 時間 lin bsp 1050 循環數組最大子段和 基準時間限制:1 秒 空間限制:131072 KB 分值: 10 難度:2級算法題 收藏 關註 N個整數組成的循環序列a[1],a[2
51nod-1065 最小正子段和 【貪心 + 思維】
記錄 n) 51nod nta cstring pla CA 找到 class N個整數組成的序列a[1],a[2],a[3],…,a[n],從中選出一個子序列(a[i],a[i+1],…a[j]),使這個子序列的和>0,並且這個和是所有和>0的
【51nod 1065】【貪心+字首和】最小正子段和【最小正子串和】
思路: 可以參見夾克老爺的回覆的那個帖子:http://bbs.csdn.net/topics/370106383。 對於這類連續序列的問題可以先求出字首和,對於每個位置求某個位置到當前位置和大於1
1007 Maximum Subsequence Sum(25 分)【最大連續子序列和】
題意:求最大連續子序列和並記錄該序列的頭尾元素 #include <bits/stdc++.h> using namespace std; int N; int main() { cin>>N; vector<int>
【DP經典系列】最大連續子序列和
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers
【資料結構與演算法】小於等於k的最大連續子序列和
使用Kadane演算法可以在On內得到最大連續子序列和。如果要求得到和不超過k,那麼該如何解決? 首先要看能不能繼續使用Kadane演算法。答案是不能。回顧Kadane,Kadane中使用dp[i]存最後一個元素是array[i]的最大和,然後所有dp[i]的最大值。 那麼
51nod 1275 連續子段的差異
dash 什麽 splay display 最小值 當我 ace gif isp 分析: 1、首先是尺取,尺取到每一個區間,區間滿足這個條件,最大-最小<=k; 2、對於一個動態區間,怎麽維護他的最大值,最小值(的下標);——單調
51nod 1052 最大M子段和(動態規劃)
表示 image cnblogs class () main png 分享 == 分析:記dp[x][s][1]為從第x個數開始,剩余s段可以分,1表示x跟上一段連著,0表示不連著,遞推式為dp[x][s][1]=max{dp[x+1][s][1]+a[x],dp[x+1
hdu 1024 Max Sum Plus Plus (子段和最大問題)
got mil 計算 itl tex seq panel gin enter Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (
(4)C語言——求最大連續子序列和
log spa clas 最大連續子序列和 alloc 最大 code max 連續 題目: 輸入一組整數,求出這組數字子序列和中最大值。也就是只要求出最大子序列的和,不必求出最大的那個序列。例如: 序列:-2 11 -4 13 -5 -2,則最大子序列和為20。 序列:-
最大連續子序列和
運行時 介紹 最大連續子序列和 () vector 運行 n) else 連續子序列和 下面介紹一個線性的算法,這個算法是許多聰明算法的典型:運行時間是明顯的,但是正確性則很不明顯(不容易理解)。 //線性的算法O(N) long maxSubSum4(const vec
最小正子段和 貪心
復雜度 max 限制 需要 16px size typedef urn span 最小正子段和 基準時間限制:1 秒 空間限制:131072 KB N個整數組成的序列a[1],a[2],a[3],…,a[n],從中選出一個子序列(a[i],a[i+1],…a[