結果

問題 No.528 10^9と10^9+7と回文
ユーザー koba-e964koba-e964
提出日時 2017-07-10 02:14:58
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 1,000 ms
コード長 1,668 bytes
コンパイル時間 664 ms
コンパイル使用メモリ 89,416 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-01 01:00:02
合計ジャッジ時間 1,847 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 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 5 ms
4,380 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 5 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 3 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 5 ms
4,376 KB
testcase_27 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef pair<int, int> PI;
const ll mod1 = 1e9;
const ll mod2 = 1e9 + 7;

ll powmod(ll a, ll e, ll mod) {
  ll sum = 1;
  ll cur = a;
  while (e > 0) {
    if (e % 2) {
      sum = sum * cur % mod;
    }
    cur = cur * cur % mod;
    e /= 2;
  }
  return sum;
}

ll rex(const string &s, ll mod) {
  int n = s.length();
  ll cur = 0;
  REP(i, 0, n) {
    cur = 10 * cur + (s[i] - '0');
    cur %= mod;
  }
  return cur;
}

ll calc(const string &s, ll mod) {
  int n = s.length();
  if (n % 2 == 1) {
    ll t = rex(s.substr(0, n / 2 + 1), mod);
    t += powmod(10, n / 2, mod) + mod - 1;
    t %= mod;
    string cp(s);
    REP(i, 0, n / 2) {
      cp[n - 1 - i] = s[i];
    }
    if (cp > s) {
      t = (t + mod - 1) % mod;
    }
    return t;
  }
  ll t = rex(s.substr(0, n / 2), mod);
  t += powmod(10, n / 2, mod) + mod - 1;
  t %= mod;
  string cp(s);
  REP(i, 0, n / 2) {
    cp[n - 1 - i] = s[i];
  }
  if (cp > s) {
    t = (t + mod - 1) % mod;
  }
  return t;
}

int main(void){
  string s;
  cin >> s;
  ll t1 = calc(s, mod1);
  ll t2 = calc(s, mod2);
  cout << t1 << endl << t2 << endl;
}
0