ソースコード 拡げる

Copy
```#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }

long long n, s;

long long sum(long long n, long long b) {
long long res = 0;
while (n > 0) {
res += (n % b);
n /= b;
}
return res;
}

vector<long long> divisor(long long n) {
vector<long long> res;
for (long long i = 1LL; i*i <= n; ++i) {
if (n%i == 0LL) {
res.push_back(i);
long long temp = n/i;
if (i != temp) res.push_back(temp);
}
}
sort(res.begin(), res.end());
return res;
}

long long solve() {
if (n == s) return n+1;
if (n < s) return -1;

long long b = 0;
for (b = 2; b <= 400000; ++b) {
long long ts = sum(n, b);
if (ts == s) return b;
}

vector<long long> div = divisor(n-s);
sort(div.begin(), div.end());
for (int i = 0; i < div.size(); ++i) {
b = div[i] + 1;
long long ts = sum(n, b);
if (ts == s) return b;
}

return -1;
}

int main() {
//cout << sum(1434, 10) << endl;
//cout << sum(1690, 10) << endl;
while (cin >> n >> s) {
cout << solve() << endl;
}
}

```

#### 提出情報

提出日時 2016-08-28 21:22:55+0900 D - 桁和 drken C++14 (GCC 5.4.1) 500 2566 Byte AC 31 ms 256 KB

#### ジャッジ結果

セット名 Sample All

 AC × 5
 AC × 47
セット名 テストケース
ケース名 結果 実行時間 メモリ
subtask1_100000000000_1.txt AC 4 ms 256 KB
subtask1_100000000000_100000000000.txt AC 4 ms 256 KB
subtask1_100000000000_2.txt AC 30 ms 256 KB
subtask1_100000000000_3.txt AC 30 ms 256 KB
subtask1_100000000000_50000000000.txt AC 29 ms 256 KB
subtask1_100000000000_50000000001.txt AC 29 ms 256 KB
subtask1_100000000000_99999999999.txt AC 25 ms 256 KB
subtask1_16983563041_1.txt AC 4 ms 256 KB
subtask1_1_1.txt AC 4 ms 256 KB
subtask1_1_2.txt AC 4 ms 256 KB
subtask1_1_31415926535.txt AC 4 ms 256 KB
subtask1_239484768_194586924.txt AC 18 ms 256 KB
subtask1_2_1.txt AC 4 ms 256 KB
subtask1_2_2.txt AC 4 ms 256 KB
subtask1_31415926535_1.txt AC 25 ms 256 KB
subtask1_49234683534_2461734011.txt AC 26 ms 256 KB
subtask1_4_1.txt AC 4 ms 256 KB
subtask1_58640129658_232122496.txt AC 27 ms 256 KB
subtask1_68719476735_35.txt AC 12 ms 256 KB
subtask1_68719476735_36.txt AC 4 ms 256 KB
subtask1_68719476735_37.txt AC 28 ms 256 KB
subtask1_68719476736_1.txt AC 4 ms 256 KB
subtask1_68719476736_2.txt AC 4 ms 256 KB
subtask1_72850192441_16865701.txt AC 29 ms 256 KB
subtask1_79285169301_27.txt AC 4 ms 256 KB
subtask1_82914867733_1676425945.txt AC 29 ms 256 KB
subtask1_8594813796_75700.txt AC 7 ms 256 KB
subtask1_87654_12345.txt AC 5 ms 256 KB
subtask1_87654_138.txt AC 4 ms 256 KB
subtask1_87654_30.txt AC 4 ms 256 KB
subtask1_87654_4294967308.txt AC 4 ms 256 KB
subtask1_87654_45678.txt AC 12 ms 256 KB
subtask1_97822032312_49157112.txt AC 31 ms 256 KB
subtask1_98750604051_977728851.txt AC 31 ms 256 KB
subtask1_99999515529_1.txt AC 22 ms 256 KB
subtask1_99999515529_316226.txt AC 30 ms 256 KB
subtask1_99999515529_316227.txt AC 22 ms 256 KB
subtask1_99999515529_316228.txt AC 30 ms 256 KB
subtask1_99999515529_49999757765.txt AC 29 ms 256 KB
subtask1_99999515529_49999757766.txt AC 29 ms 256 KB
subtask1_99999515530_2.txt AC 22 ms 256 KB
subtask1_99999999977_1.txt AC 30 ms 256 KB
subtask1_99999999977_2.txt AC 31 ms 256 KB
subtask1_99999999977_49999999989.txt AC 29 ms 256 KB
subtask1_99999999977_49999999990.txt AC 29 ms 256 KB
subtask1_99999999999_1.txt AC 31 ms 256 KB
subtask1_99999999999_100000000000.txt AC 4 ms 256 KB