結果
| 問題 | No.2479 Sum of Squares |
| コンテスト | |
| ユーザー |
ococonomy1
|
| 提出日時 | 2023-09-22 21:22:28 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,853 bytes |
| コンパイル時間 | 896 ms |
| コンパイル使用メモリ | 109,096 KB |
| 最終ジャッジ日時 | 2025-02-17 00:14:02 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
// #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;
}
ococonomy1