結果
問題 | No.2280 FizzBuzz Difference |
ユーザー | Rubikun |
提出日時 | 2023-04-21 22:20:45 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 20 ms / 2,000 ms |
コード長 | 4,650 bytes |
コンパイル時間 | 2,278 ms |
コンパイル使用メモリ | 207,356 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-06 15:50:31 |
合計ジャッジ時間 | 2,944 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 14 ms
5,248 KB |
testcase_02 | AC | 18 ms
5,248 KB |
testcase_03 | AC | 10 ms
5,248 KB |
testcase_04 | AC | 20 ms
5,248 KB |
testcase_05 | AC | 15 ms
5,248 KB |
testcase_06 | AC | 16 ms
5,248 KB |
testcase_07 | AC | 16 ms
5,248 KB |
ソースコード
#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); } ll solve(ll N,ll A,ll B,ll K){ ll g=gcd(A,B); if(K%g){ return 0; } A/=g; B/=g; K/=g; N/=g; ll L=A*B; ll ans=0; if(N<=A||K>A){ return 0; } if(A==K){ ll syuu=N/L; ans+=syuu*(A+B-1-2*(A-1)); N%=L; ans+=N/A-(N/A*A)/B; if(ans) 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++; } return ans; } ll gu(ll N,ll A,ll B,ll K){ vector<ll> X; for(ll i=1;i<=N;i++){ if(i%A==0||i%B==0) X.push_back(i); } ll re=0; for(int i=0;i+1<si(X);i++) if(X[i+1]-X[i]==K) re++; return re; } int main(){ std::ifstream in("text.txt"); std::cin.rdbuf(in.rdbuf()); cin.tie(0); ios::sync_with_stdio(false); /* for(ll n=1;n<=100;n++){ for(ll a=1;a<=100;a++){ for(ll b=a+1;b<=100;b++){ for(int k=1;k<=100;k++){ ll x=solve(n,a,b,k),y=gu(n,a,b,k); if(x!=y) cout<<x<<" "<<y<<" "<<n<<" "<<a<<" "<<b<<" "<<k<<endl; } } } } */ int Q;cin>>Q; while(Q--){ ll N,A,B,K;cin>>N>>A>>B>>K; cout<<solve(N,A,B,K)<<"\n"; } }