結果

問題 No.319 happy b1rthday 2 me
ユーザー motimoti
提出日時 2015-12-14 18:40:29
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,748 bytes
コンパイル時間 2,379 ms
コンパイル使用メモリ 100,628 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-13 16:38:11
合計ジャッジ時間 2,878 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <iomanip>
#include <assert.h>
#include <array>
#include <cstdio>
#include <cstring>
#include <random>
#include <functional>
#include <numeric>
#include <bitset>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define minimize(a, x) a = std::min(a, x)
#define maximize(a, x) a = std::max(a, x)

typedef long long ll;
int const inf = 1<<29;

ll solve(string const& S, bool last) {

  const int N = S.size();

  ll ret = 0;
  
  ll dp[14][2][2][14*14];  // i桁目, 最大値未満, 前の桁が'1', "12"の個数
  zero(dp);
  dp[0][0][0][0] = 1;
  rep(i, N) rep(less, 2) rep(prevone, 2) rep(twcount, 14*14) {
    auto const& curr = dp[i][less][prevone][twcount];
    const int dmax = less ? 9 : (S[i] - '0');
    rep(d, dmax + 1) {
      auto const ni       = i + 1;
      auto const nprevone = d == 1;
      auto const nless    = less || (d < dmax);
      auto const ntwcount = twcount + (prevone && d == 2);
      auto& next = dp[ni][nless][nprevone][ntwcount];
      next += curr;
    }
  }

  rep(i, 2) rep(j, 2) rep(k, 14*14) {
    ret += dp[N][i][j][k] * k;
  }

  if(last) {
    rep(i, 2) rep(j, 14*14) {
      ret -= dp[N][0][i][j] * j;
    }
  }

  return ret;
}

ll count2_2(string const& S, bool last) {

  int N = S.size();

  ll dp[14][2][2][2][2];  // i桁目, 最大値未満, 前の桁が'2', leading0, はじめの桁が'2'
  zero(dp);
  dp[0][0][0][1][0] = 1;
  rep(i, N) rep(less, 2) rep(prev2, 2) rep(leading0, 2) rep(start2, 2) {
    auto const& curr = dp[i][less][prev2][leading0][start2];
    int dmax = less ? 9 : (S[i] - '0');
    rep(d, dmax + 1) {
      auto const ni         = i + 1;
      auto const nless      = less || (d < dmax);
      auto const nprev2     = d == 2;
      auto const nleading0  = leading0 && (d == 0);
      auto const nstart2    = start2 || (leading0 && (d == 2));
      auto& next = dp[ni][nless][nprev2][nleading0][nstart2];
      next += curr;
    }
  }

  ll ret = 0;

  rep(i, 2) {
    if(last && i == 0) { continue; }
    ret += dp[N][i][1][0][1];
  }

  return ret;
}

int main() {

  string A, B; cin >> A >> B;
  /*
  cout << solve(A, false) << endl;
  cout << solve(B, false) << endl;
  cout << "::" << count2_2(A, true) << endl;
  cout << count2_2(B, false) << endl;
  */
  cout << solve(B, false) - solve(A, true) + count2_2(B, false) - (count2_2(A, true) + (A.back() != '2')) << endl;
  
  return 0;
}
0