結果

問題 No.2479 Sum of Squares
ユーザー ococonomy1ococonomy1
提出日時 2023-09-22 21:22:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,853 bytes
コンパイル時間 2,032 ms
コンパイル使用メモリ 109,052 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-22 21:22:32
合計ジャッジ時間 2,480 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #pragma GCC target("avx2")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
using lg = long long;
using pii = pair<int, int>;
using pll = pair<lg, lg>;
#define TEST cerr << "TEST" << endl
#define AMARI 998244353
// #define AMARI 1000000007
#define TEMOTO ((sizeof(long double) == 16) ? false : true)
#define TIME_LIMIT 1980 * (TEMOTO ? 1 : 1000)
#define el '\n'
#define El '\n'


//floor(sqrt(x))を整数で返す。にぶたんしているが幅が定数なので計算時間はO(1) にぶたん内で重い動作もない(代入、掛け算、ビットシフト、大小比較だけ)
long long ococo_floor_lsqrt(long long x){

    //この仕様で嬉しいか?
    if(x < 0){
        return -1;
    }
    if(x < INT_MAX / 2){
        int ix = x;
        if(x == 0)return 0;
        if(x < 4)return 1;
        if(x < 9)return 2;

        int l = std::sqrt((double)x) - 2,r = std::sqrt((double)x) + 2;
        for(int i = l; i <= r + 1; i++){
            if(i * i > ix)return i - 1;
        }
        return r + 1;
    }
    //doubleの仮数部が52bitでlong longが64bitだから差が12bitのため、誤差が2**12=4096を超えないんじゃないかと思ってこの範囲でにぶたんをしているが、
    //全然これは嘘かもしれないので、めちゃくちゃ要検証である。
    //一旦±10で見てダメそうだったら5000広げる方向性にした。これ以上の高速化が必要なパターンはあんまないだろうしこれでええやろ
    //とりあえずこのにぶたんは、sqrt(x)の答えが[l,r]のどこかにあるものとしている
    long long l = std::sqrt((double)x) - 10,r = std::sqrt((double)x) + 10;
    if(l * l > x)l -= 5000;
    if(r * r < x)r += 5000;
    while(r - l >= 2){
        long long c = (r + l) >> 1;
        if(c * c <= x)l = c;
        else r = c;
    }
    for(long long i = l; i <= r + 1; i++){
        if(i * i > x)return i - 1;
    }
    return r + 1;
}

#define MULTI_TEST_CASE false
void solve(void) {
    lg s;
    cin >> s;
    vector<lg> ans;
    while(s){
        lg temp = ococo_floor_lsqrt(s);
        ans.push_back(temp * temp);
        s -= temp * temp;
    }
    cout << ans.size() << el;
    for(int i  = 0; i < ans.size(); i++){
        cout << ans[i] << ' ';
    }
    cout << el;
    return;
}

void calc(void) {
    return;
}

int main(void) {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    calc();
    int t = 1;
    if(MULTI_TEST_CASE) cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}
0