QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#495759#9139. Prefix Divisible by Suffixucup-team191#TL 2887ms30236kbC++232.6kb2024-07-27 23:29:142024-07-27 23:29:14

Judging History

你现在查看的是最新测评结果

  • [2024-07-27 23:29:14]
  • 评测
  • 测评结果:TL
  • 用时:2887ms
  • 内存:30236kb
  • [2024-07-27 23:29:14]
  • 提交

answer

#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
using pii=pair<int,int>;
using ll=long long;
using vi=vector<int>;
using vl=vector<ll>;
#define pb push_back
#define all(a) begin(a),end(a)

const int N=300010,MOD=1e9+7;
const char en='\n';
const ll LLINF=1ll<<60;

ll euclid(ll a, ll b, ll &x, ll &y) {
	if (!b) return x = 1, y = 0, a;
	ll d = euclid(b, a % b, y, x);
	return y -= a/b * x, d;
}

ll crt(ll a, ll m, ll b, ll n) { //assumes nm<=1e18
	if (n > m) swap(a, b), swap(m, n);
	ll x, y, g = euclid(m, n, x, y);
	assert((a - b) % g == 0); // else no solution
	x = (b - a) % n * x % n / g * m + a;
	return x < 0 ? x + m/g*n : x;
}

ll inv(ll x,ll mod)
{
	ll a,b;
	euclid(x,mod,a,b);
	a%=mod;
	if (a<0) a+=mod;
	return a;
}

ll n,c,cn[20][N];
int sufr[20],dig[20];

void addRes(ll&u,ll&mo,ll ma,ll mu,ll dod,ll nm) //nm<=suf+c<1e9, mo<=ma, ma*nm<=1e18 -> mo*nm<=1e18
{
	if (mo==ma)
	{
		if (((u%nm)*mu+dod)%nm)
		{
			u=-1;
		}
		return;
	}
	ll g=gcd(mu,nm);
	if (dod%g)
	{
		u=-1;
		return;
	}
	mu/=g;
	nm/=g;
	dod/=g;
	ll req=((nm-dod%nm)*inv(mu%nm,nm))%nm;
	if ((u-req)%gcd(mo,nm))
	{
		u=-1;
		return;
	}
	ll re=crt(u,mo,req,nm);
	if (re>ma)
	{
		u=-1;
		return;
	}
	u=re;
	mo=min(ma,lcm(mo,nm));
}

ll leqmo(ll x,ll u,ll m)
{
	if (u==0) return x/m;
	else return (x+m-u)/m;
}

void rek(int i,int mask,ll u,ll mo,ll ma,ll mul,ll dod)
{
	if (i==0)
	{
		//cout<<i<<' '<<mask<<' '<<u<<' '<<mo<<' '<<ma<<en;
		ll l=1,r=10,lp=1;
		int lsuf=__lg(mask);
		while (l<=ma) //l .. r-1
		{
			cn[lp+lsuf][mask]+=leqmo(min(r-1,ma),u,mo)-leqmo(l-1,u,mo);
			l*=10;
			r*=10;
			++lp;
		}
		return;
	}
	rek(i-1,mask,u,mo,ma,mul*10,dod*10+dig[i-1]);
	addRes(u,mo,ma,mul,dod,sufr[i]+c);
	if (u==-1) return;
	rek(i-1,mask+(1<<i),u,mo,ma,mul*10,dod*10+dig[i-1]);
	//if (r<=ma) rek(i-1,mask+(1<<i),r,ma);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>c;
	for (int suf=0;suf<1e7;++suf)
	{
		memset(dig,0,sizeof(dig));
		ll cc=suf,l=0,p10=1;
		while (cc)
		{
			dig[l]=cc%10;
			cc/=10;
			++l;
			p10*=10;
		}
		ll p101=10;
		for (int i=1;i<=15;++i)
		{
			sufr[i]=suf%p101;
			p101*=10;
		}
		for (int l0=0;;++l0,p10*=10)
		{
			if (l+l0==0) continue;
			ll os=n/p10;
			if (suf>n%p10) --os;
			ll rr=suf+c;
			if (rr>os) break;
			rek(l+l0-1,1<<(l+l0),0,rr,os,10,dig[l+l0-1]);
		}
	}
	ll odd=0;
	for (int le=1;le<=15;++le)
	{
		ll cu=0;
		for (int b=1;b<(1<<le);++b)
		{
			if (__builtin_popcount(b)%2) cu+=cn[le][b];
			else cu-=cn[le][b];
		}
		//cout<<le<<' '<<cu<<en;
		odd+=cu;
	}
	cout<<odd<<en;
}

详细

Test #1:

score: 100
Accepted
time: 460ms
memory: 5604kb

input:

20 1

output:

2

result:

ok 1 number(s): "2"

Test #2:

score: 0
Accepted
time: 461ms
memory: 7800kb

input:

111 4

output:

9

result:

ok 1 number(s): "9"

Test #3:

score: 0
Accepted
time: 461ms
memory: 9772kb

input:

1111 10

output:

75

result:

ok 1 number(s): "75"

Test #4:

score: 0
Accepted
time: 462ms
memory: 15888kb

input:

1000000 7

output:

111529

result:

ok 1 number(s): "111529"

Test #5:

score: 0
Accepted
time: 459ms
memory: 3552kb

input:

1 1

output:

0

result:

ok 1 number(s): "0"

Test #6:

score: 0
Accepted
time: 459ms
memory: 3552kb

input:

99999 10000

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 457ms
memory: 9704kb

input:

1000 1

output:

287

result:

ok 1 number(s): "287"

Test #8:

score: 0
Accepted
time: 462ms
memory: 17964kb

input:

10000000 1

output:

3102010

result:

ok 1 number(s): "3102010"

Test #9:

score: 0
Accepted
time: 462ms
memory: 20096kb

input:

100000000 1

output:

31035571

result:

ok 1 number(s): "31035571"

Test #10:

score: 0
Accepted
time: 470ms
memory: 22052kb

input:

1000000000 1

output:

310375697

result:

ok 1 number(s): "310375697"

Test #11:

score: 0
Accepted
time: 520ms
memory: 24184kb

input:

10000000000 1

output:

3103907933

result:

ok 1 number(s): "3103907933"

Test #12:

score: 0
Accepted
time: 637ms
memory: 28124kb

input:

100000000000 1

output:

31039265058

result:

ok 1 number(s): "31039265058"

Test #13:

score: 0
Accepted
time: 1345ms
memory: 28220kb

input:

1000000000000 1

output:

310394177863

result:

ok 1 number(s): "310394177863"

Test #14:

score: 0
Accepted
time: 2887ms
memory: 30236kb

input:

10000000000000 1

output:

3103943538348

result:

ok 1 number(s): "3103943538348"

Test #15:

score: -100
Time Limit Exceeded

input:

100000000000000 1

output:


result: