結果
| 問題 |
No.3250 最小公倍数
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-08-04 21:16:06 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
(最新)
AC
(最初)
|
| 実行時間 | - |
| コード長 | 3,348 bytes |
| コンパイル時間 | 1,961 ms |
| コンパイル使用メモリ | 207,680 KB |
| 実行使用メモリ | 65,224 KB |
| 最終ジャッジ日時 | 2025-10-16 16:06:21 |
| 合計ジャッジ時間 | 4,435 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 1 |
| other | WA * 22 |
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:119:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
119 | freopen("lcm.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:120:12: warning: ignoring return value of ‘FILE* freopen(const char*, const char*, FILE*)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
120 | freopen("lcm.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:122:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
122 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
main.cpp:123:39: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
123 | for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
| ~~~~~^~~~~~~~~~~~~~~
main.cpp:126:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
126 | scanf("%d%d", &u, &v);
| ~~~~~^~~~~~~~~~~~~~~~
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 998244353;
const int N = 500005;
const int INF = 0x3f3f3f3f;
ll a[N];
ll powmod(ll x, ll y, ll mod) {
ll res = 1;
while (y) {
if (y & 1) res = res * x % mod;
y >>= 1;
x = x * x % mod;
}
return res;
}
bool Mr(ll n) {
if (n == 2) return true;
if (n <= 1 || n % 2 == 0) return false;
ll base[7] = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
ll u = n - 1, k = 0;
while (u % 2 == 0) u /= 2, k++;
for (auto x : base) {
if (x % n == 0) continue;
ll v = powmod(x, u, n);
if (v == 1 || v == n - 1) continue;
for (int j = 1; j <= k; j++) {
ll last = v;
v = (ll)v * v % n;
if (v == 1) {
if (last != n - 1) return false;
break;
}
}
if (v != 1) return false;
}
return true;
}
ll pr(ll n) {
static mt19937_64 sj(chrono::steady_clock::now().time_since_epoch().count());
uniform_int_distribution<ll> u0(1, n - 1);
ll c = u0(sj);
auto f = [&](ll x) { return ((ll)x * x + c) % n; };
ll x = 0, y = 0, s = 1;
for (int k = 1;; k <<= 1, y = x, s = 1) {
for (int i = 1; i <= k; i++) {
x = f(x);
s = (ll)s * abs(x - y) % n;
if (i % 127 == 0) {
ll d = __gcd(s, n);
if (d > 1) return d;
}
}
ll d = __gcd(s, n);
if (d > 1) return d;
}
return n;
}
void get_factor(ll n, unordered_map<ll, int>& mp) {
if (n == 1) return;
if (Mr(n)) {
mp[n]++;
return;
}
ll x = n;
while (x == n) x = pr(x);
get_factor(x, mp);
get_factor(n / x, mp);
}
vector<int> G[N], H[N];
int fa[N];
ll ans[N];
unordered_map<ll, int>* mp2[N];
unordered_map<ll, int> mp[N];
int sz[N], son[N];
void dfs1(int u, int fa) {
sz[u] = 1;
int mx = 0;
for (auto v : G[u]) {
if (v != fa) {
dfs1(v, u);
sz[u] += sz[v];
if (sz[v] > mx) {
mx = sz[v];
son[u] = v;
}
}
}
}
void dfs2(int u, int fa) {
for (auto v : G[u]) {
if (v != fa && v != son[u]) {
dfs2(v, u);
}
}
if (son[u]) {
dfs2(son[u], u);
mp2[u] = mp2[son[u]];
} else {
mp2[u] = new unordered_map<ll, int>();
}
for (auto v : G[u]) {
if (v != fa && v != son[u]) {
for (auto [x, y] : *mp2[v]) {
if ((*mp2[u])[x] < y) (*mp2[u])[x] = y;
}
delete mp2[v];
}
}
for (auto [x, y] : mp[u]) {
if ((*mp2[u])[x] < y) (*mp2[u])[x] = y;
}
ll res = 1;
for (auto [x, y] : *mp2[u]) res = res * powmod(x, y, mod) % mod;
ans[u] = res;
}
int main() {
freopen("lcm.in", "r", stdin);
freopen("lcm.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
for (int i = 1; i < n; i++) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; i++) get_factor(a[i], mp[i]);
dfs1(1, 0);
dfs2(1, 0);
for (int i = 1; i <= n; i++) printf("%lld\n", ans[i]);
return 0;
}