結果

問題 No.1502 Many Simple Additions
ユーザー milanis48663220milanis48663220
提出日時 2021-05-07 23:19:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 69 ms / 2,000 ms
コード長 5,088 bytes
コンパイル時間 1,205 ms
コンパイル使用メモリ 106,656 KB
実行使用メモリ 14,604 KB
最終ジャッジ日時 2023-10-13 22:36:55
合計ジャッジ時間 3,537 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,760 KB
testcase_01 AC 3 ms
5,684 KB
testcase_02 AC 3 ms
5,756 KB
testcase_03 AC 3 ms
5,656 KB
testcase_04 AC 3 ms
5,720 KB
testcase_05 AC 3 ms
5,956 KB
testcase_06 AC 3 ms
5,720 KB
testcase_07 AC 3 ms
5,724 KB
testcase_08 AC 3 ms
5,684 KB
testcase_09 AC 3 ms
5,788 KB
testcase_10 AC 3 ms
5,832 KB
testcase_11 AC 3 ms
5,984 KB
testcase_12 AC 3 ms
5,740 KB
testcase_13 AC 3 ms
5,708 KB
testcase_14 AC 3 ms
5,776 KB
testcase_15 AC 3 ms
5,708 KB
testcase_16 AC 3 ms
5,720 KB
testcase_17 AC 3 ms
5,696 KB
testcase_18 AC 3 ms
6,000 KB
testcase_19 AC 3 ms
5,800 KB
testcase_20 AC 4 ms
5,764 KB
testcase_21 AC 3 ms
5,736 KB
testcase_22 AC 3 ms
5,784 KB
testcase_23 AC 3 ms
5,844 KB
testcase_24 AC 3 ms
5,664 KB
testcase_25 AC 3 ms
5,684 KB
testcase_26 AC 3 ms
5,724 KB
testcase_27 AC 48 ms
12,544 KB
testcase_28 AC 45 ms
12,812 KB
testcase_29 AC 9 ms
7,536 KB
testcase_30 AC 67 ms
14,604 KB
testcase_31 AC 69 ms
14,452 KB
testcase_32 AC 67 ms
14,508 KB
testcase_33 AC 52 ms
12,752 KB
testcase_34 AC 60 ms
12,944 KB
testcase_35 AC 56 ms
12,728 KB
testcase_36 AC 33 ms
9,360 KB
testcase_37 AC 33 ms
9,268 KB
testcase_38 AC 53 ms
11,680 KB
testcase_39 AC 33 ms
9,412 KB
testcase_40 AC 13 ms
6,936 KB
testcase_41 AC 5 ms
6,168 KB
testcase_42 AC 52 ms
13,016 KB
testcase_43 AC 4 ms
5,736 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 long long ll;

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%MOD;
    }

    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;
}

ModInt pow(ModInt a, ll n) {
    assert(n >= 0);
	ModInt ans = 1;
	ModInt tmp = a;
	for (int i = 0; i <= 60; i++) {
		ll m = (ll)1 << i;
		if (m & n) {
		ans *= tmp;
		}
		tmp *= tmp;
	}
	return ans;
}

struct edge{
    int to;
    ll cost;
};

using mint = ModInt;

vector<edge> g[100000];
ll a[100000], b[100000];
bool used[100000];

bool ng;
vector<int> visited;
set<ll> cand;
void dfs(int v){
    visited.push_back(v);
    used[v] = true;
    for(edge e : g[v]){
        if(used[e.to]) {
            if(a[v] == a[e.to]){
                // 2*a[v]*x+b[v]+b[e.to] = e.cost
                if((e.cost-b[v]-b[e.to])%2 != 0) ng = true;
                else{
                    cand.insert((e.cost-b[v]-b[e.to])/(2*a[v]));
                }
            }else{
                if(b[v]+b[e.to] != e.cost) ng = true;
            }
        }else{
            a[e.to] = -a[v];
            b[e.to] = e.cost-b[v];
            dfs(e.to);
        }
    }
}

const ll INF = 1e15;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    ll n, m, k; cin >> n >> m >> k;
    for(int i = 0; i < m; i++){
        int x, y; ll z; cin >> x >> y >> z; x--; y--;
        g[x].push_back(edge{y, z});
        g[y].push_back(edge{x, z});
    }
    auto clear = [&](){
        visited.clear();
        ng = false;
        cand.clear();
    };
    auto f = [&](ll x, int v){
        return a[v]*x+b[v];
    };
    mint n_all = 1, n_ng = 1;
    for(int i = 0; i < n; i++){
        if(used[i]) continue;
        clear();
        a[i] = 1;
        b[i] = 0;
        dfs(i);
        if(ng || cand.size() >= 2){
            cout << 0 << endl;
            return 0;
        }else{
            if(cand.empty()){
                ll x_min = -INF, x_max = INF;
                for(int v : visited){
                    ll s = a[v]*(1-b[v]);
                    ll t = a[v]*(k-b[v]);
                    chmax(x_min, min(s, t));
                    chmin(x_max, max(s, t));
                }
                set<ll> just;
                // cout << x_min << ' ' << x_max << endl;
                if(x_max < x_min){
                    cout << 0 << endl;
                    return 0;
                }
                for(int v : visited){
                    ll x = (k-b[v])/a[v];
                    if(x_min <= x && x <= x_max) just.insert(x);
                }
                n_all *= x_max-x_min+1;
                n_ng *= x_max-x_min+1-just.size();
            }else{
                ll x = *cand.begin();
                for(int v : visited){
                    if(f(x, v) < 1 || f(x, v) > k){
                        cout << 0 << endl;
                        return 0;
                    }
                    if(f(x, v) == k) n_ng = 0;
                }
            }
        }
    }
    cout << n_all-n_ng << endl;
}
0