「SOL」Bad Cryptography(Codeforces)

爲何這幾天想要寫博客呢?
由於感受本身也沒有幾天了,如今多寫一些至少證實本身涉足過OI這個領域ios


# 題面

若記 \(a,b\) 的「NIM積」爲 \(a\odot b\),則有如下性質:es6

  • 對於任意正整數 \(k\),數集 \(A_k=\{x\mid x\in\mathbb{N},x<2^{2^k}\}\) 知足 \(\forall x,y\in A_k,x\odot y\in A_k\)(運算在 \(A_k\) 內封閉);
  • \(a\odot 1=a\)(單位元);
  • \(a\odot 0=0\)(零元);
  • \(a\odot b=b\odot a\)(交換律);
  • \((a\odot b)\odot c=a\odot(b\odot c)\)(結合律);
  • \(a\odot(b\otimes c)=(a\odot b)\otimes(b\odot c)\),其中 \(\otimes\) 是按位異或(分配律);

\(a^{\odot b}=\overbrace{a\odot a\odot a\odot\cdots\odot a}^{b個}\)。給定 \(a,b\) 求解方程:優化

\[a^{\odot x}=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

\[a^x=b \]

是一個離散對數的經典模型。可是數集大小 \(F\) 太大,並不能 \(O(\sqrt F)\) 用 BSGS 暴力求解。input

觀察到 \(F\) 並不是素數,實際上質因數分解獲得

\[F=3\times5\times17\times257\times641\times65537\times6700417 \]

這些質因子都不大,考慮可否對單個質因子求解,而後獲得原問題的答案。

好比對質因子 \(p\) 單獨求解。不妨設 \(x=kp+r\),則:

\[a^{kp+r}=b \]

通過下列推導(注意 \(p\mid F\)):

\[\begin{aligned} a^{kpF+rF}&=b^F\\ a^{kF+r\frac{F}{p}}&=b^{\frac{F}{p}}\\ \end{aligned} \]

上述方程有兩個未知數 \(k,r\)。可是由於 \(a^F=1\),有

\[\Big(a^{\frac{F}{p}}\Big)^{r}=b^{\frac{F}{p}} \]

相似的,定義 \(a\) 的階 \({\rm ord}\ a\) 爲知足 \(a^k=1\) 的最小正整數 \(k\),則

  • \({\rm ord}\ a\mid F\),顯然有 \({\rm ord}\ a\le F\)
  • \(k\mid F\),則 \({\rm ord}\ a^k=\frac{{\rm ord}\ a}{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\) 後,有:

\[\begin{aligned} a^{kp+r}&=b\\ (a^p)^k&=b\cdot a^{-r} \end{aligned} \]

\(a^{-r}=a^{F-r}\),相似於費馬小定理求逆元。因而轉化爲子問題,\(a'=a^p\)\(b'=b\cdot a^{-r}\),求解未知數 \(k\)

記對第 \(i\) 個質因子執行上述過程後獲得的方程是

\[a_i^{k_i}=b_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;
}

THE END

Thanks for reading!

若我是宇宙裏眇小的一顆星星
在你目光裏找到方向 從未想過 如此幸運
或許我曾片刻 指引迷途的你
勇敢前行 別在乎

——《守望者》By 司南

> Link 守望者-網易雲

相關文章
相關標籤/搜索