結果
| 問題 | 
                            No.2119 一般化百五減算
                             | 
                    
| コンテスト | |
| ユーザー | 
                             NAMIDAIRO
                         | 
                    
| 提出日時 | 2022-11-04 21:56:17 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                WA
                                 
                             
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 1,971 bytes | 
| コンパイル時間 | 908 ms | 
| コンパイル使用メモリ | 100,480 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-18 19:41:31 | 
| 合計ジャッジ時間 | 1,768 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 21 WA * 4 | 
ソースコード
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
#include <iomanip>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
template<class T>
using vi = vector<T>;
template<class T>
using vii = vector<vi<T>>;
template<class T>
using viii = vector<vii<T>>;
void chmax(ll & x, ll y) { x = max(x, y); }
ll gcd(ll x, ll y) {
    if (y == 0) {
        return x;
    }
    return gcd(y, x % y);
    // return y ? gcd(y, x%y) : x;
}
//ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; }
ll lcm(ll x, ll y) {
    return x / gcd(x, y) * y;
}
ll extgcd(ll x, ll y, ll& a, ll& b) {
    if (y == 0) {
        a = 1LL, b = 0LL;
        return x;
    }
    ll g = extgcd(y, x % y, a, b);
    ll ta = a, tb = b;
    a = tb;
    b = ta - x / y * tb;
    if (a < 0LL) a += y / g, b -= x / g; //when you wanna a >= 0 
    return g;
}
struct status {
    ll g, lcm, cr;
};
//m(mod x), n(mod y)
status crt(ll x, ll y, ll m, ll n) {
    status res;
    ll a, b;
    res.g = extgcd(x, y, a, b); //assume non-zero
    //g = xa + yb
    res.lcm = x / res.g * y;
    if ((m - n) % res.g) res.cr = -1;
    else {//xa'+m=-yb'+n ⇔ xa'+yb'=n-m=kg ⇔ xak+ybk = kg
        res.cr = (n - m) / res.g * a % (y / res.g); //係数の桁数を先に落とす
        res.cr = res.cr * x + m;
        res.cr = (res.cr % res.lcm + res.lcm) % res.lcm;
    }
    return res;
}
int main()
{
    int n, m;
    cin >> n >> m;
    status ans = {0, 0, 0};
    while (m--) {
        ll b, c;
        cin >> b >> c;
        c -= c / b * b;
        if (c < 0) c += b;
        if (ans.g == 0) {
            ans.g = b;
            ans.lcm = b;
            ans.cr = c;
            continue;
        }
        ans = crt(ans.lcm, b, ans.cr, c);
        if (ans.cr < 0 || ans.cr > n) {
            cout << "NaN" << endl;
            return 0;
        }
    }
    cout << ans.cr << endl;
	return 0;
}
            
            
            
        
            
NAMIDAIRO