結果

問題 No.3332 Consecutive Power Sum (Small)
コンテスト
ユーザー Vi24E
提出日時 2025-10-01 03:26:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 3,913 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 221,076 KB
実行使用メモリ 15,944 KB
最終ジャッジ日時 2025-11-02 21:11:05
合計ジャッジ時間 6,143 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other WA * 8 TLE * 1 -- * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

//a ^ b mod m
//aがlong longじゃないと危険
template<typename T = long long, typename U, typename V>
T modpow(T a, U b, V m) {
	T res = 1;
	a %= m;
	while (b) {
		if (b & 1) res = (res * a) % m;
		a = (a * a) % m;
		b >>= 1;
	}
	return res;
}

// miller-Rabin 素数判定(O(log(N)))
bool isprime(long long N) {
	if (N <= 1) return 0;
	if (N == 2) return 1;
	if (N % 2 == 0) return 0;
	vector<long long> a = {2, 325, 9375, 28178, 450775, 9780504, 1795265022};
	long long s = 0, d = N - 1;
	while (d % 2 == 0) {
		s++;
		d >>= 1;
	}
	for (auto a : a) {
		if (a % N == 0) return 1;
		long long t, x = modpow<__int128_t>(a, d, N);
		if (x != 1) {
			for (t = 0; t < s; t++) {
				if (x == N - 1) break;
				x = __int128_t(x) * x % N;
			}
			if (t == s) return 0;
		}
	}
	return 1;
}

template<typename T>
T gcd(T a, T b) {
	if (a > b) swap(a, b);
	while (a != 0){
		b %= a;
		swap(a, b);
	}
	return b;
}

// Pollard のロー法
long long pollard(long long N) {
	if (N % 2 == 0) return 2;
	if (isprime(N)) return N;

	auto f = [&](long long x) -> long long {
		return (__int128_t(x) * x + 1) % N;
	};
	long long step = 0;
	while (1) {
		++step;
		long long x = step, y = f(x);
		while (1) {
			long long p = gcd(y - x + N, N);
			if (p == 0 || p == N) break;
			if (p != 1) return p;
			x = f(x);
			y = f(f(y));
		}
	}
}

//素因数分解(O(N^1/4)log(N)?)
vector<long long> pfact(long long N) {
	if (N == 1) return {};
	long long p = pollard(N);
	if (p == N) return {p};
	vector<long long> left = pfact(p);
	vector<long long> right = pfact(N / p);
	left.insert(left.end(), right.begin(), right.end());
	sort(left.begin(), left.end());
	return left;
}

vector<long long> get_factor(long long N) {
    if (N == 1) return {1};
    
    vector<long long> prime_factors = pfact(N);
    
    map<long long, int> factor_count;
    for (long long p : prime_factors) {
        factor_count[p]++;
    }
    
    vector<long long> divisors = {1};
    
    for (auto& [prime, count] : factor_count) {
        vector<long long> new_divisors;
        for (long long div : divisors) {
            long long power = 1;
            for (int i = 0; i <= count; i++) {
                new_divisors.push_back(div * power);
                power *= prime;
            }
        }
        divisors = new_divisors;
    }
    
    sort(divisors.begin(), divisors.end());
    return divisors;
}

ll power(ll x, int e){
    ll t = 1;
    for (int i = 0; i < e; i++) t *= x;
    return t;
}

vector<array<ll, 3>> ans;

void solve(ll n, int e) {
    ll l = 1, r = 1;
    ll t = 1;
    while (power(r, e) <= n){
        if (t == n){
            ans.push_back({e, l, r});
        }
        if (t <= n) {
            r++;
            t += power(r, e);
        }
        else {
            t -= power(l, e);
            l++;
        }
    }
    return;
}

__int128_t square_sum(ll x){
    return (__int128_t)x * (x + 1) * (2 * x + 1) / 6;
}

void solve2(ll n){
    vector<ll> factor = get_factor(n);
    for (ll d : factor){
        auto check = [&](ll mid)->bool {
            return square_sum(mid + d - 1) - square_sum(mid - 1) <= n;
        };

        ll ok = 1, ng = 1<<30;
        if (square_sum(ok + d - 1) - square_sum(ok - 1) >= n){
            continue;
        }

        while (abs(ok - ng) > 1){
            ll mid = (ok + ng) / 2;
            if (check(mid)) ok = mid;
            else ng = mid;
        }
        if (square_sum(ok + d - 1) - square_sum(ok - 1) == n){
            ans.push_back({2, ok, ok + d - 1});
        }
    }
}

int main(){
    ll n;
    cin >> n;
    assert(1 <= n && n <= 1000000000000000000);

    solve2(n);
    for (int i = 3; i < 60; i++){
        solve(n, i);
    }

    sort(ans.begin(), ans.end());
    cout << ans.size() << "\n";
    for (auto [e, l, r] : ans){
        cout << e << " " << l << " " << r << endl;
    }
}
0