結果

問題 No.1358 [Zelkova 2nd Tune *] 語るなら枚数を...
ユーザー milanis48663220milanis48663220
提出日時 2021-01-22 22:25:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 793 ms / 2,000 ms
コード長 4,730 bytes
コンパイル時間 1,119 ms
コンパイル使用メモリ 100,252 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-08 15:47:09
合計ジャッジ時間 5,869 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 10 ms
5,376 KB
testcase_07 AC 8 ms
5,376 KB
testcase_08 AC 8 ms
5,376 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 7 ms
5,376 KB
testcase_11 AC 683 ms
5,376 KB
testcase_12 AC 610 ms
5,376 KB
testcase_13 AC 793 ms
5,376 KB
testcase_14 AC 542 ms
5,376 KB
testcase_15 AC 603 ms
5,376 KB
testcase_16 AC 734 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef __int128_t ll;

__int128 parse(string &s) {
  __int128 ret = 0;
  for (int i = 0; i < s.length(); i++)
    if ('0' <= s[i] && s[i] <= '9')
      ret = 10 * ret + s[i] - '0';
  return ret;
}

istream & operator >> (istream &in,  __int128_t &m){
    string s; cin >> s;
    m = parse(s);
    return in;
}


ostream &operator<<(std::ostream &dest, __int128_t value) {
    ostream::sentry s(dest);
    if (s) {
        __uint128_t tmp = value < 0 ? -value : value;
        char buffer[128];
        char *d = end(buffer);
        do {
        --d;
        *d = "0123456789"[tmp % 10];
        tmp /= 10;
        } while (tmp != 0);
        if (value < 0) {
        --d;
        *d = '-';
        }
        int len = end(buffer) - d;
        if (dest.rdbuf()->sputn(d, len) != len) {
        dest.setstate(ios_base::badbit);
        }
    }
    return dest;
}

const ll MOD = 1000000007;

class ModInt{
    public:
    ll v;
    ModInt(ll _v = 0){
        if(_v >= MOD) _v %= MOD;
        v = _v;
    }
    ModInt operator+(ll n){
        return ModInt((v+n)%MOD);
    }
    ModInt operator-(ll n){
        return ModInt((v-n+MOD)%MOD);
    }
    ModInt operator*(ll n){
        if(n >= MOD) n %= MOD;
        return ModInt((v*n)%MOD);
    }
    ModInt operator/(ll n){
        return ModInt((ModInt(n).inv()*v).v%MOD);
    }

    void operator+=(ll n){
        v = (v+n)%MOD;
    }
    void operator-=(ll n){
        v = (v-n+MOD)%MOD;
    }
    void operator*=(ll n){
        v = (v*n+MOD)%MOD;
    }
    
    
    ModInt operator+(ModInt n){
        return ModInt((v+n.v)%MOD);
    }
    ModInt operator-(ModInt n){
        return ModInt((v-n.v+MOD)%MOD);
    }
    ModInt operator*(ModInt n){
        return ModInt((v*n.v)%MOD);
    }
    ModInt operator/(ModInt n){
        return ModInt((n.inv()*v).v%MOD);
    }

    void operator+=(ModInt n){
        v = (v+n.v)%MOD;
    }
    void operator-=(ModInt n){
        v = (v-n.v+MOD)%MOD;
    }
    void operator*=(ModInt n){
        v = (v*n.v)%MOD;
    }

    void operator=(ModInt n){
        v = n.v;
    }
    bool operator==(ModInt n){
        return v == n.v;
    }
    bool operator!=(ModInt n){
        return v != n.v;
    }
    void operator=(ll n){
        v = n;
    }

    ModInt inv(){
        if(v == 1) return ModInt(1);
        else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
    }
};

ostream& operator<<(ostream& os, const ModInt& m){
    os << m.v;
    return os;
}

istream & operator >> (istream &in,  ModInt &m){
    in >> m.v;
    return in;
}

template <typename T>
T gcd(T a, T b) {
	if (a < b) swap(a, b);
		while (b != 0) {
			T tmp = b;
			b = a % b;
			a = tmp;
		}
	return a;
}

template <typename T>
T gcd(initializer_list<T> v) {
	T ans = 0;
    for(T x : v) ans = gcd(ans, x);
	return ans;
}

template <typename T>
T lcm(T a, T b) {
  	return (a * b) / gcd<T>(a, b);
}

ll tot(ll n){
    ll ans = n;
    ll tmp = n;
    for(ll i = 2; i*i <= n; i++){
        if(tmp%i == 0){
            ans /= i;
            ans *= (i-1);
            while(tmp%i == 0){
                tmp /= i;
            }
        }
    }
    if(tmp != 1){
        ans /= tmp;
        ans *= (tmp-1);
    }
    return ans;
}

template <typename T>
T pow(T a, ll n, ll MOD) {
	T ans = 1;
	T tmp = a;
	for (int i = 0; i <= 60; i++) {
		ll m = (ll)1 << i;
		if (m & n) {
		ans *= tmp;
		ans %= MOD;
		}
		tmp *= tmp;
		tmp %= MOD;
	}
	return ans;
}

void solve(){
    vector<ll> x(3);
    ll y;
    for(int i = 0; i < 3; i++) cin >> x[i];
    cin >> y;
    sort(x.begin(), x.end());
    ModInt ans = 0;
    ll g = gcd(x[0], x[1]);
    ll a = x[0]/g, b = x[1]/g;
    ll a_inv = pow(a, tot(b)-1, b);
    for(ll i = 0; i*x[2] <= y; i++){
        ll r = y-(i*x[2]);
        if(r%g != 0) continue;
        r /= g;
        ll s = (r*a_inv)%b;
        // bで割ってs余る数が解の候補
        ll s_max = r/a;
        if(s_max%b >= s){
            ans += s_max/b+1;
        }else{
            ans += s_max/b;
        }
    }
    cout << ans << endl;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int t; cin >> t;
    for(int i = 0; i < t; i++) solve();
}
0