Luogu4884 多少个1?(BSGS)

本文介绍了一种使用Baby Step Giant Step (BSGS)算法求解特定形式离散对数问题的方法。通过数学推导和C++实现,展示了如何高效解决形如11...1(n个)的离散对数求解问题。

  11……1(n个)=99……9(n个)/9=(10n-1)/9。

  那么显然就是求离散对数了,BSGS即可。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define ll long long
ll read()
{
    ll x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
ll k,m,block,a[1000000];
map<ll,ll> f;
ll ksc(ll a,ll b)
{
    ll t=a*b-(ll)((long double)a*b/m+0.5)*m;
    return t<0?t+m:t;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("a.in","r",stdin);
    freopen("a.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    k=read(),m=read();
    k=(k*9+1)%m;
    block=sqrt(m);f[a[0]=k]=1;ll s=1;
    for (int i=1;i<=block;i++) f[a[i]=a[i-1]*10%m]=i+1,s=s*10%m;
    ll t=s;
    for (ll i=block;i<=m+block;i+=block,s=ksc(s,t))
    if (f[s]) {cout<<i-f[s]+1;return 0;}
}

 

转载于:https://www.cnblogs.com/Gloid/p/9656632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值