B. M-arrays
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given an array a1,a2,…,ana1,a2,…,an consisting of nn positive integers and a positive integer mm.
You should divide elements of this array into some arrays. You can order the elements in the new arrays as you want.
Let's call an array mm-divisible if for each two adjacent numbers in the array (two numbers on the positions ii and i+1i+1 are called adjacent for each ii) their sum is divisible by mm. An array of one element is mm-divisible.
Find the smallest number of mm-divisible arrays that a1,a2,…,ana1,a2,…,an is possible to divide into.
Input
The first line contains a single integer tt (1≤t≤1000)(1≤t≤1000) — the number of test cases.
The first line of each test case contains two integers nn, mm (1≤n≤105,1≤m≤105)(1≤n≤105,1≤m≤105).
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109).
It is guaranteed that the sum of nn and the sum of mm over all test cases do not exceed 105105.
Output
For each test case print the answer to the problem.
Example
input
Copy
4 6 4 2 2 8 6 9 4 10 8 1 1 1 5 2 4 4 8 6 7 1 1 666 2 2 2 4
output
Copy
3 6 1 1
Note
In the first test case we can divide the elements as follows:
========================================================================
比较有意思,其实就是统计模数的个数,模数n与m-n进行交叉组合,差值绝对值在1之内能够完全匹配,否则就会有剩余。特判0,m/2,当前模数个数为0的情况即可
- #include
- #include
- #include
- #include
- #include
- #include
-
- using namespace std;
- typedef long long ll;
-
-
- int cnt[100000+10];
- int main()
- {
-
- int t;
-
- cin>>t;
-
- while(t--)
- {
- int n;
- cin>>n;
-
- int m;
-
- cin>>m;
-
- for(int i=0;i
- {
- cnt[i]=0;
- }
- for(int i=1;i<=n;i++)
- {
- int x;
- cin>>x;
-
- x%=m;
- cnt[x]++;
- }
-
- int ans=0;
-
- if(cnt[0])
- ans++;
-
-
- for(int i=1;i<=m/2;i++)
- {
- if(i*2==m&&cnt[i])
- {
- ans+=1;
-
- break;
- }
-
-
- int now=cnt[i];
-
- int temp=cnt[m-i];
-
- if(!cnt[i])
- {
- ans+=temp;
-
- }
- else if(now==temp)
- {
- ans+=1;
-
- }
- else if(now-temp>=1)
- {
- now-=(temp+1);
-
- ans+=1;
-
- ans+=now;
- }
- else if(temp-now>=1)
- {
- temp-=(now+1);
- ans+=1;
- ans+=temp;
- }
-
-
- }
-
- cout<
- }
- return 0;
- }
-
相关阅读:
100行代码实现HarmonyOS“画图”应用,eTS开发走起!
第4集丨Caché 类使用(二)
虚函数可不可以重载为内联 —— 在开启最大优化时gcc、clang和msvc的表现
OpenCV4.9.0开源计算机视觉库在 Linux 中安装
语音分类入门案例: 英文数字音频分类
机器学习中的 朴素贝叶斯算法及其优缺点
【SSM】MyBatis 操作数据库(重点:Mybatis两种使用方式)
Android提供了多种方式来打开特定文件夹中的视频
HIVESQL的列转行和行转列使用总结
虚拟机搭建负载均衡,mysql主从复制和读写分离(一、搭建虚拟机)
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126191090