結果
問題 | No.491 10^9+1と回文 |
ユーザー |
|
提出日時 | 2020-12-25 01:23:44 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 17 ms / 1,000 ms |
コード長 | 2,713 bytes |
コンパイル時間 | 1,499 ms |
コンパイル使用メモリ | 132,012 KB |
最終ジャッジ日時 | 2025-01-17 06:55:48 |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 103 |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <queue> #include <string> #include <map> #include <set> #include <stack> #include <tuple> #include <deque> #include <array> #include <numeric> #include <bitset> #include <iomanip> #include <cassert> #include <chrono> #include <random> #include <limits> #include <iterator> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <cstring> #include <unordered_map> using namespace std; using ll = long long; using P = pair<int, int>; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template<class T> inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template<class T> inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); ll N; cin >> N; // 回文かつ 10^9+1 の倍数であることから、 // 1 2 3 4 4 3 2 1 0 1 2 3 4 4 3 2 1 // のようになっている。 ll ans = 0; int cnt = 1, zeros = 8; for(ll i = 1000000000LL; i <= min((ll)1e+18 - 1, N); i *= 10){ if(N / i >= 10){ ll add = cnt % 2 ? (cnt / 2 ? 10 : 9) : 1; for(int j = 0; j < cnt / 2; ++j) add *= (j ? 10 : 9); ans += add; } else{ // 自由に決められる値は最大でも 4 桁 // (dfs が呼び出される回数) <= 10^4 auto dfs = [&](auto&& self, string s = "") -> void { if((int)s.length() == cnt / 2){ string t = s; reverse(t.begin(), t.end()); if(cnt % 2 == 0){ string num = s + t; for(int i = 0; i < zeros; ++i) num += '0'; num += s + t; ans += stoll(num) <= N; } else{ for(char ch = (s.length() ? '0' : '1'); ch <= '9'; ++ch){ string num = s + ch + t; for(int i = 0; i < zeros; ++i) num += '0'; num += s + ch + t; ans += stoll(num) <= N; } } return; } for(char ch = (s.length() ? '0' : '1'); ch <= '9'; ++ch){ self(self, s + ch); } }; dfs(dfs); } cnt += 1; zeros -= 1; } cout << ans << endl; return 0; }