結果
問題 | No.2744 Power! or +1 |
ユーザー |
![]() |
提出日時 | 2024-06-17 14:42:30 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,796 bytes |
コンパイル時間 | 711 ms |
コンパイル使用メモリ | 65,932 KB |
最終ジャッジ日時 | 2025-02-21 23:06:01 |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 3 WA * 6 |
コンパイルメッセージ
main.cpp: In function ‘int main()’: main.cpp:87:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 87 | scanf("%d%d%d%d", &n, &a, &b, &c); | ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
ソースコード
/* -*- coding: utf-8 -*-** 2744.cc: No.2744 Power! or +1 - yukicoder*/#include<cstdio>#include<vector>#include<algorithm>#include<utility>using namespace std;/* constant */const int MAX_N = 200000;const int MAX_N2 = MAX_N * 2;const int MAX_P = 200000;/* typedef */using ll = long long;using vb = vector<bool>;using vi = vector<int>;using pii = pair<int,int>;using vpii = vector<pii>;/* global variables */vb primes;vi pnums;ll dp[MAX_N2 + 1];/* subroutines */int gen_primes(int maxp) {primes.assign(maxp + 1, true);primes[0] = primes[1] = false;int p;for (p = 2; p * p <= maxp; p++)if (primes[p]) {pnums.push_back(p);for (int q = p * p; q <= maxp; q += p) primes[q] = false;}for (; p <= maxp; p++)if (primes[p]) pnums.push_back(p);return (int)pnums.size();}bool prime_decomp(int n, vpii& pds) {pds.clear();for (auto pi: pnums) {if (pi * pi > n) {if (n > 1) pds.push_back(pii(n, 1));return true;}if (n % pi == 0) {int fi = 0;while (n % pi == 0) n /= pi, fi++;pds.push_back(pii(pi, fi));}}return false;}inline void setmin(ll &a, ll b) { if (a > b) a = b; }template <typename T>T gcd(T m, T n) { // m >= 0, n >= 0if (m < n) swap(m, n);while (n > 0) {T r = m % n;m = n;n = r;}return m;}/* main */int main() {gen_primes(MAX_P);int n, a, b, c;scanf("%d%d%d%d", &n, &a, &b, &c);int n2 = n * 2;ll b2 = (ll)b * b, b3 = b2 * b;vpii npds;prime_decomp(n, npds);//for (auto [p, f]: npds) printf(" %d^%d", p, f); putchar('\n');dp[0] = dp[1] = 0;for (int i = 2; i <= n2; i++) dp[i] = dp[i - 1] + a;ll f = 1;for (int x = 1; x < n2; x++) {// op 1setmin(dp[x + 1], dp[x] + a);// op 2if (x > 1) {ll x2 = (ll)x * x, x3 = x2 * x;if (x2 <= n2) setmin(dp[x2], dp[x] + b2);if (x3 <= n2) setmin(dp[x3], dp[x] + b3);}// op 3if (x > 1 && f < n2) {f *= x;if (f <= n2 && f > x) setmin(dp[f], dp[x] + c);}}//printf(" dp[n]=%lld\n", dp[n]);ll mind = dp[n];for (int x = 2; x < n2; x++) {int maxk = 0;for (auto [p, f]: npds) {if (x % p != 0) { maxk = 0; break; }int fx = 0;for (int y = x; y % p == 0; y /= p) fx++;maxk = max(maxk, (f + fx - 1) / fx);}if (maxk >= 2) {if (maxk & 1) {int c2 = (maxk - 3) / 2;setmin(mind, dp[x] + b2 * c2 + b3);}else {int c2 = maxk / 2;setmin(mind, dp[x] + b2 * c2);}}}//printf(" mind=%lld\n", mind);for (int x = 2, r = n; x < n2; x++) {if (r > 1) r /= gcd(x, r);if (r == 1) setmin(mind, dp[x] + c);}printf("%lld\n", mind);return 0;}