結果

問題 No.377 背景パターン
ユーザー EmKjpEmKjp
提出日時 2015-05-16 12:32:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 756 ms / 5,000 ms
コード長 3,169 bytes
コンパイル時間 886 ms
コンパイル使用メモリ 99,084 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 09:08:52
合計ジャッジ時間 3,312 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 751 ms
4,376 KB
testcase_17 AC 756 ms
4,376 KB
testcase_18 AC 3 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;
    }
}


vector<ll_t> g_prime;
const int MAX_PRIME=100000;
bool is_prime_[(MAX_PRIME+1)/2] ; 

bool is_prime(ll_t x){
    if(x==2) return true;
    if(x<=1 || x%2==0) return false;
    return is_prime_[x/2];
}

void setup_prime()
{
    fill_n(is_prime_ , (MAX_PRIME+1)/2 , true);
    g_prime.clear() ; 
    g_prime.push_back(2);
    for(int i=3;i<=MAX_PRIME;i+=2){
        if(!is_prime_[i>>1]) continue;
        g_prime.push_back(i); 
        for(int j=i+i+i;j<=MAX_PRIME;j+=i+i){
            is_prime_[j>>1]=false;
        }
    }
}

// Euler function
map<int,int> memo;
ll_t phi(ll_t x){
    ll_t xx = x;
    if(memo.count(x)) return memo[x];
    ll_t val = x;
    for(int i=0;i<(int)g_prime.size();i++){
        if(g_prime[i] > x) break;
        if(x % g_prime[i] == 0){      
            val = val / g_prime[i] * (g_prime[i] - 1) ;
            while(x % g_prime[i] == 0)
                x /= g_prime[i];
        }
    }
    if(x>1){
        val = val / x * (x - 1) ; 
    }
    return memo[xx] = val;
}


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

long long solve(long long w,long long h, int k){
    auto a = aliquot(h);
    auto b = aliquot(w);
    long long ans = 0;
    for(auto pa : a){
        for(auto pb : b){
            long long groupNum = w * h / lcm(pa, pb);
            ans += modpow(k, groupNum, mod) * phi(pa) % mod * phi(pb);
            ans %= mod;
        }
    }
    ans *= inv(w * h, mod);
    ans %= mod;    
    return ans;
}

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