解决思路:假设对数
a
i
\small a_i
ai取的个数为
k
i
\small k_i
ki,可得到如下等式:
a
1
k
1
+
a
2
k
2
+
.
.
.
+
a
n
k
n
≡
a
(
0
≤
a
≤
m
−
1
)
\small a_1k_1+a_2k_2+...+a_nk_n\equiv a~(0 \leq a \leq m-1)
a1k1+a2k2+...+ankn≡a(0≤a≤m−1)
a
1
k
1
+
a
2
k
2
+
.
.
.
+
a
n
k
n
+
m
k
m
=
a
(
0
≤
a
≤
m
−
1
)
\small a_1k_1+a_2k_2+...+a_nk_n+mk_m= a~(0 \leq a \leq m-1)
a1k1+a2k2+...+ankn+mkm=a(0≤a≤m−1) 发现有解的充要条件是
(
a
1
,
a
2
.
.
.
a
n
,
m
)
∣
a
\small (a_1,a_2...a_n,m) \mid a
(a1,a2...an,m)∣a,此时
a
\small a
a为
0
\small 0
0时不符合裴蜀定理的要求,所以可以通过同余的关系,将
a
\small a
a等于
0
\small 0
0变为
a
\small a
a等于
m
\small m
m。
∴
a
1
k
1
+
a
2
k
2
+
.
.
.
+
a
n
k
n
+
m
k
m
=
a
(
1
≤
a
≤
m
)
\small \therefore a_1k_1+a_2k_2+...+a_nk_n+mk_m= a~(1 \leq a \leq m)
∴a1k1+a2k2+...+ankn+mkm=a(1≤a≤m) 计算
[
1
,
m
]
\small [1,m]
[1,m]中
(
a
1
,
a
2
.
.
.
a
n
,
m
)
\small (a_1,a_2...a_n,m)
(a1,a2...an,m)的倍数即是答案。
AC代码
//优化#pragma GCC optimize(2)//C#include<string.h>#include<stdio.h>#include<stdlib.h>#include<math.h>//C++//#include<unordered_map>#include<algorithm>#include<iostream>#include<istream>#include<iomanip>#include<climits>#include<cstdio>#include<string>#include<vector>#include<cmath>#include<queue>#include<stack>#include<map>#include<set>//宏定义#define N 2010#define DoIdo main//#define scanf scanf_s#define it set<ll>::iterator//定义+命名空间typedeflonglong ll;typedefunsignedlonglong ull;const ll mod =998244353;const ll INF =1e18;constint maxn =5e6+10;usingnamespace std;//全局变量//函数区
ll max(ll a, ll b){return a > b ? a : b;}
ll min(ll a, ll b){return a < b ? a : b;}
ll gcd(ll a, ll b){return!b ? a :gcd(b, a % b);}//主函数intDoIdo(){
ios::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
ll n, m;
cin >> n >> m;
ll g = m;for(int i =1; i <= n; i++){
ll val; cin >> val;
g =gcd(g, val);}
cout << m / g << endl;return0;}//分割线---------------------------------QWQ/*
*/