結果
| 問題 |
No.377 背景パターン
|
| コンテスト | |
| ユーザー |
anta
|
| 提出日時 | 2016-06-05 01:06:27 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 418 ms / 5,000 ms |
| コード長 | 3,783 bytes |
| コンパイル時間 | 2,498 ms |
| コンパイル使用メモリ | 183,008 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-11 00:58:41 |
| 合計ジャッジ時間 | 3,632 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 14 |
ソースコード
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
template<int MOD>
struct ModInt {
static const int Mod = MOD;
unsigned x;
ModInt() : x(0) {}
ModInt(signed sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
ModInt(signed long long sig) { int sigt = sig % MOD; if(sigt < 0) sigt += MOD; x = sigt; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const {
signed a = x, b = MOD, u = 1, v = 0;
while(b) {
signed t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < 0) u += Mod;
ModInt res; res.x = (unsigned)u;
return res;
}
};
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1;
while(k) {
if(k & 1) r *= a;
a *= a;
k >>= 1;
}
return r;
}
typedef ModInt<1000000007> mint;
vector<bool> isprime;
vector<int> primes;
void sieve(int n) {
if((int)isprime.size() >= n + 1) return;
isprime.assign(n + 1, true);
isprime[0] = isprime[1] = false;
int sqrtn = (int)(sqrt(n * 1.) + .5);
for(int i = 2; i <= sqrtn; i ++) if(isprime[i]) {
for(int j = i * i; j <= n; j += i)
isprime[j] = false;
}
primes.clear();
for(int i = 2; i <= n; i ++) if(isprime[i])
primes.push_back(i);
}
typedef int FactorsInt;
typedef vector<pair<FactorsInt, int> > Factors;
void primeFactors(FactorsInt x, Factors &out_v) {
out_v.clear();
int sqrtx = (int)(sqrt(x*1.) + 10.5);
sieve(sqrtx);
for(vector<int>::const_iterator p = primes.begin(); p != primes.end(); ++ p) {
if(*p > sqrtx) break;
if(x % *p == 0) {
int t = 1;
x /= *p;
while(x % *p == 0) {
t ++;
x /= *p;
}
out_v.push_back(make_pair(*p, t));
}
}
if(x != 1) out_v.push_back(make_pair(x, 1));
}
template<typename T>T gcd(T x, T y) { if(y == 0)return x; else return gcd(y, x%y); }
void getDivisors(int N, vector<pair<int,int>> &res) {
Factors fs;
primeFactors(N, fs);
res.emplace_back(1, 1);
for(auto f : fs) {
int p = f.first, k = f.second;
for(int i = (int)res.size() - 1; i >= 0; -- i) {
int d = res[i].first, t = res[i].second;
t *= p - 1;
for(int j = 1; j <= k; ++ j) {
d *= p;
res.emplace_back(d, t);
t *= p;
}
}
}
sort(res.begin(), res.end());
}
int main() {
sieve(100000);
int H; int W; int K;
while(~scanf("%d%d%d", &H, &W, &K)) {
vector<pair<int,int>> divsH, divsW;
getDivisors(H, divsH);
getDivisors(W, divsW);
mint ans;
for(auto d : divsH) for(auto e : divsW) {
int a = d.first, b = e.first;
mint cnt = mint(K) ^ ((ll)W * H / a / b * gcd(a, b));
ans += cnt * d.second * e.second;
}
ans /= (ll)H * W;
printf("%d\n", ans.get());
}
return 0;
}
anta