当前位置: 首页 > news >正文

【算法】Partitioning the Array(数论)

题目

Allen has an array a1,a2,…,an. For every positive integer k that is a divisor of n, Allen does the following:

  • He partitions the array into n/k disjoint subarrays of length k. In other words, he partitions the array into the following subarrays:

    [a1,a2,…,ak],[ak+1,ak+2,…,a2k],…,[an−k+1,an−k+2,…,an]

  • Allen earns one point if there exists some positive integer m (m≥2) such that if he replaces every element in the array with its remainder when divided by m, then all subarrays will be identical.

Help Allen find the number of points he will earn.

================================================================

Allen 有一个数组 a1,a2,…,an。对于每一个能被 n 整除的正整数 k,艾伦都会做如下运算:

  • 他将数组划分为长度为 k 的 n/k 个互不相交的子数组:

    [a1,a2,…,ak],[ak+1,ak+2,…,a2k],…,[an−k+1,an−k+2,…,an]

  • 如果存在某个正整数 m (m≥2),使得如果他把数组中的每个元素都替换成除以 m 后的余数,那么所有的子数组都是相同的,Allen 就可以得到一分。

帮助艾伦找出他将获得的分数。

Input

Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (1≤n≤2⋅10^5) — the length of the array a.

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — the elements of the array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

输入

每个测试由多个测试用例组成。第一行包含一个整数 t(1≤t≤104)–测试用例数。测试用例说明如下。

每个测试用例的第一行包含一个整数 n(1≤n≤2⋅105)–数组 a 的长度。

每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)–数组 a 的元素。

保证所有测试用例中 n 的总和不超过 2⋅10^5。

Output

For each test case, output a single integer — the number of points Allen will earn.

输出

对于每个测试用例,输出一个整数 - 艾伦将获得的分数。

思路

本题用到一个概念:如果m为|x-y|的因数,则x % m == y % m.
将数组划分为长度相等的 i 段,将所有的数全部模m之后,所有数组相同。例如当m = 1的时候每个数组中的数均为0,(当然题目中要求m != 0,这里只是做个假设)可以得到一分。
若 n % k == 0,将数组分成了k段
则原数组中m 必须为 abs(h[ i ] - h[ i + k])的因数.

在这里插入图片描述

代码

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n;
int h[N];int gcd(int a, int b)  // 欧几里得算法
{return b ? gcd(b, a % b) : a;
}void solve()
{cin >> n;int ans = 0;for(int i = 1; i <= n; i ++) cin >> h[i];for(int i = 1; i <= n; i ++){if(n % i == 0){int m = 0;for(int k = 1; k + i <= n; k ++){m = gcd(m,abs(h[i + k] - h[k]));}ans += (m != 1);}}cout << ans << endl;
}int main()
{int t;cin >> t;while(t --)solve();return 0;
}

题目来自:Partitioning the Array

http://www.lryc.cn/news/291268.html

相关文章:

  • ASP.NET Core 7 Web 使用Session
  • (1)SpringBoot学习——芋道源码
  • 宏景eHR FrCodeAddTreeServlet SQL注入漏洞复现
  • STM32——I2C
  • 笔记本从零安装ubuntu server系统+环境配置
  • SQL 快速参考手册
  • Linux/Windows系统无法git clone解决办法
  • 【算法与数据结构】198、213、337LeetCode打家劫舍I, II, III
  • React、React Router、JSX 简单入门快速上手
  • 从 0 开始搭建 React 框架
  • 网站地址怎么改成HTTPS?
  • Blender教程(基础)-面的细分与删除、挤出选区-07
  • QT自制软键盘 最完美、最简单、支持中文输入(二)
  • SpringCloud_学习笔记_1
  • 容器算法迭代器初识
  • 瑞_力扣LeetCode_二叉搜索树相关题
  • python爬虫爬取网站
  • c# Get方式调用WebAPI,WebService等接口
  • 银行数据仓库体系实践(11)--数据仓库开发管理系统及开发流程
  • 微信小程序引导用户打开定位授权通用模版
  • JVM篇----第十篇
  • DevSecOps 参考模型介绍
  • 什么是okhttp?
  • R语言基础学习-02 (此语言用途小众 用于数学 生物领域 基因分析)
  • CTF-WEB的入门真题讲解
  • 【C项目】顺序表
  • 【Docker】在Windows下使用Docker Desktop创建nginx容器并访问默认网站
  • 详讲api网关之kong的基本概念及安装和使用(二)
  • 取消Vscode在输入符号时自动补全
  • ElementUI Form:Input 输入框