結果

問題 No.757 チャンパーノウン定数 (2)
ユーザー heno239heno239
提出日時 2018-12-09 04:11:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 3,312 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 114,108 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-13 16:39:30
合計ジャッジ時間 3,682 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,356 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 1 ms
4,348 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 2 ms
4,352 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 10 ms
4,348 KB
testcase_29 AC 8 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 3 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 4 ms
4,348 KB
testcase_34 AC 5 ms
4,348 KB
testcase_35 AC 3 ms
4,352 KB
testcase_36 AC 2 ms
4,352 KB
testcase_37 AC 7 ms
4,348 KB
testcase_38 AC 9 ms
4,372 KB
testcase_39 AC 11 ms
4,352 KB
testcase_40 AC 11 ms
4,348 KB
testcase_41 AC 11 ms
4,352 KB
testcase_42 AC 11 ms
4,352 KB
testcase_43 AC 11 ms
4,352 KB
testcase_44 AC 11 ms
4,348 KB
testcase_45 AC 11 ms
4,348 KB
testcase_46 AC 10 ms
4,352 KB
testcase_47 AC 11 ms
4,352 KB
testcase_48 AC 10 ms
4,372 KB
testcase_49 AC 11 ms
4,356 KB
testcase_50 AC 1 ms
4,352 KB
testcase_51 AC 1 ms
4,348 KB
testcase_52 AC 2 ms
4,372 KB
testcase_53 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef unsigned long long ul;
int b;
string minu(string x, string y) {
	int len = x.length();
	int len2 = y.length();
	string res;
	rep1(i, len) {
		int t = x[len - i] - '0';
		if (len2 - i >= 0) {
			t -= y[len2 - i] - '0';
		}
		while (t < 0) {
			t += b; x[len - i - 1]--;
		}
		res.push_back('0' + t);
	}
	reverse(res.begin(), res.end()); 

	while (!res.empty() && res[0] == '0') {
		res.erase(res.begin());
	}
	return res;
}
string make(int n) {
	string res;
	if (n == 0)return "0";
	else if (n == 1) {
		res.push_back('0' + b - 1); return res;
	}
	else {
		int c = n - 1;
		while (c) {
			res.push_back('0' + (c%b));
			c /= b;
		}
		reverse(res.begin(), res.end());
		rep(i, n - 1) {
			res.push_back('0' + b - 2);
		}
		res.push_back('0' + b - 1);
	}
	return res;
}
bool great(string x, string y) {
	if (x.length() != y.length()) {
		return x.length() > y.length();
	}
	return x >= y;
}
pair<string, int> q(string x, int d) {
	int len = x.length();
	string res;
	int c = 0;
	rep(i, len) {
		c += x[i] - '0';
		res.push_back('0' + (c / d)); c %= d;
		c *= b;
	}
	while (!res.empty() && res[0] == '0') {
		res.erase(res.begin());
	}
	return { res,c / b };
}
string make_base(int n) {
	string res;
	res.push_back('1');
	rep(i, n - 1) {
		res.push_back('0');
	}
	return res;
}
string add(string a, string y) {
	reverse(a.begin(), a.end());
	reverse(y.begin(), y.end());
	int len = max(a.length(), y.length());
	int rest = 0; string res;
	rep(i, len + 1) {
		if (i < (int)a.length()) {
			rest += a[i] - '0';
		}
		if (i < (int)y.length()) {
			rest += y[i] - '0';
		}
		res.push_back('0' + (rest % b));
		rest /= b;
	}
	while (res.length() && res[res.length() - 1] == '0')res.erase(res.end() - 1);
	reverse(res.begin(), res.end());
	return res;
}

int main() {
	cin >> b;
	string s; cin >> s;
	int le = 0, ri = 100001;
	while (ri - le > 1) {
		int mid = (ri + le) >> 1;
		string nex = make(mid);
		if (great(nex, s))ri = mid;
		else le = mid;
	}
	//cout << ri << endl;
	//cout << s << endl;
	//cout << make(ri - 1) << endl;
	s = minu(s, make(ri - 1));
	//cout << s << endl;
	pair<string, int> ans = q(s, ri);
	//cout << ans.first << endl;
	ans.first = add(ans.first, make_base(ri));
	//cout << ans.first << endl;
	//cout << ans.second << endl;
	if (ans.second > 0) {
		cout << ans.first[ans.second-1] << endl;
	}
	else {
		ans.first = minu(ans.first, "1");
		cout << ans.first[ri - 1] << endl;
	}
	return 0;
}
0