結果
問題 | No.737 PopCount |
ユーザー |
![]() |
提出日時 | 2022-11-01 23:09:48 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 7,011 bytes |
コンパイル時間 | 2,213 ms |
コンパイル使用メモリ | 188,192 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-07-16 11:07:06 |
合計ジャッジ時間 | 3,155 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 15 |
ソースコード
/*** @FileName a.cpp* @Author kanpurin* @Created 2022.11.01 23:09:39**/#include "bits/stdc++.h"using namespace std;typedef long long ll;template< int MOD >struct mint {public:unsigned int x;mint() : x(0) {}mint(long long v) {long long w = (long long)(v % (long long)(MOD));if (w < 0) w += MOD;x = (unsigned int)(w);}mint(std::string &s) {unsigned int z = 0;for (int i = 0; i < s.size(); i++) {z *= 10;z += s[i] - '0';z %= MOD;}x = z;}mint operator+() const { return *this; }mint operator-() const { return mint() - *this; }mint& operator+=(const mint &a) {if ((x += a.x) >= MOD) x -= MOD;return *this;}mint& operator-=(const mint &a) {if ((x -= a.x) >= MOD) x += MOD;return *this;}mint& operator*=(const mint &a) {unsigned long long z = x;z *= a.x;x = (unsigned int)(z % MOD);return *this;}mint& operator/=(const mint &a) {return *this = *this * a.inv(); }friend mint operator+(const mint& lhs, const mint& rhs) {return mint(lhs) += rhs;}friend mint operator-(const mint& lhs, const mint& rhs) {return mint(lhs) -= rhs;}friend mint operator*(const mint& lhs, const mint& rhs) {return mint(lhs) *= rhs;}friend mint operator/(const mint& lhs, const mint& rhs) {return mint(lhs) /= rhs;}friend bool operator==(const mint& lhs, const mint& rhs) {return lhs.x == rhs.x;}friend bool operator!=(const mint& lhs, const mint& rhs) {return lhs.x != rhs.x;}friend std::ostream& operator<<(std::ostream &os, const mint &n) {return os << n.x;}friend std::istream &operator>>(std::istream &is, mint &n) {unsigned int x;is >> x;n = mint(x);return is;}mint inv() const {assert(x);return pow(MOD-2);}mint pow(long long n) const {assert(0 <= n);mint p = *this, r = 1;while (n) {if (n & 1) r *= p;p *= p;n >>= 1;}return r;}mint sqrt() const {if (this->x < 2) return *this;if (this->pow((MOD-1)>>1).x != 1) return mint(0);mint b = 1, one = 1;while (b.pow((MOD-1) >> 1) == 1) b += one;long long m = MOD-1, e = 0;while (m % 2 == 0) m >>= 1, e += 1;mint x = this->pow((m - 1) >> 1);mint y = (*this) * x * x;x *= (*this);mint z = b.pow(m);while (y.x != 1) {int j = 0;mint t = y;while (t != one) j += 1, t *= t;z = z.pow(1LL << (e-j-1));x *= z; z *= z; y *= z; e = j;}return x;}};constexpr int MOD = 1e9+7;struct Monoid {mint<MOD> v1;mint<MOD> v2;mint<MOD> v3;mint<MOD> num;bool undef = true;Monoid() { *this = zero(); }Monoid(mint<MOD> v1, mint<MOD> v2,mint<MOD> v3, mint<MOD> num, bool undef = true) : v1(v1),v2(v2),v3(v3),num(num),undef(undef) {}static Monoid zero() { return Monoid(0,0,0,0); }static Monoid e() { return Monoid(0,0,0,1,false); }Monoid& operator+=(const Monoid &a) {if (this->undef) *this = a;else if (!a.undef) {this->v1 += a.v1;this->v2 += a.v2;this->v3 += a.v3;this->num += a.num;}return *this;}Monoid& operator*=(int c) {this->v1 = (this->v1+this->v2*c)*2+this->v3*c+this->num*c;this->v2 = this->v2*2+this->num*c;this->v3 = this->v3+this->num*c;return *this;}friend Monoid operator+(const Monoid& a, const Monoid& b) {return Monoid(a) += b;}friend Monoid operator*(const Monoid& a, int c) {return Monoid(a) *= c;}friend std::ostream& operator<<(std::ostream &os, const Monoid &x) {return os << x.v1;}};struct Automaton {vector<vector<int>> delta;vector<bool> is_accept, is_reject;int qsize;int init;int alphabet_size = 10;inline int next(int state, int c) const { return delta[state][c]; }inline bool accept(int state) const { return is_accept[state]; }inline bool reject(int state) const { return is_reject[state]; }inline int size() const {return qsize; }};template<typename Automaton>Monoid digitDP(const string &s, const Automaton &dfa, bool eq = 1) {vector<vector<Monoid>> dp(2,vector<Monoid>(dfa.size(),Monoid::zero()));dp[1][dfa.init] = Monoid::e();for (int i = 0; i < s.size(); i++) {vector<vector<Monoid>> dp2(2,vector<Monoid>(dfa.size(),Monoid::zero()));for (int tight = 0; tight <= 1; tight++) {for (int state = 0; state < dfa.size(); state++) {if (dfa.reject(state) || dp[tight][state].undef) continue;int lim = (tight ? s[i] - '0' : dfa.alphabet_size - 1);for (int c = 0; c <= lim; c++) {int tight_ = tight && c == lim;int state_ = dfa.next(state,c);if (dfa.reject(state_)) continue;dp2[tight_][state_] += dp[tight][state]*c;}}}dp = move(dp2);}Monoid ans = Monoid::zero();for (int tight = 0; tight <= eq; tight++)for (int state = 0; state < dfa.size(); state++)if (dfa.accept(state)) ans += dp[tight][state];return ans;}struct SimpleAutomaton : public Automaton {private:void initializer() {qsize = 1;init = 0;set_delta();set_is_accept();set_is_reject();}void set_delta() {delta.resize(qsize,vector<int>(alphabet_size,0));}void set_is_accept() {is_accept.resize(qsize,true);}void set_is_reject() {is_reject.resize(qsize,false);}public:SimpleAutomaton(int alpha_size = 10) {alphabet_size = alpha_size;initializer();}};string binarynumber(long long x, int len = -1) {string res;while(x) {if (x&1) res.push_back('1');else res.push_back('0');x>>=1;}while((int)res.size() < len) res.push_back('0');reverse(res.begin(), res.end());return res;}int main() {long long n;cin >> n;string ns = binarynumber(n);cout << digitDP(ns,SimpleAutomaton(2)) << endl;return 0;}