「SOL」Bad Cryptography(Codeforces) | Lucky_Glass's Blog
0%

「SOL」Bad Cryptography(Codeforces)

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


# 题面

若记 $a,b$ 的「NIM积」为 $a\odot b$,则有以下性质:

  • 对于任意正整数 $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,b$。数据规模:$a,b< 2^{64}$,数据组数不超过 $100$。


# 解析

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

记数集 $A=\{x\mid x\in[1,2^{64}),x\in\mathbb{N}^+\}$。(注意排除了 $0$)

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

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

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

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

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

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

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

经过下列推导(注意 $p\mid F$):

上述方程有两个未知数 $k,r$。但是因为 $a^F=1$,有

类似的,定义 $a$ 的阶 ${\text{ord}} a$ 为满足 $a^k=1$ 的最小正整数 $k$,则

png1

(抱歉,不知道博客渲染出了点啥问题,上图的文字就是渲染不出来,只能截图将就一下了 QwQ)

因此 ${\text{ord}} a^{\frac{F}{p}}=\frac{p}{F}{\text{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$。


# 源代码

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
/*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 守望者-网易云