fork(4) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define gc getchar_unlocked
  4. #define fo(i,n) for(i=0;i<n;i++)
  5. #define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  6. #define si(x) scanf("%d",&x)
  7. #define sl(x) scanf("%lld",&x)
  8. #define ss(s) scanf("%s",s)
  9. #define ll long long
  10. #define pi(x) printf("%d\n",x)
  11. #define pl(x) printf("%lld\n",x)
  12. #define ps(s) printf("%s\n",s)
  13. #define pb push_back
  14. #define mp make_pair
  15. #define F first
  16. #define S second
  17. #define all(x) x.begin(), x.end()
  18. #define clr(x) memset(x, 0, sizeof(x))
  19. #define sortall(x) sort(all(x))
  20. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  21. #define PI 3.1415926535897932384626
  22. typedef pair<int, int> pii;
  23. typedef pair<ll, ll> pll;
  24. typedef vector<int> vi;
  25. typedef vector<ll> vll;
  26. typedef vector<pii> vpii;
  27. typedef vector<pll> vpll;
  28. typedef vector<vi> vvi;
  29. typedef vector<vll> vvl;
  30. const ll mod = 2e18;
  31. const int N = 3e5;
  32. vi g[N];
  33. int a[N];
  34. ll mpow(ll base, ll exp);
  35. string s;
  36. vll cnt;
  37.  
  38. int dig;
  39. ll y;
  40. bool f(ll val){
  41. ll sum = 1;
  42. ll beg = val;
  43. int x = 1;
  44. while(sum<y and x<dig){
  45. x++;
  46. if(sum >= y) return 1;
  47. sum += beg;
  48. if(beg >= y/val and x<dig) return 1;
  49. beg *= val;
  50. }
  51. return sum>=y;
  52. }
  53. bool check(ll val){
  54. ll sum = 1;
  55. ll beg = val;
  56. int x = 1;
  57. while(sum<y and x<dig){
  58. x++;
  59. if(sum > y) return 0;
  60. sum += beg;
  61. beg *= val;
  62. }
  63. return sum==y;
  64. }
  65. ll bin(ll lo, ll hi){
  66. if(lo==hi){
  67. if(f(lo))
  68. return lo;
  69. return -1;
  70. }
  71. if(lo+1==hi){
  72. if(f(lo))
  73. return lo;
  74.  
  75. if(f(hi))
  76. return hi;
  77. return -1;
  78. }
  79. ll mid = (lo+hi)/2;
  80. if(f(mid)) return bin(lo, mid);
  81. return bin(mid+1, hi);
  82.  
  83.  
  84. }
  85. int main()
  86. {
  87. ios_base::sync_with_stdio(false);
  88. cin.tie(NULL);
  89. int i,n,k,j;
  90. int T , t;
  91. //freopen("input.txt", "r", stdin);
  92. //freopen("output.txt", "w", stdout);
  93. cin>>t; T = t;
  94. for(int zz = 1; zz <= T; zz++){
  95. cout<<"Case #"<<zz<<": ";
  96. cin>>y;
  97. ll x;
  98. Fo(dig, 64, 1){
  99. //2 se y
  100. ll can = bin(2, y);
  101. if (can != -1 and check(can)){
  102. cout<<can<<endl;
  103. dig = -1;
  104. break;
  105. }
  106. }
  107. }
  108.  
  109. return 0;
  110. }
  111.  
  112.  
Success #stdin #stdout 0s 8156KB
stdin
1
3906
stdout
Case #1: 5