icpc:problems:919c_partitioning_the_array
problems | |
---|---|
名称 | Partitioning the Array |
题目编号 | 919C |
题目链接 | codeforces.com/… |
来源 | CodeForces |
算法分类 | 数学 |
难易程度 | 一般般 |
Partitioning the Array
想法
划分序列,一种划分能得分为:划分完了之后,对同一个数进行取余,每个划分的子序列变成一样的。
可以发现,划分后的子序列每个对应位置的数同余。所以可以对对应位置的数两两相减,余下的数一定是$m$的倍数,对所有对应的数相减的数取gcd,这个gcd一定是$m$。
所以变成了遍历n个数,对划分对应的下一个数相减求绝对值,所有的数求gcd,当gcd不为1时说明$m$存在。如$m$存在,则说明该划分得分。
代码实现
#include<cstdio> #include<cmath> #include<algorithm> #include<numeric> using namespace std; const int N = 1e5*2+10; int line[N]; void solve() { int n, ans = 0; scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &line[i]); for(int i=1; i<=n; i++) { if(n % i != 0)continue; int g = 0; for(int j=1; j+i<=n;j++) { g = gcd(g, (int)abs(line[j] - line[j+i])); } ans += (g != 1); } printf("%d\n", ans); return ; } int main() { int T; scanf("%d", &T); for(int t=1; t<=T; t++) { solve(); } return 0; }
/app/www/public/data/pages/icpc/problems/919c_partitioning_the_array.txt · 最后更改: 2024/01/18 11:43 由 温婕莺