QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#71772#2020. 微信步数He_Ren100 ✓404ms95632kbC++143.3kb2023-01-12 01:05:422023-01-12 01:05:44

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-01-12 01:05:44]
  • 评测
  • 测评结果:100
  • 用时:404ms
  • 内存:95632kb
  • [2023-01-12 01:05:42]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int MAXN = 5e5 + 5;
const int MAXM = 10 + 5;
const ll linf = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

inline void add_mod(int &a,int b){ a+=b; if(a>=mod) a-=mod;}
inline int mod_add(int a,int b){ return a+b>=mod? a+b-mod: a+b;}

inline ll pw(ll a,ll b)
{
	ll res = 1;
	while(b)
	{
		if(b&1) res=res*a%mod;
		a=a*a%mod; b>>=1;
	}
	return res;
}

const int MAXF = 1e3 + 5;
ll fac[MAXF], inv[MAXF];

inline int interpolation(int ys[],int n,ll x)
{
	x %= mod;
	int res = 0;
	static int pre[MAXM+5], suf[MAXM+5];
	pre[0] = suf[n-1] = 1;
	for(int i=0; i+1<n; ++i) pre[i+1] = (ll)pre[i] * (x-i) %mod;
	for(int i=n-1; i>0; --i) suf[i-1] = (ll)suf[i] * (x-i) %mod;
	for(int i=0; i<n; ++i)
	{
		ll cur = inv[i] * inv[n-i-1] %mod;
		if((n-i-1) & 1) cur = -cur;
		res = (res + cur * pre[i] %mod * suf[i] %mod * ys[i]) %mod;
	}
	add_mod(res, mod);
	return res;
}

int a[MAXM];
int c[MAXN], d[MAXN];
int mn[MAXN][MAXM], mx[MAXN][MAXM];
int len[MAXN][MAXM];

int main(void)
{
	fac[0] = 1;
	for(int i=1; i<MAXF; ++i) fac[i] = fac[i-1] * i %mod;
	inv[MAXF-1] = pw(fac[MAXF-1], mod-2);
	for(int i=MAXF-2; i>=0; --i) inv[i] = inv[i+1] * (i+1) %mod;
	
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1; i<=m; ++i) scanf("%d",&a[i]);
	for(int i=1; i<=n; ++i) scanf("%d%d",&c[i],&d[i]);
	
	ll lim = linf;
	static int cx[MAXM];
	for(int i=1; i<=m; ++i) len[0][i] = a[i];
	for(int i=1; i<=n; ++i)
	{
		memcpy(mn[i], mn[i-1], (m+1)<<2);
		memcpy(mx[i], mx[i-1], (m+1)<<2);
		memcpy(len[i], len[i-1], (m+1)<<2);
		
		int k = c[i];
		cx[k] += d[i];
		mn[i][k] = min(mn[i][k], cx[k]);
		mx[i][k] = max(mx[i][k], cx[k]);
		len[i][k] = a[k] - (mx[i][k] - mn[i][k] + 1) + 1;
		if(len[i][k] <= 0) lim = min(lim, (ll)i);
	}
	
	auto getlen = [&] (int k,ll t)
	{
		ll num = t / n; t %= n;
		ll tmn = mn[n][k], tmx = mx[n][k];
		if(cx[k] < 0)
			tmn = min(tmn, min(num * cx[k] + mn[t][k], (num-1) * cx[k] + mn[n][k]));
		if(cx[k] > 0)
			tmx = max(tmx, max(num * cx[k] + mx[t][k], (num-1) * cx[k] + mx[n][k]));
		return a[k] - (tmx - tmn + 1) + 1;
	};
	
	if(lim == linf)
	{
		for(int i=1; i<=m; ++i) if(cx[i])
		{
			ll l = n, r = (ll)a[i] * n + 5;
			while(l<r)
			{
				ll mid = (l+r)>>1;
				if(getlen(i, mid) > 0) l = mid+1;
				else r = mid;
			}
			lim = min(lim, l);
		}
	}
	if(lim == linf)
	{
		printf("-1");
		return 0;
	}
	
	int ans = 0;
	for(int i=1; i<=n; ++i) if(i <= lim)
	{	
		ll t = (lim - i) / n + 1;
		
		int fir = 1;
		for(int j=1; j<=m; ++j)
			fir = (ll)fir * len[i][j] %mod;
		add_mod(ans, fir);
		if(t == 1) continue;
		
		static int ys[MAXM+5];
		for(int j=0; j<m+2; ++j) ys[j] = 1;
		for(int k=1; k<=m; ++k)
		{
			int cmn = mn[n][k], cmx = mx[n][k];
			int has = cx[k];
			for(int j=0; j<m+2; ++j, has += cx[k])
			{
				cmn = min(cmn, has + mn[i][k]);
				cmx = max(cmx, has + mx[i][k]);
				
				int clen = a[k] - (cmx - cmn + 1) + 1;
				ys[j] = (ll)ys[j] * clen %mod;
				
				cmn = min(cmn, has + mn[n][k]);
				cmx = max(cmx, has + mx[n][k]);
			}
		}
		for(int j=1; j<m+2; ++j) ys[j] = (ys[j-1] + ys[j]) %mod;
		
		add_mod(ans, interpolation(ys, m+2, t - 2));
	}
	
	int tot = 1;
	for(int i=1; i<=m; ++i) tot = (ll)tot * a[i] %mod;
	add_mod(ans, tot);
	
	printf("%d",ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 2ms
memory: 5700kb

input:

5 5
2 2 1 3 3
1 1
2 -1
1 1
5 -1
5 1

output:

63

result:

ok 1 number(s): "63"

Test #2:

score: 5
Accepted
time: 3ms
memory: 5888kb

input:

5 5
2 2 2 3 2
5 -1
5 1
2 1
2 1
5 1

output:

108

result:

ok 1 number(s): "108"

Test #3:

score: 5
Accepted
time: 3ms
memory: 5644kb

input:

5 5
3 3 1 3 2
4 1
4 -1
1 -1
3 -1
2 1

output:

150

result:

ok 1 number(s): "150"

Test #4:

score: 5
Accepted
time: 1ms
memory: 5704kb

input:

100 3
8 7 6
1 1
1 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
1 1
1 -1
1 1
1 -1
2 1
2 -1
1 1
1 -1
2 1
2 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
2 1
2 -1
3 1
3 -1
3 1
3 -1
1 1
1 -1
1 1
1 -1
3 1
3 -1
3 1
3 -1
1 1
1 -1
1 1
1 -1
2 1
2 -1
1 1
1 -1
2 1
2 -1
2 1
2 -1
2 1
2 -1
3 1
3 -1
1 1
1 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
3 1
3 -1
...

output:

-1

result:

ok 1 number(s): "-1"

Test #5:

score: 5
Accepted
time: 3ms
memory: 7904kb

input:

100 3
6 7 5
1 1
2 1
3 1
2 -1
2 -1
3 -1
3 -1
2 -1
3 1
2 -1
2 -1
2 1
1 -1
1 1
2 -1
2 -1
2 1
3 -1
3 -1
1 -1
2 1
3 -1
3 -1
1 -1
3 1
3 1
1 1
3 -1
1 1
1 1
1 -1
1 1
3 1
2 -1
2 1
2 1
1 -1
2 1
1 -1
2 1
3 -1
2 -1
1 -1
3 1
1 -1
2 1
2 -1
1 -1
2 1
2 -1
3 -1
1 1
3 -1
1 1
1 -1
3 1
1 1
2 1
3 1
2 1
2 1
2 -1
2 1
3 1
...

output:

1445

result:

ok 1 number(s): "1445"

Test #6:

score: 5
Accepted
time: 2ms
memory: 5704kb

input:

100 3
5 7 6
3 1
1 1
2 -1
2 -1
2 1
2 1
1 1
1 -1
2 1
2 -1
3 1
2 1
3 -1
2 -1
1 1
1 -1
1 -1
3 1
2 1
1 1
1 1
2 1
1 1
1 1
2 1
1 1
3 -1
3 1
3 1
3 1
2 1
3 1
3 -1
3 -1
3 1
2 1
1 -1
1 1
1 1
3 -1
1 -1
3 -1
3 -1
2 1
2 1
3 -1
2 -1
2 -1
3 -1
1 1
1 -1
1 -1
3 1
2 -1
2 1
3 -1
1 -1
2 1
2 -1
1 -1
1 -1
2 1
3 -1
1 1
3 1...

output:

1823

result:

ok 1 number(s): "1823"

Test #7:

score: 5
Accepted
time: 26ms
memory: 23676kb

input:

100000 1
84020
1 -1
1 -1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 -1
1 1
1 1
1 1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 -1
1 -1
1 -1
1 1
1 -1
1 -1
1 1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 1
1...

output:

333173136

result:

ok 1 number(s): "333173136"

Test #8:

score: 5
Accepted
time: 25ms
memory: 21984kb

input:

100000 1
74078
1 -1
1 1
1 1
1 -1
1 -1
1 -1
1 1
1 1
1 1
1 1
1 1
1 -1
1 1
1 1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 -1
1 -1
1 -1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 1
1 1
1 -1
1 1
1 -1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 1
1 -1
1 -1
1 1
1 -1
1 -1
1 -1
1 -1
1 1
1 1
1 1
1 -1
1 1
1 -1
1 -1
1 1
...

output:

453472675

result:

ok 1 number(s): "453472675"

Test #9:

score: 5
Accepted
time: 20ms
memory: 23616kb

input:

100000 2
788727 548098
1 -1
2 1
2 -1
2 1
1 -1
1 1
2 -1
2 -1
1 -1
2 1
2 -1
2 1
2 -1
1 -1
2 -1
2 1
1 -1
2 1
1 -1
1 -1
1 1
1 -1
1 -1
2 -1
1 1
1 -1
2 1
2 1
1 -1
1 1
1 -1
1 1
2 -1
2 -1
1 -1
2 -1
2 1
2 -1
2 -1
2 -1
1 -1
2 -1
2 1
2 1
1 1
1 1
2 1
1 -1
2 -1
2 1
1 -1
1 -1
1 -1
2 -1
1 1
1 -1
1 1
2 1
2 1
1 1
1 ...

output:

433871022

result:

ok 1 number(s): "433871022"

Test #10:

score: 5
Accepted
time: 29ms
memory: 23864kb

input:

100000 2
851838 526074
1 1
1 1
1 1
1 -1
2 -1
2 -1
1 1
2 1
2 1
2 1
2 1
1 1
1 -1
2 1
1 1
2 1
1 -1
2 -1
1 -1
2 1
2 1
2 -1
1 -1
2 -1
2 -1
2 -1
1 -1
2 -1
2 1
2 -1
2 1
2 -1
2 -1
2 1
2 1
2 -1
2 -1
1 -1
2 -1
1 -1
1 -1
1 -1
2 1
1 -1
1 1
2 -1
2 -1
1 1
2 1
2 -1
2 1
1 -1
2 -1
1 1
2 1
2 -1
1 -1
1 -1
1 1
2 -1
2 1...

output:

635878930

result:

ok 1 number(s): "635878930"

Test #11:

score: 5
Accepted
time: 33ms
memory: 23648kb

input:

100000 2
936902 869936
1 -1
1 -1
1 -1
2 -1
2 1
1 -1
2 1
2 1
2 -1
1 1
2 -1
2 -1
1 -1
2 -1
1 -1
1 -1
1 -1
2 -1
2 1
2 1
2 -1
2 -1
2 1
1 1
1 -1
1 -1
2 -1
1 -1
2 -1
1 -1
2 -1
1 -1
2 1
2 -1
1 -1
2 1
2 1
1 1
1 -1
1 1
2 -1
2 1
2 -1
2 -1
1 -1
2 1
1 1
1 1
1 1
1 -1
2 1
2 1
1 -1
1 -1
2 -1
2 -1
2 1
1 1
2 -1
1 -1...

output:

442317474

result:

ok 1 number(s): "442317474"

Test #12:

score: 5
Accepted
time: 26ms
memory: 23580kb

input:

100000 2
696105 696736
2 1
1 -1
1 -1
1 1
1 1
1 -1
1 -1
2 1
2 -1
2 -1
1 -1
1 1
1 -1
2 1
2 -1
1 1
2 -1
1 -1
1 -1
1 1
1 -1
2 -1
1 1
2 1
1 1
1 -1
1 1
1 -1
2 1
1 -1
2 1
1 1
1 1
2 -1
1 -1
2 1
1 1
1 -1
2 1
1 -1
2 1
2 1
1 -1
1 -1
1 -1
2 1
2 1
2 1
1 -1
1 1
2 1
2 -1
2 -1
2 -1
2 -1
2 -1
2 1
1 1
2 1
2 -1
1 -1
1...

output:

564885404

result:

ok 1 number(s): "564885404"

Test #13:

score: 5
Accepted
time: 57ms
memory: 95316kb

input:

500000 10
755576 503368 607237 931815 889701 742191 594456 676526 704905 569952
9 1
9 -1
8 1
8 -1
4 1
4 -1
7 1
7 -1
3 1
3 -1
1 1
1 -1
6 1
6 -1
1 1
1 -1
10 1
10 -1
2 1
2 -1
1 1
1 -1
2 1
2 -1
5 1
5 -1
4 1
4 -1
3 1
3 -1
7 1
7 -1
3 1
3 -1
4 1
4 -1
6 1
6 -1
7 1
7 -1
4 1
4 -1
1 1
1 -1
2 1
2 -1
5 1
5 -1
6 ...

output:

-1

result:

ok 1 number(s): "-1"

Test #14:

score: 5
Accepted
time: 386ms
memory: 95632kb

input:

500000 10
843479 559917 806296 766435 965493 781368 889933 632099 689781 934269
3 1
10 1
3 1
3 -1
6 1
7 -1
8 1
9 1
4 1
2 -1
5 1
2 -1
5 1
8 -1
4 -1
10 -1
8 -1
7 -1
1 -1
3 -1
5 1
6 1
3 -1
6 -1
3 1
10 -1
5 1
4 1
2 1
10 -1
5 -1
4 1
5 -1
4 -1
5 -1
4 1
3 -1
8 1
5 1
3 -1
1 -1
5 1
2 -1
8 1
5 1
9 -1
4 -1
10 ...

output:

212475448

result:

ok 1 number(s): "212475448"

Test #15:

score: 5
Accepted
time: 404ms
memory: 95372kb

input:

500000 10
564550 662731 907937 653446 887637 552698 501487 502890 574491 689451
8 -1
2 1
2 -1
10 -1
5 -1
10 1
9 1
8 1
6 1
6 1
2 -1
2 -1
9 -1
1 -1
9 -1
7 -1
8 1
9 -1
9 1
9 -1
6 1
10 1
1 -1
8 -1
10 -1
10 1
7 1
4 1
6 1
1 -1
2 -1
2 1
5 -1
2 -1
7 1
5 -1
8 -1
5 1
7 -1
2 -1
3 1
9 1
3 -1
7 1
8 1
5 -1
1 1
6 ...

output:

27023740

result:

ok 1 number(s): "27023740"

Test #16:

score: 5
Accepted
time: 382ms
memory: 95412kb

input:

500000 10
918488 957499 964399 900665 976079 674192 501308 980114 958902 545155
6 1
1 -1
1 1
9 1
1 1
5 -1
4 1
1 -1
9 -1
6 1
1 -1
6 1
10 1
5 -1
8 1
10 1
2 1
7 1
5 -1
6 -1
10 -1
1 -1
4 -1
6 -1
4 1
10 -1
10 -1
6 -1
4 -1
7 -1
4 1
6 1
6 -1
10 1
9 1
4 -1
4 1
3 -1
7 -1
1 -1
5 1
8 1
10 1
2 1
2 -1
2 1
3 1
6 ...

output:

128050940

result:

ok 1 number(s): "128050940"

Test #17:

score: 5
Accepted
time: 138ms
memory: 95612kb

input:

500000 3
826619243 796070724 841056572
3 1
2 1
3 -1
1 -1
2 1
2 -1
1 -1
1 1
1 -1
3 1
1 -1
2 -1
3 -1
3 1
1 -1
1 1
3 -1
2 1
2 1
3 -1
2 1
2 1
1 -1
1 1
1 1
1 -1
2 1
3 -1
2 -1
2 1
3 1
1 1
1 -1
3 1
2 -1
1 -1
2 1
2 1
2 -1
3 -1
1 -1
1 -1
2 -1
3 -1
1 -1
3 -1
2 1
2 -1
3 1
1 -1
1 1
1 1
1 1
1 1
3 -1
1 1
3 1
2 1
...

output:

953829771

result:

ok 1 number(s): "953829771"

Test #18:

score: 5
Accepted
time: 151ms
memory: 95436kb

input:

500000 3
956491428 866109891 631435277
2 1
1 -1
1 -1
3 -1
3 1
2 -1
2 1
1 -1
2 1
2 -1
1 -1
2 1
2 1
2 -1
1 1
3 1
3 -1
1 -1
1 1
1 1
3 -1
2 -1
1 1
1 1
2 -1
2 1
2 -1
2 -1
2 1
3 1
1 -1
2 -1
1 -1
2 1
3 1
1 1
3 -1
1 -1
2 -1
3 1
2 1
3 1
3 1
2 1
3 -1
1 -1
3 -1
2 1
1 1
2 1
3 -1
3 1
1 -1
3 -1
1 1
2 -1
2 -1
2 1
...

output:

286394049

result:

ok 1 number(s): "286394049"

Test #19:

score: 5
Accepted
time: 147ms
memory: 95576kb

input:

500000 3
816766322 779617142 794227651
3 1
2 1
1 1
3 -1
2 1
2 -1
1 1
1 1
1 1
3 -1
1 -1
2 1
3 1
1 -1
1 -1
1 1
3 1
3 1
1 -1
3 -1
2 1
1 -1
2 1
3 1
1 1
2 -1
3 -1
2 -1
1 -1
3 -1
1 1
3 -1
3 1
1 -1
3 1
2 -1
2 -1
1 -1
3 1
3 1
2 -1
3 1
3 -1
1 1
1 -1
3 1
1 1
2 1
2 1
1 1
2 1
1 -1
2 1
2 -1
2 1
3 -1
3 1
1 1
3 -1...

output:

950925221

result:

ok 1 number(s): "950925221"

Test #20:

score: 5
Accepted
time: 144ms
memory: 95616kb

input:

500000 3
600925621 781710332 540983402
1 -1
1 1
2 1
3 1
1 1
1 1
3 1
3 1
2 -1
3 -1
3 1
1 -1
1 1
2 -1
1 -1
1 -1
3 -1
2 1
1 1
1 -1
1 -1
1 1
3 -1
2 -1
2 -1
3 1
1 1
1 -1
3 1
2 1
1 -1
2 1
1 -1
3 -1
3 -1
2 -1
3 -1
2 1
3 -1
1 1
2 1
2 1
3 -1
2 1
1 1
1 1
3 1
2 -1
1 1
3 1
2 -1
3 -1
3 1
2 -1
3 -1
1 1
1 1
1 -1
3...

output:

370329204

result:

ok 1 number(s): "370329204"

Extra Test:

score: 0
Extra Test Passed