众所周知zhu是一个大厨,zhu一直有自己独特的咸鱼制作技巧.

tang是一个咸鱼供应商,他告诉zhu在他那里面有N条咸鱼(标号从1到N)可以被用来制作.

每条咸鱼都有一个咸鱼值0.

zhu是一个特别的人,他有1.

zhu现在想知道经过了这1?

Input

输入的第一行包含一个整数T组数据.

对于每组数据:

输入第一行只有两个整数M.

接下来一行有M个整数,依次对应zhu的每个咸数.

数据保证:

  • 1≤T≤1000

  • 1≤N≤109

  • 1≤M≤15

  • 1≤咸数≤2∗105

Output

对于每组数据,输出一行表示答案

Sample Input


10 2 
5 8



10 1 

10 1 
1

 

Sample Output

3



10

 

Hint

Prepared by xiper

 

容斥原理,只统计奇数集合覆盖的元素个数,假设m=3,答案等于A+B+C-2AB-2AC-2BC+4ABC……以此类推,每层的系数乘以2。自己瞎几把推一下就行。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll lcm(ll a,ll b)
{
	return a/__gcd(a,b)*b;
}
int T,n,m,a[21];
ll lcms[33000];
void inidfs(int cur,int S,ll now)
{
	lcms[S]=now;
	if(cur==m)
	  return;
	ll t=lcm(now,(ll)a[cur+1]);
	if(t<=(ll)n)
	  inidfs(cur+1,S|(1<<cur),t);
	inidfs(cur+1,S,now);
}
ll ans;
void dfs(int cur,int dep,int S)
{
	if(cur>m)
	  return;
	ans+=(lcms[S]==0 ? 0 : (1ll<<(dep-1))*(dep%2==0 ? (-1ll) : 1ll)*((ll)n/lcms[S]));
	for(int i=cur+1;i<=m;++i)
	  dfs(i,dep+1,S|(1<<(i-1)));
}
int main()
{
	scanf("%d",&T);
	for(;T;--T)
	  {
	  	ans=0;
	  	memset(lcms,0,sizeof(lcms));
	  	scanf("%d%d",&n,&m);
	  	for(int i=1;i<=m;++i)
	  	  scanf("%d",&a[i]);
	  	inidfs(0,0,1);
	  	lcms[0]=0;
	  	dfs(0,0,0);
	  	cout<<ans<<endl;
	  }
	return 0;
}

相关文章:

猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-07
  • 2022-01-24
  • 2022-12-23
  • 2021-06-06
  • 2021-07-12
相关资源
相似解决方案