結果
問題 | No.377 背景パターン |
ユーザー |
![]() |
提出日時 | 2015-05-10 07:06:22 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 507 ms / 5,000 ms |
コード長 | 2,485 bytes |
コンパイル時間 | 1,098 ms |
コンパイル使用メモリ | 95,868 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-05 21:22:36 |
合計ジャッジ時間 | 2,635 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 14 |
ソースコード
#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;}