four
7月10日
sequence|sequence.in|sequence.out
?
題目描述:
給定一個整數K和長為N的數列{Ai},求有多少個子串(不含空串)的和為K的倍數。(在這里子串表示{A[i]..A[j]},i<=j)
?
輸入格式:
共兩行
第一行 兩個整數N,K
第二行 N個整數表示數列{Ai}
?
輸出格式:
一行一個整數 表示滿足條件的子串的個數
?
樣例輸入:
6 3
1 2 6 3 7 4
?
樣例輸出:
7
?
數據范圍:
20%??? N<=100
對另外20%? K<=100
對所有數據? N<=500000? K<=500000
保證輸入數據中所有數都能用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; }錯因:
1.正解:余數 前綴和組合
做:反應較快 5分鐘
改:沒有注意到500000的平方會超longint
得:注意數據規模 especially 負數 和 計算過程中產生的大數據
2.未完待續
3.未完待續
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; }錯因:
1.正解:DP: dp[i]表示使得最大公約數是i最小使用多少數。
???????????? 對于每個i枚舉所有數,進行更新。
理由:1.gcd滿足“交換律結合律”。
????? 2. 更新結果與搜索順序無關。
做:審題:誤把gcd當成了最大公因數。
改:no problem
得:縝密審題? 動態思維
2.未完待續
3.未完待續
轉載于:https://www.cnblogs.com/rubylan/p/3836801.html
總結
- 上一篇: 数学图形(1.21)蚌线
- 下一篇: 通过微软的cors类库,让ASP.NET