2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest E. Equal Digits

E. Equal Digits
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

For the given integer N and digit D, find the minimal integer K ≥ 2 such that the representation of N in the positional numeral system with base K contains the maximum possible consecutive number of digits D at the end.

Input

The input contains two integers N and D (0 ≤ N ≤ 1015, 0 ≤ D ≤ 9).

Output

Output two integers: K, the answer to the problem, and R, the the number of consecutive digits D at the end of the representation of Nin the positional numeral system with base K.

Sample test(s)
input
3 1
output
2 2
input
29 9
output
10 1
input
0 4
output
2 0
input
90 1
output
89 2

 

分析:
显然答案是一个正整数,因为我们可以取k=n-d进制。
那么,因为至少为1,则k必定整除于n-d
那么将n-d分解,将其因数全部拿出来。
暴力更新答案即可。
因数个数在根号级别,暴力计算复杂度在log级别,不会超时。
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cmath>
  5 #include <ctime>
  6 #include <iostream>
  7 #include <algorithm>
  8 #include <map>
  9 #include <set>
 10 #include <vector>
 11 #include <deque>
 12 #include <queue>
 13 using namespace std;
 14 typedef long long LL;
 15 typedef double DB;
 16 #define Rep(i, n) for(int i = (0); i < (n); i++)
 17 #define Repn(i, n) for(int i = (n)-1; i >= 0; i--)
 18 #define For(i, s, t) for(int i = (s); i <= (t); i++)
 19 #define Ford(i, t, s) for(int i = (t); i >= (s); i--)
 20 #define rep(i, s, t) for(int i = (s); i < (t); i++)
 21 #define repn(i, s, t) for(int i = (s)-1; i >= (t); i--)
 22 #define MIT (2147483647)
 23 #define MLL (1000000000000000000LL)
 24 #define INF (1000000001)
 25 #define mk make_pair
 26 #define ft first
 27 #define sd second
 28 #define clr(x, y) (memset(x, y, sizeof(x)))
 29 #define sqr(x) ((x)*(x))
 30 #define sz(x) ((int) (x).size())
 31 #define puf push_front
 32 #define pub push_back
 33 #define pof pop_front
 34 #define pob pop_back
 35 
 36 template<class T>
 37 inline T Getint()
 38 {
 39     char Ch = ' ';
 40     T Ret = 0;
 41     while(!(Ch >= '0' && Ch <= '9')) Ch = getchar();
 42     while(Ch >= '0' && Ch <= '9')
 43     {
 44         Ret = Ret * 10 + Ch - '0';
 45         Ch = getchar();
 46     }
 47     return Ret;
 48 }
 49 
 50 LL n;
 51 int d;
 52 vector<LL> Factor;
 53 
 54 inline void Input()
 55 {
 56     cin >> n >> d;
 57 }
 58 
 59 inline void Solve()
 60 {
 61     if(n == d)
 62     {
 63         if(n <= 1) puts("2 1");
 64         else cout << n + 1LL << " 1" << endl;
 65         return;
 66     }
 67     if(n < d)
 68     {
 69         puts("2 0");
 70         return;
 71     }
 72     
 73     LL T = n - d;
 74     for(LL i = 1; i * i <= T; i++)
 75         if(T % i == 0)
 76         {
 77             if(i > d) Factor.pub(i);
 78             if(T / i > d) Factor.pub(T / i);
 79         }
 80     
 81     LL R = 0, k = 2;
 82     int Length = sz(Factor), Cnt;
 83     Rep(i, Length)
 84     {
 85         if(Factor[i] == 1) continue;
 86         
 87         T = n, Cnt = 0;
 88         while(T % Factor[i] == d)
 89             T /= Factor[i], Cnt++;
 90         
 91         if(Cnt > R) R = Cnt, k = Factor[i];
 92         else if(Cnt == R) k = min(k, Factor[i]);
 93     }
 94     
 95     cout << k << ' ' << R << endl;
 96 }
 97 
 98 int main() {
 99     //freopen("E.in", "r", stdin);
100      Input();
101      Solve();
102     return 0;
103 }
View Code

 

posted @ 2015-12-21 15:23  yanzx6  阅读(487)  评论(0编辑  收藏  举报