結果

問題 No.28 末尾最適化
ユーザー tu-satu-sa
提出日時 2018-06-19 06:22:37
言語 C++14
(gcc 10.2.0 + boost 1.73.0)
結果
WA  
実行時間 -
コード長 6,749 Byte
コンパイル時間 1,551 ms
使用メモリ 4,976 KB
最終ジャッジ日時 2021-04-14 17:35:03
合計ジャッジ時間 2,316 ms
ジャッジサーバーID
(参考情報)
judge6 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 2 ms
3,416 KB
testcase_01 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

////////////////////////////////////////
///  tu3 pro-con template            ///
////////////////////////////////////////
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <numeric>
#include <functional>
#include <vector>
#include <queue>
#include <string>
#include <complex>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <regex>
#include <type_traits>
#include <mutex>
using namespace std;

//// MACRO ////
#define countof(a) (sizeof(a)/sizeof(a[0]))

#define REP(i,n) for (int i = 0; i < (n); i++)
#define RREP(i,n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i,s,n) for (int i = (s); i < (n); i++)
#define RFOR(i,s,n) for (int i = (n)-1; i >= (s); i--)
#define pos(c,i) c.being() + (i)
#define allof(c) c.begin(), c.end()
#define aallof(a) a, countof(a)
#define partof(c,i,n) c.begin() + (i), c.begin() + (i) + (n)
#define apartof(a,i,n) a + (i), a + (i) + (n)
#define long long long

#define EPS 1e-9
#define INF (1L << 28)
#define LINF (1LL << 60)

#define PREDICATE(t,a,exp) [&](const t & a) -> bool { return exp; }
#define COMPARISON_T(t) bool(*)(const t &, const t &)
#define COMPARISON(t,a,b,exp) [&](const t & a, const t & b) -> bool { return exp; }
#define CONVERTER(TSrc,t,TDest,exp) [&](const TSrc &t)->TDest { return exp; }

inline int sign_of(double x) { return (abs(x) < EPS ? 0 : x > 0 ? 1 : -1); }
inline bool inRange(int val, int min, int max) { return val >= min && val < max; }
inline bool inRange(double val, double min, double max) { return val - min > -EPS && val - max < EPS; }
inline bool inRange(int x, int y, int W, int H) { return x >= 0 && x < W && y >= 0 && y < H; } // W,H含まない

template <class T> struct vevector : public vector<vector<T>> { vevector(int n = 0, int m = 0, const T &initial = T()) : vector<vector<T>>(n, vector<T>(m, initial)) { } };
template <class T> struct vevevector : public vector<vevector<T>> { vevevector(int n = 0, int m = 0, int l = 0, const T &initial = T()) : vector<vevector<T>>(n, vevector<T>(m, l, initial)) { } };
template <class T> struct vevevevector : public vector<vevevector<T>> { vevevevector(int n = 0, int m = 0, int l = 0, int k = 0, const T &initial = T()) : vector<vevevector<T>>(n, vevevector<T>(m, l, k, initial)) { } };

//// i/o helper ////

namespace std {
template <class T1, class T2> inline istream & operator >> (istream & in, pair<T1, T2> &p) { in >> p.first >> p.second; return in; }
template <class T1, class T2> inline ostream & operator << (ostream &out, const pair<T1, T2> &p) { out << p.first << " " << p.second; return out; }
}
template <class T> T read() { T t; cin >> t; return t; }
template <class T> vector<T> read(int n) { vector<T> v; REP(i, n) { v.push_back(read<T>()); } return v; }
template <class T> vevector<T> read(int n, int m) { vevector<T> v; REP(i, n) v.push_back(read<T>(m)); return v; }
template <class T> vector<T> readjag() { return read<T>(read<int>()); }
template <class T> vevector<T> readjag(int n) { vevector<T> v; REP(i, n) v.push_back(readjag<T>()); return v; }

template <class T> struct iter_pair_t { T beg, end; };
template <class T> iter_pair_t<T> iter_pair(T beg, T end) { return iter_pair_t<T>{beg, end}; }
template <class T> ostream & operator << (ostream &out, const iter_pair_t<T> &v)
{
	std::copy(v.beg, v.end, ostream_iterator<typename decltype(v.beg)::reference>(out, " "));
	return out;
}
template <class T1> ostream & operator << (ostream &out, const vector<T1> &v) { return out << iter_pair(begin(v), end(v)); }
template <class T1> ostream & operator << (ostream &out, const set<T1> &v) { return out << iter_pair(begin(v), end(v)); }
template <class T1, class T2> ostream & operator << (ostream &out, const map<T1,T2> &v) { return out << iter_pair(begin(v), end(v)); }

struct _Reader { istream &cin;template <class T> _Reader operator ,(T &rhs) { cin >> rhs; return *this; } };
struct _Writer { ostream &cout; bool f{false}; template <class T> _Writer operator ,(const T &rhs) { cout << (f ? " " : "") << rhs; f = true; return *this; } };
#define READ(t,...) t __VA_ARGS__; (_Reader{cin}), __VA_ARGS__
#define WRITE(...) (_Writer{cout}), __VA_ARGS__; cout << endl
#define DEBUG(...) (_Writer{cerr}), __VA_ARGS__; cerr << endl

//// number ////
// 最大公約数
template <class T> inline T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; }

vevector<int> divMap(37, 0);
void initDivMap()
{
	FOR(i, 2, 36)
	{
		vector<int> m;
		FOR(j, 1, i)
		{
			m.push_back(j);
		}
		sort(allof(m), COMPARISON(int, a, b, gcd(a, i) < gcd(b, i)));
		divMap[i] = m;
	}
}

// 約数
inline vector<int> allDivisor(int n)
{
	vector<int> r;
	for (int i = 1; i * i <= n; i++)
	{
		if (n % i == 0) { r.push_back(i); }
	}
	int rsz = r.size() - (r.back() * r.back() == n);
	RREP(i, rsz) { r.push_back(n / r[i]); }

	return r;
}

// エラトステネス
vector<unsigned char> isPrime;
vector<int> primes;
void initPrimes(int n)
{
	isPrime = vector<unsigned char>(n + 1, true);
	primes.reserve(max(n / 10, 10000));

	isPrime[0] = isPrime[1] = false;
	FOR(i, 2, n + 1)
	{
		if (!isPrime[i]) continue;
		primes.push_back(i);
		for (int j = i * 2; j <= n; j += i)
			isPrime[j] = false;
	}
}


// 素因数分解 //前提: initPrims(sqrt(n)+1)
template <class TInt>
map<int, int> factsMap(TInt n)
{
	map<int, int> result;
	for (auto p : primes)
	{
		if (n <= 1) { break; }
		if (n % p != 0) { continue; }

		int k = 0;
		while (n % p == 0)
		{
			n /= p;
			k++;
		}
		result[p] = k;
	}
	return result;
}
void solve();
int main()
{
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	
	initDivMap();
	initPrimes(100);
	READ(int, Q);
	REP(qqqq, Q) solve();
	
	return 0;
}

////////////////////
/// template end ///
////////////////////

void solve()
{
	READ(int, seed, N, K, B);

	map<int, vector<int>> mp;
	map<int, int> mp2;

	long x = seed;
	REP(i, N + 1)
	{
		long y = x;
		int zeros = 0;
		while (y % B == 0)
		{
			zeros++;
			y /= B;
		}
		long d = y % B;
		if (mp.count(zeros) == 0)
		{
			mp[zeros] = vector<int>(B);
		}
		mp[zeros][d]++;
		mp2[zeros]++;

		x = 1 + (x * x + x * 12345) % 100000009;
	}

	auto factors = vector<int>(B);

	int minZ = 0;
	for (auto &z : mp)
	{
		for (auto &v: divMap[B])
		{
			while (z.second[v])
			{
				if (K == 0)
				{
					goto ok;
				}

				K--;
				z.second[v]--;
				factors[v]++;
				minZ += z.first;
			}
		}
	}
	ok:;

	map<int, int> pf;
	for (auto d : divMap[B])
	{
		auto xfm = factsMap(d);
		for (auto f : xfm)
		{
			pf[f.first] += f.second * factors[d];
		}
	}

	int minz = INF;
	for (auto f : factsMap(B))
	{
		minz = min(minz, pf[f.first] / f.second);
	}

	WRITE(minZ + minz) ;
}
0