#include "dzilib.h" #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937_64 mrand(114514); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head typedef pair<ll,ll> PLL; namespace Factor { const int N=1010000; ll C,fac[10010],n,mut,a[1001000]; int T,cnt,i,l,prime[N],p[N],psize,_cnt; ll _e[100],_pr[100]; vector<ll> d; inline ll mul(ll a,ll b,ll p) { if (p<=1000000000) return a*b%p; else if (p<=1000000000000ll) return (((a*(b>>20)%p)<<20)+(a*(b&((1<<20)-1))))%p; else { ll d=(ll)floor(a*(long double)b/p+0.5); ll ret=(a*b-d*p)%p; if (ret<0) ret+=p; return ret; } } void prime_table(){ int i,j,tot,t1; for (i=1;i<=psize;i++) p[i]=i; for (i=2,tot=0;i<=psize;i++){ if (p[i]==i) prime[++tot]=i; for (j=1;j<=tot && (t1=prime[j]*i)<=psize;j++){ p[t1]=prime[j]; if (i%prime[j]==0) break; } } } void init(int ps) { psize=ps; prime_table(); } ll powl(ll a,ll n,ll p) { ll ans=1; for (;n;n>>=1) { if (n&1) ans=mul(ans,a,p); a=mul(a,a,p); } return ans; } bool witness(ll a,ll n) { int t=0; ll u=n-1; for (;~u&1;u>>=1) t++; ll x=powl(a,u,n),_x=0; for (;t;t--) { _x=mul(x,x,n); if (_x==1 && x!=1 && x!=n-1) return 1; x=_x; } return _x!=1; } bool miller(ll n) { if (n<2) return 0; if (n<=psize) return p[n]==n; if (~n&1) return 0; for (int j=0;j<=7;j++) if (witness(rand()%(n-1)+1,n)) return 0; return 1; } ll gcd(ll a,ll b) { ll ret=1; while (a!=0) { if ((~a&1) && (~b&1)) ret<<=1,a>>=1,b>>=1; else if (~a&1) a>>=1; else if (~b&1) b>>=1; else { if (a<b) swap(a,b); a-=b; } } return ret*b; } ll rho(ll n) { for (;;) { ll X=rand()%n,Y,Z,T=1,*lY=a,*lX=lY; int tmp=20; C=rand()%10+3; X=mul(X,X,n)+C;*(lY++)=X;lX++; Y=mul(X,X,n)+C;*(lY++)=Y; for(;X!=Y;) { ll t=X-Y+n; Z=mul(T,t,n); if(Z==0) return gcd(T,n); tmp--; if (tmp==0) { tmp=20; Z=gcd(Z,n); if (Z!=1 && Z!=n) return Z; } T=Z; Y=*(lY++)=mul(Y,Y,n)+C; Y=*(lY++)=mul(Y,Y,n)+C; X=*(lX++); } } } void _factor(ll n) { for (int i=0;i<cnt;i++) { if (n%fac[i]==0) n/=fac[i],fac[cnt++]=fac[i];} if (n<=psize) { for (;n!=1;n/=p[n]) fac[cnt++]=p[n]; return; } if (miller(n)) fac[cnt++]=n; else { ll x=rho(n); _factor(x);_factor(n/x); } } void dfs(ll x,int dep) { if (dep==_cnt) d.pb(x); else { dfs(x,dep+1); for (int i=1;i<=_e[dep];i++) dfs(x*=_pr[dep],dep+1); } } void norm() { sort(fac,fac+cnt); _cnt=0; rep(i,0,cnt) if (i==0||fac[i]!=fac[i-1]) _pr[_cnt]=fac[i],_e[_cnt++]=1; else _e[_cnt-1]++; } vector<ll> getd() { d.clear(); dfs(1,0); return d; } vector<ll> factor(ll n) { cnt=0; _factor(n); norm(); return getd(); } vector<PLL> factorG(ll n) { cnt=0; _factor(n); norm(); vector<PLL> d; rep(i,0,_cnt) d.pb(mp(_pr[i],_e[i])); return d; } bool is_primitive(ll a,ll p) { assert(miller(p)); vector<PLL> D=factorG(p-1); a%=p; if (a<0) a+=p; if (a==0) return 0; rep(i,0,SZ(D)) if (powl(a,(p-1)/D[i].fi,p)==1) return 0; return 1; } int D(ll x) { auto d=factorG(x); int v=1; for (auto [p,e]:d) v=v*(e+1); return v; } } bool ispr(int x) { for (int i=2;i<x;i++) if (x%i==0) { return 0; } return 1; } /* ll ans=-1; int GetT() { return 1000; } ll GetN() { return 100000000000000ll; } int GetQ() { return 720; } ll GetC() { return 100000000000000000ll; } int cnt=0; ll Ask(ll x) { if (ans==-1) { ans=mrand()%GetN()+1; } ++cnt; return Factor::D(ans+x); } void Answer(ll x) { assert(x==ans); ans=-1; }*/ map<ll,int> cache; ll offset; ll myask(ll x) { if (cache.count(x)) return cache[x]; return cache[x]=Ask(x+offset); } int mc=0; void solve() { cache.clear(); offset=mrand()%GetN()+123456; ll lim=GetN()+offset; ll p2=8,r2=-1; int v=0; while (1) { myask(v); vector<ll> pos; rep(o,0,p2) { bool val=1; for (auto [key,d]:cache) { int z=(o+key)%p2; if (z!=0) { val&=d%(__builtin_ctz(z)+1)==0; } } if (val) pos.pb(o); } if (SZ(pos)==1) { r2=pos[0]; break; } ++v; } while (p2<=lim) { ll o=(p2-r2)%p2; while (1) { int d=myask(o); if (d%(__builtin_ctzll(p2)+1)!=0) { p2*=2; r2=(p2-o%p2)%p2; break; } ll q2=p2*4; vector<ll> pos; for (ll cand:{r2,r2+p2,r2+p2*2,r2+p2*3}) { bool val=1; for (auto [key,d]:cache) { int z=(cand+key)%q2; if (z!=0) { val&=d%(__builtin_ctz(z)+1)==0; } } if (val) pos.pb(cand); } if (SZ(pos)==1) { r2=pos[0]; p2=q2; break; } o+=p2; } } Answer(r2-offset); } int main() { Factor::init(1000000); for (int _=GetT();_;_--) { solve(); } }
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | #include "dzilib.h" #include <bits/stdc++.h> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define eb emplace_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) typedef vector<int> VI; typedef basic_string<int> BI; typedef long long ll; typedef pair<int,int> PII; typedef double db; mt19937_64 mrand(114514); const ll mod=1000000007; int rnd(int x) { return mrand() % x;} ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} // head typedef pair<ll,ll> PLL; namespace Factor { const int N=1010000; ll C,fac[10010],n,mut,a[1001000]; int T,cnt,i,l,prime[N],p[N],psize,_cnt; ll _e[100],_pr[100]; vector<ll> d; inline ll mul(ll a,ll b,ll p) { if (p<=1000000000) return a*b%p; else if (p<=1000000000000ll) return (((a*(b>>20)%p)<<20)+(a*(b&((1<<20)-1))))%p; else { ll d=(ll)floor(a*(long double)b/p+0.5); ll ret=(a*b-d*p)%p; if (ret<0) ret+=p; return ret; } } void prime_table(){ int i,j,tot,t1; for (i=1;i<=psize;i++) p[i]=i; for (i=2,tot=0;i<=psize;i++){ if (p[i]==i) prime[++tot]=i; for (j=1;j<=tot && (t1=prime[j]*i)<=psize;j++){ p[t1]=prime[j]; if (i%prime[j]==0) break; } } } void init(int ps) { psize=ps; prime_table(); } ll powl(ll a,ll n,ll p) { ll ans=1; for (;n;n>>=1) { if (n&1) ans=mul(ans,a,p); a=mul(a,a,p); } return ans; } bool witness(ll a,ll n) { int t=0; ll u=n-1; for (;~u&1;u>>=1) t++; ll x=powl(a,u,n),_x=0; for (;t;t--) { _x=mul(x,x,n); if (_x==1 && x!=1 && x!=n-1) return 1; x=_x; } return _x!=1; } bool miller(ll n) { if (n<2) return 0; if (n<=psize) return p[n]==n; if (~n&1) return 0; for (int j=0;j<=7;j++) if (witness(rand()%(n-1)+1,n)) return 0; return 1; } ll gcd(ll a,ll b) { ll ret=1; while (a!=0) { if ((~a&1) && (~b&1)) ret<<=1,a>>=1,b>>=1; else if (~a&1) a>>=1; else if (~b&1) b>>=1; else { if (a<b) swap(a,b); a-=b; } } return ret*b; } ll rho(ll n) { for (;;) { ll X=rand()%n,Y,Z,T=1,*lY=a,*lX=lY; int tmp=20; C=rand()%10+3; X=mul(X,X,n)+C;*(lY++)=X;lX++; Y=mul(X,X,n)+C;*(lY++)=Y; for(;X!=Y;) { ll t=X-Y+n; Z=mul(T,t,n); if(Z==0) return gcd(T,n); tmp--; if (tmp==0) { tmp=20; Z=gcd(Z,n); if (Z!=1 && Z!=n) return Z; } T=Z; Y=*(lY++)=mul(Y,Y,n)+C; Y=*(lY++)=mul(Y,Y,n)+C; X=*(lX++); } } } void _factor(ll n) { for (int i=0;i<cnt;i++) { if (n%fac[i]==0) n/=fac[i],fac[cnt++]=fac[i];} if (n<=psize) { for (;n!=1;n/=p[n]) fac[cnt++]=p[n]; return; } if (miller(n)) fac[cnt++]=n; else { ll x=rho(n); _factor(x);_factor(n/x); } } void dfs(ll x,int dep) { if (dep==_cnt) d.pb(x); else { dfs(x,dep+1); for (int i=1;i<=_e[dep];i++) dfs(x*=_pr[dep],dep+1); } } void norm() { sort(fac,fac+cnt); _cnt=0; rep(i,0,cnt) if (i==0||fac[i]!=fac[i-1]) _pr[_cnt]=fac[i],_e[_cnt++]=1; else _e[_cnt-1]++; } vector<ll> getd() { d.clear(); dfs(1,0); return d; } vector<ll> factor(ll n) { cnt=0; _factor(n); norm(); return getd(); } vector<PLL> factorG(ll n) { cnt=0; _factor(n); norm(); vector<PLL> d; rep(i,0,_cnt) d.pb(mp(_pr[i],_e[i])); return d; } bool is_primitive(ll a,ll p) { assert(miller(p)); vector<PLL> D=factorG(p-1); a%=p; if (a<0) a+=p; if (a==0) return 0; rep(i,0,SZ(D)) if (powl(a,(p-1)/D[i].fi,p)==1) return 0; return 1; } int D(ll x) { auto d=factorG(x); int v=1; for (auto [p,e]:d) v=v*(e+1); return v; } } bool ispr(int x) { for (int i=2;i<x;i++) if (x%i==0) { return 0; } return 1; } /* ll ans=-1; int GetT() { return 1000; } ll GetN() { return 100000000000000ll; } int GetQ() { return 720; } ll GetC() { return 100000000000000000ll; } int cnt=0; ll Ask(ll x) { if (ans==-1) { ans=mrand()%GetN()+1; } ++cnt; return Factor::D(ans+x); } void Answer(ll x) { assert(x==ans); ans=-1; }*/ map<ll,int> cache; ll offset; ll myask(ll x) { if (cache.count(x)) return cache[x]; return cache[x]=Ask(x+offset); } int mc=0; void solve() { cache.clear(); offset=mrand()%GetN()+123456; ll lim=GetN()+offset; ll p2=8,r2=-1; int v=0; while (1) { myask(v); vector<ll> pos; rep(o,0,p2) { bool val=1; for (auto [key,d]:cache) { int z=(o+key)%p2; if (z!=0) { val&=d%(__builtin_ctz(z)+1)==0; } } if (val) pos.pb(o); } if (SZ(pos)==1) { r2=pos[0]; break; } ++v; } while (p2<=lim) { ll o=(p2-r2)%p2; while (1) { int d=myask(o); if (d%(__builtin_ctzll(p2)+1)!=0) { p2*=2; r2=(p2-o%p2)%p2; break; } ll q2=p2*4; vector<ll> pos; for (ll cand:{r2,r2+p2,r2+p2*2,r2+p2*3}) { bool val=1; for (auto [key,d]:cache) { int z=(cand+key)%q2; if (z!=0) { val&=d%(__builtin_ctz(z)+1)==0; } } if (val) pos.pb(cand); } if (SZ(pos)==1) { r2=pos[0]; p2=q2; break; } o+=p2; } } Answer(r2-offset); } int main() { Factor::init(1000000); for (int _=GetT();_;_--) { solve(); } } |