For the given integer N and digit D, find the minimal integer K ≥ 2 such that the representation ofN in the positional numeral system with baseK contains the maximum possible consecutive number of digitsD at the end.
The input contains two integers N and D (0 ≤ N ≤ 1015,0 ≤ D ≤ 9).
Output two integers: K, the answer to the problem, andR, the the number of consecutive digits D at the end of the representation of N in the positional numeral system with baseK.
3 1
2 2
29 9
10 1
0 4
2 0
90 1
89 2
题意:
给出n 和 d , 要求得到用k 进制得到n,最后一个数要求为d,求出从后数d 的数量,要求数量最多是k 尽可能小。
思路:
要使n - d 用k 进制表示出,所以求出n-d 的因子,暴力枚举得到最大答案。
特殊情况是:n== d 和n< d
CODE:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
vector<ll> v;
ll n, d;
void work(ll nn)
{
for(ll i = 1; i * i <= nn; ++i){
if(nn%i == 0){
if(i > d)
v.push_back(i);
if((nn/i) > d && i*i != nn)
v.push_back(nn/i);
}
}
}
int main()
{
//freopen("in", "r", stdin);
while(~scanf("%I64d %I64d", &n, &d)){
if(n == d){
if(n <= 1) printf("2 1\n");
else printf("%I64d 1\n",n + 1);
continue;
}
if(n < d){
printf("2 0\n");
continue;
}
v.clear();
work(n - d);
ll r = 0, k = 2;
for(ll i = 0; i < v.size(); ++i){
ll n1 = n, s = 0;
if(v[i] > 1){
while(n1){
if(n1 % v[i] != d) break;
else{
s++;
n1 = n1/v[i];
}
}
if(s > r){
r = s;
k = v[i];
}
if(s == r && v[i] < k){
k = v[i];
}
}
}
printf("%I64d %I64d\n", k, r);
}
return 0;
}