C. Raspberries

time limit per test

2 seconds

memory limit per test

256 megabytes

You are given an array of integers a1,a2,...,ana1,a2,...,an and a number kk (2≤k≤52≤k≤5). In one operation, you can do the following:

  • Choose an index 1≤i≤n1≤i≤n,
  • Set ai=ai+1ai=ai+1.

Find the minimum number of operations needed to make the product of all the numbers in the array a1⋅a2⋅...⋅ana1⋅a2⋅...⋅an divisible by kk.

Input

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

The first line of each test case contains two integers nn and kk (2≤n≤1052≤n≤105, 2≤k≤52≤k≤5) --- the size of the array aa and the number kk.

The second line of each test case contains nn integers a1,a2,...,ana1,a2,...,an (1≤ai≤101≤ai≤10).

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

Output

For each test case, output the minimum number of operations needed to make the product of all the numbers in the array divisible by kk.

Example

Input

Copy

复制代码

15

2 5

7 3

3 3

7 4 1

5 2

9 7 7 3 9

5 5

5 4 1 2 3

7 4

9 5 1 5 9 5 1

3 4

6 3 6

3 4

6 1 5

3 4

1 5 9

4 4

1 4 1 1

3 4

3 5 3

4 5

8 9 9 3

2 5

1 6

2 5

10 10

4 5

1 6 1 1

2 5

7 7

Output

Copy

复制代码
2
2
1
0
2
0
1
2
0
1
1
4
0
4
3

Note

In the first test case, we need to choose the index i=2i=2 twice. After that, the array will be a=[7,5]a=[7,5]. The product of all the numbers in the array is 3535.

In the fourth test case, the product of the numbers in the array is 120120, which is already divisible by 55, so no operations are needed.

In the eighth test case, we can perform two operations by choosing i=2i=2 and i=3i=3 in any order. After that, the array will be a=[1,6,10]a=[1,6,10]. The product of the numbers in the array is 6060.

解题说明:此题是一道数学题,由于K范围很小,可以分类讨论。当k=4的时候存在给两个数都加1的情况,其他情况下的k都只可以给一个数一直加1直到这个数可以整除k。

cpp 复制代码
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int INF = 0x3f3f3f3f;
int main()
{
	int T; 
	cin >> T;
	while (T--)
	{
		int n, k; 
		cin >> n >> k;
		int a[100004];
		int ans = INF;
		int cal = 1;
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			cal *= a[i];
			if (a[i] % k == 0) 
			{
				ans = 0;
			}
			int t = a[i] / k + 1;
			ans = min(ans, t * k - a[i]);
		}
		if (k == 4)
		{
			int cnt1 = 0, cnt2 = 0;
			for (int i = 1; i <= n; i++)
			{
				switch (a[i] % 4)
				{
				case 1:
					cnt1++;
					break;
				case 2:
					cnt2++;
					break;
				case 3:
					break;
				default:
					ans = 0;
					break;
				}
			}
			if (cnt1 >= 2)
			{
				ans = min((int)2, ans);
			}
			if (cnt2 >= 2)
			{
				ans = 0;
			}
			if (cnt1 >= 1 && cnt2 >= 1)
			{
				ans = min((int)1, ans);
			}
		}
		cout << ans << endl;
	}
	return 0;
}
相关推荐
嗯? 嗯。11 分钟前
工作bug,keil5编译器,理解int 类型函数返回值问题,详解!!!
c语言·return·keil5编译器·整数返回类型函数
m0_7482565626 分钟前
Rust环境安装配置
开发语言·后端·rust
程序猿阿伟28 分钟前
《C++巧铸随机森林:开启智能决策新境界》
开发语言·c++·随机森林
假意诗人1 小时前
【NextJS】Arco Design与Next.js快速上手
开发语言·javascript·arco design
凡人的AI工具箱1 小时前
40分钟学 Go 语言高并发教程目录
开发语言·后端·微服务·性能优化·golang
pzx_0011 小时前
【论文阅读】相似误差订正方法在风电短期风速预报中的应用研究
开发语言·论文阅读·python·算法·leetcode·sklearn
每天写点bug1 小时前
【golang】匿名内部协程,值传递与参数传递
开发语言·后端·golang
抽风侠1 小时前
qt实现窗口的动态切换
开发语言·qt
liuweni1 小时前
Next.js系统性教学:深入理解缓存交互与API缓存管理
开发语言·前端·javascript·经验分享·缓存·前端框架·交互
AI人H哥会Java2 小时前
【JAVA】Java高级:多数据源管理与Sharding:在Spring Boot应用中实现多数据源的管理
java·开发语言