「SOL」Bad Cryptography(Codeforces)

为什么这几天想要写博客呢?
因为感觉自己也没有几天了,现在多写一些至少证明自己涉足过OI这个领域


# 题面

若记 (a,b) 的「NIM积」为 (aodot b),则有以下性质:

  • 对于任意正整数 (k),数集 (A_k={xmid xinmathbb{N},x<2^{2^k}}) 满足 (forall x,yin A_k,xodot yin A_k)(运算在 (A_k) 内封闭);
  • (aodot 1=a)(单位元);
  • (aodot 0=0)(零元);
  • (aodot b=bodot a)(交换律);
  • ((aodot b)odot c=aodot(bodot c))(结合律);
  • (aodot(botimes c)=(aodot b)otimes(bodot c)),其中 (otimes) 是按位异或(分配律);

(a^{odot b}=overbrace{aodot aodot aodotcdotsodot a}^{b个})。给定 (a,b) 求解方程:

[a^{odot x}=b ]

多组询问,每次给定 (a,b)。数据规模:(a,b< 2^{64}),数据组数不超过 (100)


# 解析

不是很清楚「NIM积」的一些技巧,所以此篇不涉及「NIM积」计算的优化。
另外本篇中极有可能有不严谨的地方 ╯︿╰ 希望各位能指出

记数集 (A={xmid xin[1,2^{64}),xinmathbb{N}^+})。(注意排除了 (0)

根据NIM积的上述性质,可以推断 ((A,odot)) 是乘法群(满足封闭性、结合律、有单位元和逆元)。以下简记「NIM积」为「乘法」

群的大小(数集的大小为)记为 (F=2^{64}-1),则有 (forall ain A,a^F=1)(可以类比整数模 (n) 乘法群)。

回过头来看题目给定的问题

[a^x=b ]

是一个离散对数的经典模型。但是数集大小 (F) 太大,并不能 (O(sqrt F)) 用 BSGS 暴力求解。

观察到 (F) 并非素数,实际上质因数分解得到

[F=3 imes5 imes17 imes257 imes641 imes65537 imes6700417 ]

这些质因子都不大,考虑能否对单个质因子求解,然后得到原问题的答案。

比如对质因子 (p) 单独求解。不妨设 (x=kp+r),则:

[a^{kp+r}=b ]

经过下列推导(注意 (pmid F)):

[egin{aligned} a^{kpF+rF}&=b^F\ a^{kF+rfrac{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) 的阶 ({ m ord} a) 为满足 (a^k=1) 的最小正整数 (k),则

  • ({ m ord} amid F),显然有 ({ m ord} ale F)
  • (kmid F),则 ({ m ord} a^k=frac{{ m ord} a}{k})

因此 ({ m ord} a^{frac{F}{p}}=frac{p}{F}{ m ord} ale p)。也就是说,如果 (Big(a^{frac{F}{p}}Big)^{r}=b^{frac{F}{p}}) 的解 (r) 存在,那么必然存在特解 (r=r_0)(r_0le p)

(p) 较小,可以直接 BSGS (O(sqrt{p})) 的复杂度内求解。

解出 (r) 后,有:

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

(a^{-r}=a^{F-r}),类似于费马小定理求逆元。于是转化为子问题,(a'=a^p)(b'=bcdot 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_2cdots p_7}=a^F=1),此时若 (b_7=1),则有无穷解,取朴素解 (k_7=1);若 (b_7 eq1),则无解。

然后 (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
");
		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('
');
		}
	}
	return 0;
}

THE END

Thanks for reading!

若我是宇宙里渺小的一颗星星
在你目光里找到方向 从未想过 如此幸运
或许我曾片刻 指引迷途的你
勇敢前行 别在意

——《守望者》By 司南

> Link 守望者-网易云

欢迎转载٩(๑❛ᴗ❛๑)۶,请在转载文章末尾附上原博文网址~
原文地址:https://www.cnblogs.com/LuckyGlass-blog/p/14441649.html