2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest E. Equal Digits
分析:
显然答案是一个正整数,因为我们可以取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 }