QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#356815#8303. Junior MathematicianFOYWA 1008ms3972kbC++234.0kb2024-03-18 12:48:282024-03-18 12:48:29

Judging History

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

  • [2024-03-18 12:48:29]
  • 评测
  • 测评结果:WA
  • 用时:1008ms
  • 内存:3972kb
  • [2024-03-18 12:48:28]
  • 提交

answer

#pragma GCC optimize "Ofast"
#include <iostream>
#include <vector>
#include <array>
using ll = int;
using namespace std;
using a2 = array<int, 2>;
const ll mod = 1e9+7;

ll sq(ll x) {
	return x*x;
}

ll fastFix(ll x, ll m) {
	if (x >= m) x-=m;
	return x;
}
ll midFix(ll x, ll m){
	return x%m;
}
ll fix(ll x, ll m) {
	return ((x%m)+m)%m;
}

ll modPow(ll n, ll p, ll mod) {
	ll b = n, o = 1;
	while (p) {
		if (p&1) o = (o*b)%mod;
		b = (b*b)%mod;
		p/=2;
	}
	return o;
}

void solve(string l, string r, int m) {
	string s = "";
	while (s.size() + l.size() < r.size()) s += "0";
	l = s+l;
	l = "0" + l;
	r = "0" + r;
	int om = m;
	m *= 2;
	// dp[i][j][k][l] -> i=0 => low, i=1 => mid, i=2 => hi
	// cnt such that dig sum is j, dig squared sum is k
	vector<vector<ll>> dp(m, vector<ll>(m));
	vector<a2> top(l.size()), bot(l.size());
	vector<ll> topMod(l.size()), botMod(l.size());
	vector<ll> pref(l.size());
	for (int i = 1; i < l.size(); i++) {
		top[i] = top[i-1];
		bot[i] = bot[i-1];
		
		top[i][0] = midFix(top[i][0] + sq(r[i]-'0'), m);
		top[i][1] = midFix(top[i][1] + r[i]-'0', m);
		bot[i][0] = midFix(bot[i][0] + sq(l[i]-'0'), m);
		bot[i][1] = midFix(bot[i][1] + l[i]-'0', m);
	}
	for (int i = 1; i < l.size(); i++) {
		ll suf = 2*modPow(10, l.size()-i-1, m);
		pref[i] = fix(suf, m);
		topMod[i] = topMod[i-1]*10 + r[i]-'0';
		topMod[i] = midFix(topMod[i],m);

		botMod[i] = botMod[i-1]*10 + l[i]-'0';
		botMod[i] = midFix(botMod[i],m);
	}
	dp[0][0] = 1;
	ll out = 0;

	if (fix(top.back()[0] + topMod.back()*2 - sq(top.back()[1]), m) == 0) {
		out++;
	}
	if (l != r && fix(bot.back()[0] + botMod.back()*2 - sq(bot.back()[1]), m) == 0) {
		out++;
	}

	if (l == r) {
		cout << out << endl;
		return;
	}

	auto test = [&](int x, int d, vector<a2> &prefVals, vector<ll> &prefMod) {
		// count total such that top[i][0] - (top[i][1])^2 + j - k = 0
		ll curSum = midFix(prefVals[x][1] + d, m);
		ll curSq = midFix(prefVals[x][0] + prefMod[x]*pref[x] + d*pref[x+1] + d*d, m);
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < m; k++) {
				if (fix(sq(curSum + j) - curSq - k, m) == 0) {
					out = fastFix((out+dp[j][k]), mod);
				}
			}
		}
	};
	vector<bool> eq(l.size()+1);
	eq[0] = true;
	bool eqNow = true;
	for (int i =0; i < l.size(); i++) {
		if (l[i] != r[i]) eqNow = false;
		eq[i+1] = eqNow;
	}
	ll base = 1;
	vector<vector<ll>> ndp(m, vector<ll>(m));
	while (l.size()) {
		int lo = l.back() - '0';
		int hi = r.back() - '0';
		l.pop_back();
		r.pop_back();
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < m; k++) {
				ndp[j][k] = 0;
			}
		}
		for (int l = 0; l < 10; l++) {
			int x = midFix(2*base*l + sq(l), m);
			for (int j = 0; j < m; j++) {
				ll a = midFix(j+l, m);
				for (int k = 0; k < m; k++) {
					ll b = fastFix(k + x,m);
					ndp[a][b] += dp[j][k];
				}
			}
		}
		for (int j = 0; j < m; j++) {
			for (int k = 0; k < m; k++) {
				ndp[j][k] = fastFix(ndp[j][k], mod);
			}
		}
		base = fix(base*10, m);
		int x = r.size()-1;
		for (int i = 0; i < 10; i++) {
			// count total such that top[i][0] - (top[i][1])^2 + j - k = 0
			if (eq[l.size()] && i < hi && i > lo) {
				test(x, i, top, topMod);
			}
			else if (!eq[l.size()]) {
				if (i < hi) {
					test(x, i, top, topMod);
				}
				if (i > lo) {
					test(x, i, bot, botMod);
				}
			}
		}
		swap(dp, ndp);
	}
  cout << fix(out, mod)  << '\n';
	//if (out != cnt) assert(out == cnt);
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
			/*for (int i = 10; i < 100; i++) {
			for (int j = i; j < 100; j++) {
				for (int k = 2; k <= 20; k++) {
					cout << i << ' ' << j << ' ' << k << endl;
					solve(to_string(i), to_string(j), k);
				}
			}
		}*/
	int t; cin >> t;
	while (t--) {
		string l, r; cin >> l >> r;
		int m; cin >> m;

		//int m = fix(rand(), 60)+1;
		//string l = to_string(fix(rand(), 10000));
		//string r = to_string(fix(rand(), 10000) + stoi(l));
		//cout << l <<' ' << r << ' ' << m << endl;
		solve(l, r, m);
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3632kb

input:

2
10
50
17
33
33
3

output:

2
1

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 33ms
memory: 3600kb

input:

1252
3893
6798
7
5883
8853
7
2999
4351
2
565
1767
7
1759
4751
10
79
8631
2
2128
8721
7
7890
8423
6
4708
7458
9
4501
6027
4
932
2708
2
3518
5859
7
4355
8296
3
2642
4470
10
7408
8939
8
4892
6777
7
4962
7976
6
2722
3171
7
6616
7527
6
7070
7612
5
429
2087
7
8786
8823
3
8831
8994
2
6346
8524
4
6026
8648
...

output:

505
447
767
143
357
5413
994
150
321
539
1083
388
1267
225
229
348
588
57
150
87
198
21
126
636
281
210
146
1538
635
143
476
700
966
173
275
29
239
48
1018
377
1349
503
646
43
980
685
563
1187
1
85
1
1025
38
16
1372
68
851
679
43
625
955
431
154
185
644
48
677
429
1182
80
518
258
34
281
36
721
148
2...

result:

ok 1252 lines

Test #3:

score: 0
Accepted
time: 828ms
memory: 3972kb

input:

1253
3862
4458
39
5034
7673
2
510
7150
27
3514
4389
7
6435
6996
58
7300
8758
12
8676
8774
44
8544
8573
49
2445
8608
49
7812
8378
42
4214
4543
50
4995
5544
22
677
6824
55
7444
8746
9
7224
8456
25
3857
5279
26
1343
6042
22
6173
6548
57
2386
6274
58
489
1984
24
1351
2880
47
8499
8624
25
307
8952
46
641...

output:

10
1580
243
143
13
166
4
1
144
17
13
26
107
149
49
82
286
4
83
75
25
5
234
15
200
333
5
31
115
46
17
227
2
129
48
70
142
35
27
244
220
62
251
370
10
375
58
73
211
107
17
79
362
30
1559
397
37
3
24
5
60
85
11
181
19
39
245
176
7
585
138
97
23
21
21
185
69
168
535
61
26
14
39
60
37
50
240
12
44
307
26...

result:

ok 1253 lines

Test #4:

score: 0
Accepted
time: 858ms
memory: 3776kb

input:

714
5435966
8310614
52
5704997
7881777
18
2219416
7123670
45
8753521
8908557
36
6324833
6708892
16
7826873
8940017
40
4564053
4856296
3
7244416
7308752
50
1082842
3048434
39
1798212
4400110
53
1816169
7932160
12
1357724
3034166
19
7895400
8555848
51
8776920
8789604
46
4322545
5374832
11
6456132
6467...

output:

48977
101411
106247
4030
20946
26428
93477
858
49983
49245
433492
88224
12879
218
95549
823
16450
152429
210336
36228
27704
25600
9019
234595
32348
153999
310550
39190
26443
79160
16566
89981
39522
115033
151913
13156
85511
206261
7650
103807
133667
6639
143061
37525
274112
436224
51814
38402
20615
...

result:

ok 714 lines

Test #5:

score: 0
Accepted
time: 921ms
memory: 3756kb

input:

500
47086127
8062735606
18
360878357
562767928
24
49480976
8576022783
13
3476757922
7841781308
36
2555413805
7145295185
18
180566103
8759489364
54
2458610039
3222800330
24
5333648352
6013137590
36
2790561710
8370697212
29
1598694193
3669508654
16
27316449
6307649412
22
739739068
8138716900
19
166733...

output:

461506504
8493277
655830486
125913564
262891609
164175315
32385778
19692674
192417430
133552569
293785322
389425594
487919772
17225005
136379
734106267
73512105
22427516
320865203
272792111
127408900
2921953
11288343
6894189
1171204
857703413
29856027
5021161
6285361
16526239
53011372
122737980
4944...

result:

ok 500 lines

Test #6:

score: -100
Wrong Answer
time: 1008ms
memory: 3740kb

input:

312
1466909309993075
8751933522748891
15
1470437399519306
2056692847618591
31
3462736756734265
3693974732267180
30
2550983922966066
2664700547522419
2
323739377300110
8202701694787388
33
4594231434730263
5457883714482730
48
2342146294298329
2905327391488781
37
576788792669307
7681525011048650
35
214...

output:

438993318
878464230
790139613
34253020
197215082
144236923
707008695
956822255
104604977
456681364
655676197
945216854
119107115
308570694
114322353
574407580
971221019
804586828
691313619
92713026
369626468
825872079
731020980
681779153
132058511
328761882
357714767
905932082
452043085
996488404
48...

result:

wrong answer 1st lines differ - expected: '304711751', found: '438993318'