Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for(auto&x:v) cin >> x;
- #define cout(v) for(auto&x:v) cout << x << " ";
- void files(){
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- #endif
- }
- const ll mod = (ll) (1e9+7);
- const int N = 1e7 ;
- int spf[N+5];
- void mod_Sieve(){
- iota(spf , spf +N+5 , 0);
- spf[0]=spf[1]=-1;
- for(int i = 2 ; i * i <= N ; i++)
- if(spf[i]==i)
- for(int j = i * i ; j <= N ; j+=i)
- if(spf[j]==j)
- spf[j]=i;
- }
- int n ,x , mxVal;
- vector < vector < int > > dp;
- vector < int > v;
- map < int , int > mp , order;
- int rec(int idx , int mask){
- int & ret = dp[idx][mask];
- if(~ret)
- return ret;
- if(idx == n ) return ret= (mask == (1 << mxVal) - 1 );
- ret =0;
- int temp = v[idx];
- int newmask = mask , cnt=0 , prev =0;
- while(temp > 1){
- if(spf[temp] != prev){
- if(cnt == mp[prev] and prev !=0 )
- newmask |= (1 << order[prev]);
- prev = spf[temp] ;
- cnt=1;
- }
- else
- cnt++;
- temp/= spf[temp];
- }
- if(cnt == mp[prev] and cnt > 0 and prev)
- newmask |= (1 << order[prev]);
- temp = rec(idx+1 , newmask) ;
- if(mask != newmask){
- ret += rec(idx+1 , mask);
- if(ret >= mod) ret-=mod;
- ret += temp;
- if(ret >= mod) ret-=mod;
- }else{
- ret += 2*temp;
- if(ret >= mod) ret-=mod;
- }
- // cout << v[idx] << " " << mask << " " << ret << nl;
- return ret ;
- }
- void solve(){
- cin >> n >> x ;
- v.clear();
- mp.clear();
- order.clear();
- for(int i =0 ; i < n ; i++){
- int q ; cin >> q;
- if(x % q ==0) v.pb(q);
- }
- int temp =x;
- while(temp > 1){
- mp[spf[temp]]++;
- temp/=spf[temp];
- }
- n = sz(v);
- int j =0;
- for(auto& [f,s] : mp) order[f] = j++;
- mxVal = sz(mp);
- dp.assign(n+1 , vector <int> (1 << mxVal , -1));
- cout << rec(0 , 0) << nl;
- }
- int main(){
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- // files();
- mod_Sieve();
- int testCase=1;
- cin >> testCase ;
- for(int i=1 ; i <= testCase ; i++){
- // cout << "Case "<< i <<": " << nl;
- solve();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement