結果

問題 No.377 背景パターン
ユーザー yosupotyosupot
提出日時 2016-06-05 01:05:22
言語 C++11
(gcc 13.3.0)
結果
AC  
実行時間 532 ms / 5,000 ms
コード長 2,731 bytes
コンパイル時間 1,190 ms
コンパイル使用メモリ 111,476 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-08 13:27:37
合計ジャッジ時間 3,176 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr int TEN(int n) {return (n==0)?1:10*TEN(n-1);}

ll gcd(ll a, ll b) {return (b==0) ? a : gcd(b, a%b);}
ll lcm(ll a, ll b) {return a/gcd(a, b)*b;}

/// x^n
template<class T>
T pow(T x, ll n) {
    T r = 1;
    while (n) {
        if (n & 1) r *= x;
        x *= x;
        n >>= 1;
    }
    return r;
}

template<uint MD>
struct ModInt {
    uint v;
    ModInt() : v(0) {}
    ModInt(ll v) : v(normS(v%MD+MD)) {}
    uint value() const {return v;}
    static uint normS(const uint &x) {return (x<MD)?x:x-MD;};
    static ModInt make(const uint &x) {ModInt m; m.v = x; return m;}
    const ModInt operator+(const ModInt &r) const {return make(normS(v+r.v));}
    const ModInt operator-(const ModInt &r) const {return make(normS(v+normS(MD-r.v)));}
    const ModInt operator*(const ModInt &r) const {return make((ull)v*r.v%MD);}
    ModInt& operator+=(const ModInt &r) {return *this=*this+r;}
    ModInt& operator-=(const ModInt &r) {return *this=*this-r;}
    ModInt& operator*=(const ModInt &r) {return *this=*this*r;}
    static ModInt inv(const ModInt &x) {
        return pow(ModInt(x), MD-2);
    }
};

using Mint = ModInt<TEN(9)+7>;

using P = pair<ll, ll>;
vector<P> calc(ll X) {
    vector<ll> v;
    for (ll i = 1; i*i <= X; i++) {
        if (X%i == 0) {
            v.push_back(i);
            if (i*i != X) {
                v.push_back(X/i);
            }
        }
    }
    sort(v.begin(), v.end());
    int N = (int)v.size();
    vector<P> ans(N);
    for (int i = N-1; i >= 0; i--) {
        ll co = X/v[i];
        for (int j = i+1; j < N; j++) {
            if (v[j] % v[i] == 0) {
                co -= ans[j].second;
            }
        }
        ans[i] = P(v[i], co);
    }
    return ans;
}
int main() {
    ll h, w, k;
    cin >> h >> w >> k;
    Mint ans = 0;
    vector<P> v1 = calc(h), v2 = calc(w);
    int N = (int)v1.size(), M = (int)v2.size();
/*    printf("a\n");
    for (auto d: v1) {
        printf("(%d %d)\n", d.first, d.second);
    }
    printf("b\n");
    for (auto d: v2) {
        printf("(%d %d)\n", d.first, d.second);
    }*/
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            ans += pow(Mint(k), h*w/lcm(h/v1[i].first, w/v2[j].first))*v1[i].second*v2[j].second;
        }
    }
    ans *= Mint::inv(h)*Mint::inv(w);
    cout << ans.value() << endl;
    return 0;
}
0