QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#713703#9585. 划分数字icpc_zhzx034#TL 34ms5464kbC++143.1kb2024-11-05 20:19:342024-11-05 20:19:34

Judging History

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

  • [2024-11-05 20:19:34]
  • 评测
  • 测评结果:TL
  • 用时:34ms
  • 内存:5464kb
  • [2024-11-05 20:19:34]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define fi first
#define se second
#define mk make_pair
#define eb emplace_back
#define rep(i,l,r) for(int i=(l); i<=(r); ++i)
#define rep_(i,l,r) for(int i=(l); i>=(r); --i)
typedef long long lr;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
constexpr int mod1=998244353,mod2=1e9+7;
constexpr db pi=3.141592653589793,eps=1e-9;
constexpr int inf32=0x3f3f3f3f,Inf32=0xc0c0c0c0;
constexpr lr inf64=0x3f3f3f3f3f3f3f3f,Inf64=0xc0c0c0c0c0c0c0c0;
template<typename T>il T Max(T x,T y) { return (x>y)? x:y; }
template<typename T>il T Min(T x,T y) { return (x<y)? x:y; }
template<typename T>il T gcd(T x,T y) { return (!y)? x:gcd(y,x%y); }
template<typename T>il T Abs(T x) { return (x>0)? x:(-x); }
template<typename T>il T Rnd(T l,T r,mt19937_64 &eng)
{
	uniform_int_distribution<T> uid(l,r);
	return uid(eng);
}
mt19937_64 eng(chrono::high_resolution_clock::now().time_since_epoch().count());
constexpr int N=524300;
lr l,r,pw[21],b[21],pre[21][180][10][2],suf[21][180][10][3],suf2[21][180][10];
il lr calc(lr x)
{
	rep(i,1,19)
		b[i]=(x/pw[i-1])%10;
	memset(pre,0,sizeof(pre));
	memset(suf,0,sizeof(suf));
	memset(suf2,0,sizeof(suf2));
	pre[20][0][0][0]=1;
	rep_(i,19,1)
		rep(j,0,(20-i)*9)
			rep(k,0,9)
				rep(p,0,1)
				{
					int lim=(!p)? b[i]:9;
					rep(q,0,lim)
					{
						if(q>j)
							continue;
						int kk=(q)? q:k,pp=(p||(q<b[i]));
						pre[i][j][kk][pp]+=pre[i+1][j-q][k][p];
					}
				}
	suf[0][0][0][0]=1;
	rep(i,1,19)
		rep(j,0,i*9)
			rep(k,0,9)
				rep(p,0,2)
					rep(q,0,9)
					{
						if(q>j)
							continue;
						int kk=q,pp=p;
						if(q<b[i]) pp=1;
						if(q>b[i]) pp=2;
						suf[i][j][kk][pp]+=suf[i-1][j-q][k][p];
					}
	suf2[0][0][0]=1;
	rep(i,1,19)
		rep(j,0,i*9)
			rep(k,0,9)
				rep(q,0,9)
				{
					if(q>j)
						continue;
					int kk=q;
					suf2[i][j][kk]+=suf2[i-1][j-q][k];
				}
	lr ans=0;
	rep(i,1,18)
	{
		int lft=19-i,rgt=i;
		rep(j,1,lft*9)
			rep(k,0,rgt*9)
			{
				if(Abs(j-k)>9)
					continue;
				rep(jj,1,9)
					rep(kk,0,9)
					{
						if(!kk&&rgt>1)
							continue;
						if(j<jj||k<kk||(lft>1&&Abs(j-k-(jj<<1))<Abs(j-k))||(rgt>1&&Abs(j-k+(kk<<1))<=Abs(j-k)))
							continue;
						lr ansl,ansr,anss=0;
						ansl=pre[i+1][j][jj][0],ansr=suf[i][k][kk][0];
						anss+=ansl*ansr;
						ansl=pre[i+1][j][jj][0],ansr=suf[i][k][kk][1];
						anss+=ansl*ansr;
						ansl=pre[i+1][j][jj][1],ansr=suf2[i][k][kk];
						anss+=ansl*ansr;
						// if(anss)
						// 	cout<<"19 ~ "<<i+1<<" , "<<j<<" , "<<jj<<" ; ",
						// 	cout<<i<<" ~ 1 , "<<k<<" , "<<kk<<" . "<<anss<<'\n';
						ans+=anss*Abs(j-k);
					}
			}
	}
	return ans;
}
il void Solve()
{
	cin>>l>>r;
	pw[0]=1;
	rep(i,1,18)
		pw[i]=pw[i-1]*10;
	cout<<calc(r)-calc(l-1)<<'\n';
}
int main()
{
#ifdef LOCAL
	string fpre="test",isuf="in",osuf="out";
	assert(freopen((fpre+"."+isuf).c_str(),"r",stdin));
	assert(freopen((fpre+"."+osuf).c_str(),"w",stdout));
#endif
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T;
	cin>>T;
	while(T--)
		Solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 34ms
memory: 5464kb

input:

5
108 112
10 1000
10 10000
10 100000
114514 1919810

output:

16
3136
31636
316636
5715693

result:

ok 5 number(s): "16 3136 31636 316636 5715693"

Test #2:

score: -100
Time Limit Exceeded

input:

1000
66 87
51 78
63 98
25 50
33 71
76 79
25 44
20 65
52 56
34 82
38 75
75 82
36 99
48 85
11 43
32 65
57 90
34 42
22 53
15 60
53 63
48 50
23 84
42 50
76 80
43 91
10 33
27 65
70 88
89 93
37 78
23 27
27 57
44 58
83 84
76 90
46 97
21 35
59 88
22 98
62 84
65 83
45 67
42 89
72 78
45 57
61 76
15 34
18 39
4...

output:


result: