結果

問題 No.1252 数字根D
ユーザー どららどらら
提出日時 2020-10-09 22:26:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 7 ms / 2,000 ms
コード長 1,096 bytes
コンパイル時間 1,608 ms
コンパイル使用メモリ 165,060 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-27 18:11:35
合計ジャッジ時間 2,203 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 6 ms
4,380 KB
testcase_05 AC 7 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 6 ms
4,380 KB
testcase_09 AC 6 ms
4,380 KB
testcase_10 AC 6 ms
4,376 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 6 ms
4,376 KB
testcase_13 AC 6 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

ll calc(ll d, ll x){
  if(x < d) return x;
  ll p = 1;
  while(p<x){
    p *= d;
  }
  ll ret = 0;
  while(p>0){
    ret += x/p;
    x%=p;
    p/=d;
  }
  return calc(d, ret);
}

ll solve(ll d, ll A, ll B){
  ll ret = 0;
  
  {
    ll n = B - (A-1);
    ll a = calc(d, A);
    ll x = d-1-(a-1);
    if(B < A+x){
      ll b = calc(d, B);
      ret += n * (a+b) / 2;
      return ret;
    }else{
      ll b = d-1;
      ret += x * (a+b) / 2;
    }
    A+= x;
  }
  {
    ll n = B - (A-1);  
    ll x = n / (d-1);
    ret += x * (d-1) * (1 + d-1) / 2;
    A += x * (d-1);
  }
  {
    ll n = B - (A-1);
    ret += n * (1 + n) / 2;
  }
  
  return ret;
}

int main(){
  int T;
  cin >> T;
  rep(t,T){
    ll d, A, B;
    cin >> d >> A >> B;
    cout << solve(d, A, B) << endl;
  }
  
  return 0;
}


0