結果

問題 No.1731 Product of Subsequence
ユーザー hiro71687khiro71687k
提出日時 2023-03-31 00:44:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,122 ms / 2,000 ms
コード長 1,239 bytes
コンパイル時間 4,648 ms
コンパイル使用メモリ 272,376 KB
実行使用メモリ 169,380 KB
最終ジャッジ日時 2023-10-22 07:39:05
合計ジャッジ時間 15,578 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 964 ms
157,864 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 33 ms
8,536 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 1,122 ms
168,736 KB
testcase_11 AC 959 ms
145,972 KB
testcase_12 AC 5 ms
4,348 KB
testcase_13 AC 16 ms
7,508 KB
testcase_14 AC 842 ms
128,592 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 906 ms
133,820 KB
testcase_19 AC 19 ms
7,516 KB
testcase_20 AC 732 ms
109,656 KB
testcase_21 AC 1,111 ms
169,380 KB
testcase_22 AC 4 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 16 ms
7,404 KB
testcase_25 AC 8 ms
5,172 KB
testcase_26 AC 166 ms
32,520 KB
testcase_27 AC 226 ms
41,824 KB
testcase_28 AC 494 ms
79,532 KB
testcase_29 AC 175 ms
34,392 KB
testcase_30 AC 929 ms
168,108 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 61 ms
16,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll=long long;
using ld=long double;
ld pie=3.141592653589793;
ll mod=1000000007;
ll inf=999999999999999999;
ll gcd(ll a, ll b) {
	a = abs(a); b = abs(b);
	if (a < b)swap(a, b);
	while (b) {
	    ll r=a%b;
        a=b;
	    b=r;
	}
	return a;
}
ll modpow(ll x, ll n) {
  x = x%mod;
  if(n==0) return 1;  //再帰の終了条件

  else if(n%2==1) {
    return (x*modpow(x, n-1))%mod;  //nが奇数ならnを1ずらす
  }
  else return modpow((x*x)%mod, n/2)%mod;  //nが偶数ならnが半分になる
}
int main(){
    ll n,k;
    cin >> n >> k;
    if (k==1)
    {
        cout << (modpow(2,n)+mod-1)%mod << endl;
        return 0;
    }
    
    vector<ll>a(n);
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
        a[i]=gcd(k,a[i]);
    }
    vector<map<ll,ll>>dp(n);
    dp[0][1]=1;
    ll x=gcd(k,a[0]);
    dp[0][x]+=1;
    for (ll i = 1; i < n; i++)
    {
        for(auto v:dp[i-1]){
            dp[i][v.first]+=v.second;
            dp[i][v.first]%=mod;
            ll z=v.first*a[i];
            z=gcd(k,z);
            dp[i][z]+=v.second;
            dp[i][z]%=mod;
        }
    }
    cout << dp[n-1][k] << endl;
}
0