Skip to content

Commit b884643

Browse files
committed
Solved problem 1029D from codeforces
1 parent fc13a3e commit b884643

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
int const N = 2e5 + 1;
6+
int n, k, a[N];
7+
long long p[11];
8+
vector<long long> rem[11];
9+
10+
int main() {
11+
p[0] = 1;
12+
for(int i = 1; i < 11; ++i)
13+
p[i] = p[i - 1] * 10;
14+
15+
scanf("%d %d", &n, &k);
16+
for(int i = 0; i < n; ++i)
17+
scanf("%d", a + i);
18+
19+
for(int i = 0; i < n; ++i)
20+
rem[int(floor(log10(a[i])) + 1)].push_back(a[i] % k);
21+
22+
for(int i = 1; i <= 10; ++i)
23+
sort(rem[i].begin(), rem[i].end());
24+
25+
long long res = 0;
26+
for(int i = 0, l, r; i < n; ++i) {
27+
for(int j = 1; j <= 10; ++j) {
28+
if(rem[j].empty())
29+
continue;
30+
31+
long long tmp = (k - ((1ll * (a[i] % k) * (p[j] % k)) % k)) % k;
32+
if(tmp < 0)
33+
continue;
34+
35+
l = lower_bound(rem[j].begin(), rem[j].end(), tmp) - rem[j].begin();
36+
r = upper_bound(rem[j].begin(), rem[j].end(), tmp) - rem[j].begin();
37+
38+
res += r - l;
39+
}
40+
41+
if((1ll * ((a[i] % k) * (p[int(floor(log10(a[i])) + 1)] % k)) % k + a[i]) % k == 0)
42+
--res;
43+
}
44+
45+
printf("%lld\n", res);
46+
47+
return 0;
48+
}

CodeForces/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,3 +564,4 @@
564564
- [1029A. Many Equal Substrings](http://codeforces.com/contest/1029/problem/A)
565565
- [1029B. Creating the Contest](http://codeforces.com/contest/1029/problem/B)
566566
- [1029C. Maximal Intersection](http://codeforces.com/contest/1029/problem/C)
567+
- [1029D. Concatenated Multiples](http://codeforces.com/contest/1029/problem/D)

0 commit comments

Comments
 (0)