P4884 多少个一

P4884 多少个1?

\(11....1(N个1)\equiv K(mod~m)\)

由等比数列易知\(10^n+10^{n-1}+...+10^0=\frac{(1-10^n)}{1-10}=\frac{10^n-1}{9}\)

\[\frac{10^n-1}{9}\equiv k(mod~m)\\ 10^n-1\equiv9k~(mod~m)\\ 10^n\equiv9k+1(mod~m) \]

#include<cstdio>
#include<map>
#include<cmath>
#define ll long long
using namespace std;
inline ll read() {
    ll X=0,w=1; char c=getchar();
    while (c<'0'||c>'9') { if (c=='-') w=-1; c=getchar(); }
    while (c>='0'&&c<='9') X=(X<<3)+(X<<1)+c-'0',c=getchar();
    return X*w;
}
ll mul(ll a,ll b,ll p){
	ll ans = 0;
	for(;b;b>>=1){
		if(b & 1) ans = (ans + a) % p;
		a = (a<<1) % p; 
	}
	return ans;
}
ll bsgs(ll a,ll b,ll m){
	map<ll,int> mp;
	int p = ceil(sqrt(m));
	ll num = 1;
	mp[1] = 0;
	for(int i=1;i<=p;i++) num = mul(num,a,m),mp[mul(num,b,m)] = i;
	ll n = 1;
	for(int i = 1;i <= p;i++){
		n = mul(n,num,m);
		if (mp.find(n) != mp.end()) return 1ll * p * i - mp[n];
	}
	return -1;
}
int main(){
	ll k, m;
	k = read(); m =read();
	printf("%lld",bsgs(10,9*k+1,m));
	return 0;
}

\(a和b不全为0,存在整数x和y,使得ax+by=gcd(a,b),x=1,y=0即是\)

扩欧应用:

1.求解不定方程\(ax+by=c\)

\(c|gcd(a,b)\)时方程有解,反之无解,令\(g=gcd(a,b),a'=a/g,b'=b/g,c'=c/g\)

可以\(exgcd求出a'x'+b'y'=1的整数解\)

\(a'c'x'+b'c'y'=c'\) \(a'gc'x'+b'gc'y'=c'g\),即\(ac'x'+bc'y'=c\),所以\(x_0=c'x',y_0=c'y'\)是方程一组解

posted @ 2020-07-26 16:10  INFP  阅读(105)  评论(0编辑  收藏  举报