結果

問題 No.2280 FizzBuzz Difference
ユーザー RubikunRubikun
提出日時 2023-04-21 22:09:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,222 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 200,336 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-24 08:16:49
合計ジャッジ時間 2,933 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 WA -
testcase_02 AC 18 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 20 ms
5,376 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 17 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=300005,INF=1<<30;

ll gcd(ll a,ll b){
    if(b==0) return a;
    return gcd(b,a%b);
}

// crt + floor_sum

//https://github.com/atcoder/ac-library/releases/tag/v1.4

constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};
    
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;
    
    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;
        
        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

std::pair<long long, long long> crt(const std::vector<long long>& r,
                                    const std::vector<long long>& m) {
    assert(r.size() == m.size());
    int n = int(r.size());
    long long r0 = 0, m0 = 1;
    for (int i = 0; i < n; i++) {
        assert(1 <= m[i]);
        long long r1 = safe_mod(r[i], m[i]), m1 = m[i];
        if (m0 < m1) {
            std::swap(r0, r1);
            std::swap(m0, m1);
        }
        if (m0 % m1 == 0) {
            if (r0 % m1 != r1) return {0, 0};
            continue;
        }
        long long g, im;
        std::tie(g, im) = inv_gcd(m0, m1);
        
        long long u1 = (m1 / g);
        if ((r1 - r0) % g) return {0, 0};
        
        long long x = (r1 - r0) / g % u1 * im % u1;
        
        r0 += x * m0;
        m0 *= u1;
        if (r0 < 0) r0 += m0;
    }
    return {r0, m0};
}

unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }
        
        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

long long floor_sum(long long n, long long m, long long a, long long b) {
    assert(0 <= n && n < (1LL << 32));
    assert(1 <= m && m < (1LL << 32));
    unsigned long long ans = 0;
    if (a < 0) {
        unsigned long long a2 = safe_mod(a, m);
        ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        unsigned long long b2 = safe_mod(b, m);
        ans -= 1ULL * n * ((b2 - b) / m);
        b = b2;
    }
    return ans + floor_sum_unsigned(n, m, a, b);
}


int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int Q;cin>>Q;
    while(Q--){
        ll N,A,B,K;cin>>N>>A>>B>>K;
        ll g=gcd(A,B);
        if(K%g){
            cout<<0<<"\n";
            continue;
        }
        A/=g;
        B/=g;
        K/=g;
        N/=g;
        ll L=A*B;
        ll ans=0;
        if(N<=A||K>A){
            cout<<0<<"\n";
            continue;
        }
        if(A==K){
            ll syuu=N/L;
            ans+=syuu*(A+B-1-2*(A-1));
            
            N%=L;
            ans+=N/A-N/B;
            ans--;
        }else{
            ll syuu=N/L;
            ans+=syuu*2;
            
            N%=L;
            
            auto re1=crt({K%A,0},{A,B});
            auto re2=crt({K%B,0},{B,A});
            
            //cout<<re1.fi<<" "<<re2.fi<<endl;
            if(re1.fi<=N) ans++;
            if(re2.fi<=N) ans++;
        }
        cout<<ans<<"\n";
    }
}
0