結果

問題 No.377 背景パターン
ユーザー EmKjpEmKjp
提出日時 2015-05-10 07:06:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 586 ms / 5,000 ms
コード長 2,485 bytes
コンパイル時間 1,046 ms
コンパイル使用メモリ 97,056 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-19 09:39:25
合計ジャッジ時間 3,363 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 4 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 571 ms
4,376 KB
testcase_17 AC 586 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<numeric>
#include<functional>
#include<complex>
#include<cassert>

using namespace std;
#define BET(a,b,c) ((a)<=(b)&&(b)<(c))
#define FOR(i,n) for(int i=0,i##_end=(int(n));i<i##_end;i++)
#define SZ(x) (int)(x.size())
#define ALL(x) (x).begin(),(x).end()
#define MP make_pair
#define FOR_EACH(it,v) for(__typeof(v.begin()) it=v.begin(),it_end=v.end() ; it != it_end ; it++)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long ll_t;

ll_t gcd(ll_t s, ll_t t){ return t ? gcd(t , s % t) : s;}
ll_t lcm(ll_t s, ll_t t){ return s/gcd(s,t)*t; }

ll_t modpow(ll_t x , ll_t y , ll_t mod){
    x %= mod;
  
    ll_t t = x, res = 1;
    for(; y ; y >>= 1){
        if(y & 1) {
            res = res * t;
            if(res >= mod) res %= mod;
        }
        t = t * t;
        if(t >= mod) t %= mod;
    }
    return res;
}

ll_t exgcd(ll_t a, ll_t b, ll_t &x, ll_t &y)
{
    if(b == 0) {
        x = 1; y = 0;
        return a;
    } else {
        ll_t d = exgcd(b, a % b, y, x);
        y -= a / b * x;
        return d;
    }
}

ll_t inv(ll_t a, ll_t mod) {
    ll_t x, y;
    if (exgcd(a, mod, x, y) == 1){
        return (x + mod) % mod;
    }
    else {
        return -1;
    }
}

const int mod = 1000000007;

vector<int> aliquot(int x){
    VI r ;
    for(int i=1;i*i<=x;i++){
        if(x % i == 0){
            r.push_back(i);
            if(i != x / i) r.push_back(x/i);
        }
    }
    sort(ALL(r));
    return r;
}

map<int,int> calc(long long x){
    map<int,int> dp;
    VI a = aliquot(x);
    for(int i=SZ(a)-1;i>=0;i--){
        int val = 0 ;
        val = x / a[i];
        for(int j=i+1;j<SZ(a);j++){
            if(a[j] % a[i] == 0) val -= dp[a[j]];
        }
        dp[a[i]] = val;
    }
    return dp;
}

long long solve(long long w,long long h, int k){
    map<int,int> a, b;
    a = calc(h);
    b = calc(w);
    long long ans = 0;
    for(auto pa : a){
        for(auto pb : b){
            long long groupNum = w * h / lcm(h / pa.first, w / pb.first);
            ans += modpow(k, groupNum, mod) * pa.second % mod * pb.second;
            ans %= mod;
        }
    }
    ans *= inv(w * h % mod, mod);
    ans %= mod;    
    return ans;
}

int main()
{
    int h, w, k;
    cin>>h>>w>>k;
    cout<<solve(w,h,k)<<endl;
    return 0;
}
0