当前位置:网站首页>B. M-arrays
B. M-arrays
2022-08-06 22:22:00 【秦小咩】
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:
- [4,8][4,8]. It is a 44-divisible array because 4+84+8 is divisible by 44.
- [2,6,2][2,6,2]. It is a 44-divisible array because 2+62+6 and 6+26+2 are divisible by 44.
- [9][9]. It is a 44-divisible array because it consists of one element.
========================================================================
比较有意思,其实就是统计模数的个数,模数n与m-n进行交叉组合,差值绝对值在1之内能够完全匹配,否则就会有剩余。特判0,m/2,当前模数个数为0的情况即可
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
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<m;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<<ans<<endl;
}
return 0;
}
边栏推荐
- 10个自动EDA库功能介绍:几行代码进行的数据分析靠不靠谱
- C语言结构体链表 节点插入方法(从前和从后)
- Cascade WPF 】 【 Combobox and its linkage with ListView
- 自定义指令注册自定义指令
- package DotaChessSelfPlay is not in GOROOT以及 relative import paths are not supported in module mode
- 硅谷课堂第十四课-直播对接和微信分享
- Convertible bond strategy under R-Breaker
- Redis 复习计划 - String内存开销问题以及基本/扩展数据类型的使用
- uniapp swipe left to delete effect 1, effect 2 (choose one of 2 methods) (sorting)
- Before start of result set error (resolved)
猜你喜欢
随机推荐
xpath的使用:(1)xpath插件的安装
0x0000003b蓝屏什么原因 win7蓝屏0x0000003b怎么恢复
淘宝的成长之路
硅谷课堂第六课-腾讯云点播管理模块(一)
正确按照字典顺序比较字符串之 localeCompare
self-attention mechanism
【How to use Medooze to realize multi-party video conference】
从 VLAN 到 IPVLAN: 聊聊虚拟网络设备及其在云原生中的应用
Mel滤波器:模拟人耳对不同频率语音的感知【人类对频率的感知不是线性的】【对低频信号的感知要比高频信号敏感;对1kHz以下,与频率成线性关系;对1kHz以上,与频率成对数关系】【频率越高,感知力越弱】
逻辑&算术运算
The Node study notes
HCIP笔记(九)
HCIP笔记(十五)
Compose 进阶挑战来啦!直播预告 | 8 月 7 日晚 19:30 与 GDE 导师面对面
LeetCode - 112. 路径总和;113. 路径总和 II【进阶】
win32概述及框架
微信公众平台服务器配置启用说明
0x000000f4蓝屏是怎么回事 win7蓝屏0x000000f4解决方法
迄今为止见过最详细的零拷贝技术讲解
12 MySQL optimization tips, the speed is more than ten times faster!









