1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include<bits/stdc++.h> using namespace std;
typedef long long ll; typedef __int128 LL; unordered_map<ll, ll> mp;
ll pow_mod(ll q, ll w, ll mod){ ll ret = 1; while(w){ if(w & 1) ret = (LL)ret * q % mod; q = (LL)q * q % mod; w >>= 1; } return ret; }
ll exbsgs(ll a, ll b, ll c){ ll cnt = 0, d = 1, t; while((t = __gcd(a, c)) != 1){ if(b % t) return 0; ++cnt, b /= t, c /= t, d = d * a / t % c; if(d == b) return cnt; } mp.clear(); ll x = sqrt(c) + 1, tmp = b % c; for(int i = 0; i < x; i++)mp[tmp] = i, tmp = (LL)tmp * a % c; ll y = tmp = pow_mod(a, x, c); tmp = (LL)tmp * d % c; for(int i = 1; i <= x; i++){ if(mp.count(tmp))return x * i - mp[tmp] + cnt; tmp = (LL)tmp * y % c; } return 0; }
int main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); ll x; int kase = 0; while(cin >> x){ if(!x) break; x *= 9; ll g = __gcd(x, 8ll); x /= g; ll res = exbsgs(10, 1, x); cout << "Case " << ++kase << ": " << res << 'n'; } return 0; }
|
近期评论