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;
- }
-
相关阅读:
springboot整合elasticsearch
设计模式之门面模式
什么变量能够影响苦艾酒的味道?
有没有知道这个怎么改呗
Redis 0817
Java架构师内功计算机网络
MYSQL存储引擎基础知识介绍
centos7安装nodejs运行环境及卸载
「PaddleOCR」 模型应用优化流程
信驰达RF-DG-52PAS CC2652P Zigbee 3.0 USB Dongle烧录指南
-
原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126191090