結果

問題 No.377 背景パターン
ユーザー chocorusk
提出日時 2018-12-03 14:09:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 563 ms / 5,000 ms
コード長 1,646 bytes
コンパイル時間 865 ms
コンパイル使用メモリ 99,612 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-01 05:36:53
合計ジャッジ時間 2,695 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, ll> Pl;
const ll MOD=1e9+7;
ll gcd(ll a, ll b){
  if(b==0) return a;
  return gcd(b, a%b);
}
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
	return powmod(a, MOD-2);
}
vector<ll> dh, dw;
int main()
{
	ll h, w, k;
	cin>>h>>w>>k;
	for(ll i=1; i*i<=h; i++){
		if(h%i==0){
			dh.push_back(i);
			if(i*i<h) dh.push_back(h/i);
		}
	}
	sort(dh.begin(), dh.end());
	for(ll i=1; i*i<=w; i++){
		if(w%i==0){
			dw.push_back(i);
			if(i*i<w) dw.push_back(w/i);
		}
	}
	sort(dw.begin(), dw.end());
  ll ans=0;
  ll ct1[1400], ct2[1400];;
  ct1[dh.size()-1]=1;
  for(int i=dh.size()-1; i>=0; i--){
    ct1[i]=h/dh[i];
    for(int j=i+1; j<dh.size(); j++){
      if(dh[j]%dh[i]==0) ct1[i]-=ct1[j];
    }
  }
  ct2[dw.size()-1]=1;
  for(int i=dw.size()-1; i>=0; i--){
    ct2[i]=w/dw[i];
    for(int j=i+1; j<dw.size(); j++){
      if(dw[j]%dw[i]==0) ct2[i]-=ct2[j];
    }
  }
  for(int i=0; i<dh.size(); i++){
    for(int j=0; j<dw.size(); j++){
      ll d=h/dh[i]/gcd(h/dh[i], w/dw[j])*w/dw[j];
      ans+=(ct1[i]*ct2[j]%MOD*powmod(k, h*w/d)%MOD);
      ans%=MOD;
    }
  }
  cout<<ans*inv(h*w%MOD)%MOD<<endl;
  return 0;
}
0