bsgs大步小步算法及其扩展 & SPOJ Power Modulo Inverted

非常不错的一篇博客 http://blog.miskcoo.com/2015/05/discrete-logarithm-problem

baby step giant step

用来解

axb(mod p),x a x ≡ b ( m o d   p ) , x 是 自 然 数

一类的方程 O(p) O ( p )

其实想法也比较简单,对于a,p互质的情况
定一个值 m=p m = p ,x可以写为%m下的表达形式: Am+r A m + r
把方程写为 aAmb(ar)1(mod p) a A m ≡ b ⋅ ( a r ) − 1 ( m o d   p )
小步: 因为A与r的范围都是 p p 的,先预处理出所有 b(ar)1 b ⋅ ( a r ) − 1 的值,hash存下。

大步: 从0到m枚举A,
aAm=b(ar)1,r[0,m) a A m = b ⋅ ( a r ) − 1 , r ∈ [ 0 , m ) ,说明存在 x=Am+r x = A m + r
若需要最小整数解则r应该取最小值。

一个小优化

可以将 Am+r A m + r 的改为 Amr A m − r 的形式,这样不需要处理逆元,但是还是需要用到逆元的存在!
aAm=b(ar),r[0,m) a A m = b ⋅ ( a r ) , r ∈ [ 0 , m ) ,说明存在 x=Amr x = A m − r
注意此时A的枚举范围从0..m改为1..m+1。
此时需要特判b=1的情况,否则因为A是从1开始枚举的,而 r<m r < m ,这样是发现不了x=0的解的。

扩展BSGS

其实也不是什么扩展,只是处理了a,p不互质的情况
g=(a,p) g = ( a , p )
axb(mod p) a x ≡ b ( m o d   p ) 写为等式,即 aax1kp=b a ⋅ a x − 1 − k p = b
由exgcd知识,b不是g的倍数时无解。
等式两边同除以g,可以得到

(a/g)ax1b/g(mod p/g) ( a / g ) ⋅ a x − 1 ≡ b / g ( m o d   p / g )

(a,p/g)=1 ( a , p / g ) = 1 ,这种形式也可以用bsgs解,方法是显然的,甚至不用处理逆元,只需要将大步与小步的初始值改变即可。
最后将解加一就是原方程的解了。

注意并不是将(a,p)变为(a/g,p/g),所以可能上述过程需要做多次,将a/g的积处理一下即可。
注意为了防止最终要解的方程出现负指数,上述过程中注意判断x=0的解。

具体实现见标吧,上面推荐的那篇博客。

Note

mod 1的情况需要特判
b=1的情况需要特判
exbsgs过程中注意判断x=0的解

下面是给自己参阅(co)的标
值得一提的是手打hash要比map快上1/2左右

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
ll y,z,p;
ll ksm(ll x,ll y,ll m) {
    ll ret=1;
    for (; y; y>>=1) {
        if (y&1) ret=ret*x%m;
        x=x*x%m;
    }
    return ret;
}
ll gcd(ll x,ll y) {return y==0?x:gcd(y,x%y);}
map<ll,ll> ha;

ll exbsgs(ll a,ll b,ll p) {
    a%=p,b%=p;
    if (b==1||p==1) return 0; //1
    ll cnt=0;
    ll t=1;
    for (ll g=gcd(a,p); g!=1; g=gcd(a,p)) {
        if (b%g) return -1;
        p/=g,b/=g,t=t*(a/g)%p;
        cnt++;
        if (b==t) return cnt;//2
    }

    ha.clear();
    ll m=sqrt(p)+1;
    for (ll i=0,s=b; i<m; i++,s=s*a%p) {
        ha[s]=i;
    }

    ll step=ksm(a,m,p);
    for (ll i=1; i<=m+1; i++) {
        t=t*step%p;
        if (ha.count(t)) return cnt + i*m - ha[t];
    }
    return -1;
}

int main() { 
    cin>>y>>p>>z;
    for (; ; cin>>y>>p>>z) {
        if (y==0 && z==0 && p==0) return 0;
        ll ret=exbsgs(y,z,p);
        if (ret==-1) cout<<"No Solution"<<endl;
        else cout<<ret<<endl;
    }
}
### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值