为何这几天想要写博客呢?
由于感受本身也没有几天了,如今多写一些至少证实本身涉足过OI这个领域ios
若记 \(a,b\) 的「NIM积」为 \(a\odot b\),则有如下性质:es6
记 \(a^{\odot b}=\overbrace{a\odot a\odot a\odot\cdots\odot a}^{b个}\)。给定 \(a,b\) 求解方程:优化
多组询问,每次给定 \(a,b\)。数据规模:\(a,b< 2^{64}\),数据组数不超过 \(100\)。es5
不是很清楚「NIM积」的一些技巧,因此此篇不涉及「NIM积」计算的优化。
另外本篇中极有可能有不严谨的地方 ╯︿╰ 但愿各位能指出spa
记数集 \(A=\{x\mid x\in[1,2^{64}),x\in\mathbb{N}^+\}\)。(注意排除了 \(0\)).net
根据NIM积的上述性质,能够推断 \((A,\odot)\) 是乘法群(知足封闭性、结合律、有单位元和逆元)。如下简记「NIM积」为「乘法」。code
群的大小(数集的大小为)记为 \(F=2^{64}-1\),则有 \(\forall a\in A,a^F=1\)(能够类比整数模 \(n\) 乘法群)。blog
回过头来看题目给定的问题get
是一个离散对数的经典模型。可是数集大小 \(F\) 太大,并不能 \(O(\sqrt F)\) 用 BSGS 暴力求解。input
观察到 \(F\) 并不是素数,实际上质因数分解获得
这些质因子都不大,考虑可否对单个质因子求解,而后获得原问题的答案。
好比对质因子 \(p\) 单独求解。不妨设 \(x=kp+r\),则:
通过下列推导(注意 \(p\mid F\)):
上述方程有两个未知数 \(k,r\)。可是由于 \(a^F=1\),有
相似的,定义 \(a\) 的阶 \({\rm ord}\ a\) 为知足 \(a^k=1\) 的最小正整数 \(k\),则
所以 \({\rm ord}\ a^{\frac{F}{p}}=\frac{p}{F}{\rm ord}\ a\le p\)。也就是说,若是 \(\Big(a^{\frac{F}{p}}\Big)^{r}=b^{\frac{F}{p}}\) 的解 \(r\) 存在,那么必然存在特解 \(r=r_0\),\(r_0\le p\)。
\(p\) 较小,能够直接 BSGS \(O(\sqrt{p})\) 的复杂度内求解。
解出 \(r\) 后,有:
\(a^{-r}=a^{F-r}\),相似于费马小定理求逆元。因而转化为子问题,\(a'=a^p\),\(b'=b\cdot a^{-r}\),求解未知数 \(k\)。
记对第 \(i\) 个质因子执行上述过程后获得的方程是
其中 \(k_i\) 是未知数。因而有 \(a_i=a_{i-1}^{p_i},b_i=b_{i-1}\cdot a^{-r_i}\),\(k_i=k_{i+1}p_{i+1}+r_{i+1}\)。
对每一个质因子都作一次上述过程后,最后咱们会获得一个关于 \(k_7\) 的方程 \(a_7^{k_7}=b_7\)。这个方程如何求解?
实际上这个方程不须要求解——\(a_7=a^{p_1p_2\cdots p_7}=a^F=1\),此时若 \(b_7=1\),则有无穷解,取朴素解 \(k_7=1\);若 \(b_7\neq1\),则无解。
而后 \(k_i=k_{i+1}p_{i+1}+r_{i+1}\) 往回代,代出 \(k_1\),而后获得 \(x=k_1p_1+r_1\)。
/*Lucky_Glass*/ #include<map> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long llong; typedef unsigned long long ullong; #define con(type) const type & template<class T>inline T rin(T &r){ int b=1,c=getchar();r=0; while(c<'0' || '9'<c) b=c=='-'?-1:b,c=getchar(); while('0'<=c && c<='9') r=(r<<1)+(r<<3)+(c^'0'),c=getchar(); return r*=b; } template<class T>inline void wri(con(T)r){ if(r<10) putchar(char('0'+r)); else wri(r/10),putchar(char('0'+r%10)); } const int PRES[]={3,5,17,257,641,65537,6700417}; const int SQRPRES[]={2,3,5,17,26,257,2589}; const ullong CONF=18446744073709551615ull; int ncas; ullong varr[10]; int nontag[10]; // 摘自 Freopen 的博客 https://blog.csdn.net/qq_35950004/article/details/107669351 ullong nim[256][256]; ullong nimMul(ullong a,ullong b,ullong L=64){ // L if(a <= 1 || b <= 1) return a * b; if(a < 256 && b < 256 && nim[a][b]) return nim[a][b]; /* X = 2 ^ L , L = 2 ^ p ([a / X]X + a%X) * ([b / X]X + b%X) c = a % X , d = b % X */ ullong S = (1ull << L) - 1; if(a <= S && b <= S) return nimMul(a,b,L>>1); ullong A = nimMul(a>>L,b>>L,L>>1) , B = nimMul((a>>L)^(a&S),(b>>L)^(b&S),L>>1) , C = nimMul(a&S,b&S,L>>1); S++; ullong r = nimMul(A,S>>1,L>>1) ^ (S * (C^B)) ^ C; if(a < 256 && b < 256) nim[a][b] = r; return r; } ullong nimPow(ullong a,ullong b){ ullong r=1; while(b){ if(b&1) r=nimMul(a,r); a=nimMul(a,a),b>>=1; } return r; } int solve(con(ullong)a,con(ullong)b,con(int)conp,con(int)consqr){ ullong x=nimPow(a,CONF/conp),y=nimPow(b,CONF/conp); map<ullong,int> mem; ullong tmp=y; for(int i=0;i<=consqr;i++,tmp=nimMul(tmp,x)) if(!mem.count(tmp)) mem[tmp]=i; tmp=1; ullong tmp_per=nimPow(x,consqr); for(int i=0;i<=consqr;i++,tmp=nimMul(tmp,tmp_per)) if(mem.count(tmp) && i*consqr>=mem[tmp]) return i*consqr-mem[tmp]; return -1; } int main(){ // freopen("input.in","r",stdin); rin(ncas); while(ncas--){ ullong a,b; rin(a),rin(b); bool fai=false; memset(nontag,false,sizeof nontag); for(int i=0;i<7;i++){ int res=solve(a,b,PRES[i],SQRPRES[i]); if(res==-2){ nontag[i]=true; continue; } if(res==-1){ fai=true; break; } varr[i]=res; ullong _b=nimMul(b,nimPow(a,CONF-res)),_a=nimPow(a,PRES[i]); a=_a,b=_b; } if(a!=b || nimMul(a,a)!=b) fai=true; if(fai) printf("-1\n"); else{ ullong x=0,mod=1; for(int i=6;~i;i--){ if(nontag[i]) continue; mod*=PRES[i]; x=x*PRES[i]+varr[i]; if(x<0) x+=mod; } wri(x),putchar('\n'); } } return 0; }