four
7月10日
sequence|sequence.in|sequence.out
?
題目描述:
給定一個(gè)整數(shù)K和長(zhǎng)為N的數(shù)列{Ai},求有多少個(gè)子串(不含空串)的和為K的倍數(shù)。(在這里子串表示{A[i]..A[j]},i<=j)
?
輸入格式:
共兩行
第一行 兩個(gè)整數(shù)N,K
第二行 N個(gè)整數(shù)表示數(shù)列{Ai}
?
輸出格式:
一行一個(gè)整數(shù) 表示滿足條件的子串的個(gè)數(shù)
?
樣例輸入:
6 3
1 2 6 3 7 4
?
樣例輸出:
7
?
數(shù)據(jù)范圍:
20%??? N<=100
對(duì)另外20%? K<=100
對(duì)所有數(shù)據(jù)? N<=500000? K<=500000
保證輸入數(shù)據(jù)中所有數(shù)都能用longint保存
?
#include<cstdio> #include<cstring> #include<iostream> #define PROC "sequence" using namespace std; const int MAXN = 500000+5; int x,n,k; int a[MAXN],rest[MAXN]; long long ans = 0; int main() { ??? scanf("%d%d",&n,&k); ??? a[0] = 0; ??????? for (int i = 1;i<=n;i++) ???? { ?????????????????????? scanf("%d",&x); ?????????????????????? int yushu = (x % k+k) %k; ?????????????????????? a[i] = (yushu + a[i-1]%k+k) % k; ??????? } ??????? for (int i = 1;i<=n;i++) ??????? ? rest[a[i]]++; ??????? ans += rest[0]; ???? long long t; ??????? for (int i = 0;i<k;i++) ?????????????? ? if (rest[i]!=0 && rest[i]!=1) ?????????????? ? { ?????????????? ?? t = rest[i]; ?????????????? ?? ans += (t * (t-1)/2); ?????????????? ? } ??????? cout<<ans; ??????? return 0; }錯(cuò)因:
1.正解:余數(shù) 前綴和組合
做:反應(yīng)較快 5分鐘
改:沒有注意到500000的平方會(huì)超longint
得:注意數(shù)據(jù)規(guī)模 especially 負(fù)數(shù) 和 計(jì)算過程中產(chǎn)生的大數(shù)據(jù)
2.未完待續(xù)
3.未完待續(xù)
s��ln�/ `��le='font-size:10.5pt;font-family:"Courier New";color:darkred;mso-ansi-language: FR'>()
{ ??????? ??????? scanf("%d",&n); ??????? for (int i = 1; i<=n; i++) ??????? scanf("%d",&b[i]); ??? sort(b+1,b+n+1); ??? int count = 0,now = b[1]; ??? a[++count] = now; ??? int ans = 0; ??????? for (int i = 2;i<=n;i++) ??????? { ???? if (b[i]!= now) { ?????????????????????? now = b[i]; ?????????????????????? a[++count] = now; ???? } ???? else ans ++; ??? } ???? int max = a[1]; ???? memset(dp, 0x3f3f,sizeof(dp)); ???? for (int i = 1;i<=count;i++) ???? { ??????? dp[a[i]] = 1; ??????? if (a[i]>max)? max = a[i]; ??????? } ??????? for (int i = 1;i<=count;i++) ??????? ? for (int j = 1;j<=max;j++) ??????? ?? { ?????????????????????? int cur = gcd(a[i],j); ?????????????????????? if (dp[cur]>dp[j]+1) dp[cur] = dp[j] + 1; ??????? ?? } ??????? int g = a[1]; ??????? for (int i =2;i<=count;i++) ??????? ? g = gcd(g,a[i]); ??????? printf("%d",count-dp[g]+ans); ??????? return 0; }錯(cuò)因:
1.正解:DP: dp[i]表示使得最大公約數(shù)是i最小使用多少數(shù)。
???????????? 對(duì)于每個(gè)i枚舉所有數(shù),進(jìn)行更新。
理由:1.gcd滿足“交換律結(jié)合律”。
????? 2. 更新結(jié)果與搜索順序無關(guān)。
做:審題:誤把gcd當(dāng)成了最大公因數(shù)。
改:no problem
得:縝密審題? 動(dòng)態(tài)思維
2.未完待續(xù)
3.未完待續(xù)
轉(zhuǎn)載于:https://www.cnblogs.com/rubylan/p/3836801.html
總結(jié)
- 上一篇: 数学图形(1.21)蚌线
- 下一篇: 通过微软的cors类库,让ASP.NET