結果
| 問題 | 
                            No.28 末尾最適化
                             | 
                    
| コンテスト | |
| ユーザー | 
                             Min_25
                         | 
                    
| 提出日時 | 2016-07-11 22:08:16 | 
| 言語 | C++14  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                CE
                                 
                             
                            
                            (最新)
                                AC
                                 
                             
                            (最初)
                            
                            
                         | 
                    
| 実行時間 | - | 
| コード長 | 2,239 bytes | 
| コンパイル時間 | 1,078 ms | 
| コンパイル使用メモリ | 94,120 KB | 
| 最終ジャッジ日時 | 2025-01-29 02:39:41 | 
| 合計ジャッジ時間 | 1,541 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge4 / judge1 | 
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
            
            
            
            
            ただし、clay言語の場合は開発者のデバッグのため、公開されます。
コンパイルメッセージ
main.cpp: In function ‘void solve()’:
main.cpp:48:28: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |       int S, N, K, B; scanf("%d %d %d %d", &S, &N, &K, &B);
      |                       ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/string:43,
                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/istream:40,
                 from /usr/include/c++/13/sstream:40,
                 from /usr/include/c++/13/complex:45,
                 from main.cpp:10:
/usr/include/c++/13/bits/allocator.h: In destructor ‘std::_Vector_base<std::pair<int, int>, std::allocator<std::pair<int, int> > >::_Vector_impl::~_Vector_impl()’:
/usr/include/c++/13/bits/allocator.h:184:7: error: inlining failed in call to ‘always_inline’ ‘std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = std::pair<int, int>]’: target specific option mismatch
  184 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/13/vector:66,
                 from main.cpp:13:
/usr/include/c++/13/bits/stl_vector.h:133:14: note: called from here
  133 |       struct _Vector_impl
      |              ^~~~~~~~~~~~
            
            ソースコード
#pragma GCC optimize ("O3")
#pragma GCC target ("avx") // yukicoder
// #pragma GCC target ("sse4") // SPOJ, codechef
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstring>
#include <complex>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <functional>
#include <tuple>
#define _rep(_1, _2, _3, _4, name, ...) name
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, a, b) rep4(i, a, b, 1)
#define rep4(i, a, b, c) for (int i = int(a); i < int(b); i += int(c))
#define rep(...) _rep(__VA_ARGS__, rep4, rep3, rep2, _)(__VA_ARGS__)
using namespace std;
using i64 = long long;
using u8 = unsigned char;
using u32 = unsigned;
using u64 = unsigned long long;
using f80 = long double; 
struct ExactDiv {
  ExactDiv() {}
  ExactDiv(u32 n) : d(n) {
    t = u32(-1) / n;
    i = n; rep(_, 5) i *= 2 - i * n;
  }
  bool divide(u32 n) { return n * i <= t; }
  u32 d, i, t;
};
int X[11111];
void solve() {
  int Q;
  while (~scanf("%d", &Q)) {
    rep(i, Q) {
      int S, N, K, B; scanf("%d %d %d %d", &S, &N, &K, &B);
      vector< pair<int, int> > facs;
      for (int i = 2; i <= B; ++i) if (B % i == 0) {
        int e = 0;
        while (B % i == 0) B /= i, e += 1;
        facs.push_back({i, e});
      }
      X[0] = S;
      rep(i, 1, N + 1) {
        int x = X[i - 1];
        X[i] = 1 + i64(x) * (x + 12345) % 100000009;
      }
      int ans = 1e9;
      for (auto d : facs) {
        int p = d.first;
        int vs[32] = {};
        if (p & (p - 1)) {
          auto e = ExactDiv(p);
          rep(i, N + 1) {
            u32 x = X[i];
            int t = 0;
            while (e.divide(x)) x *= e.i, ++t;
            vs[t] += 1;
          }
        } else {
          rep(i, N + 1) vs[__builtin_ctz(X[i])] += 1;
        }
        int c = 0, t = 0;
        rep(i, 32) {
          if (c + vs[i] >= K) {
            t += i * (K - c);
            break;
          }
          t += vs[i] * i;
          c += vs[i];
        }
        ans = min(ans, t / d.second);
      }
      printf("%d\n", ans);
    }
  }
}
int main() {
  clock_t beg = clock();
  solve();
  clock_t end = clock();
  fprintf(stderr, "%.3f sec\n", double(end - beg) / CLOCKS_PER_SEC);
  return 0;
}
            
            
            
        
            
Min_25