一、传送门
http://codeforces.com/contest/1029/problem/D
二、题意
给你$N$个数字$a_1,a_2,\cdots,a_n$,一个$K$,求所有$i \ne j$且$(a[i]*10^{len(a[j])}+a[j])%K=0$的pair的对数。
范围:$1 \le N \le 2*10^5$,$1 \le a[i] \le 10^9$,$1 \le K \le 10^9$。
三、思路
记录所有长度$i$,模$K$为$j$的数字的个数。用map记录,显然,键的种类不超过$2*10^5$。枚举map的所有键值对,假设键为$a$,解方程:$(a*10^x+b)%K=0(%K)$,当$x$已知时,$b=-a*10^x(%K)=(K-a*10^x)(%K)$。因为$1\le a[i] \le 10^9$,所以,$x$不超过$10$,可以枚举。然后已知$x$和$b$以后,作为键去map里面找出相应的值,和之前枚举的键值对的值相乘即可。
最后,因为有条件$i \ne j$,所以,再扫描一遍序列$a$,如果$(a[i]*10^{len(a[i])}+a[i])%K=0$,那么,答案减一。
但是,如果用pair<int, int>做map的键,上述思路的复杂度是$O(N*log(N)*log(N)*maxlen)$。这么大的复杂度我当时也想都没想就写了,我天,想什么呢。然后,我被Hack的TLE了。T_T。
那么,思路可以不变,把复杂度降一下就行了。把map的改为unordered_map,此时,键不能是pair,那就把记录个数的map改为$10$个unordered_map<int,long long>就行了。这样,需要枚举两层长度,再需要枚举长度为$i$下的unordered_map,总的复杂度变成了$O(N*maxlen*maxlen)$。那么,最差的ACM版复杂度就是$(N*10*10)$了,稳得很。
四、坑点
1、无论是map还是unordered_map,在迭代遍历的时候,千万不能通过方括号[]的方式去索引值。否则,如果键不存在,内置STL将会把键插进去,导致迭代次数和实际的size不相同。
2、预处理$10^i$,$i$最大是$10$,不是$9$。
3、最后扫描去掉$i=j$的情况时,因为$1 \le a[i] \le 10^9$,$10^{max\{len(a[i])\}}=10^10$,相乘等于$10^{19}$,大于long long的范围,会溢出,所以,一定要记得先把每一项$% K$,再相乘。否则,会挂在我代码最后面注释掉的那个样例。
五、代码(前面一大段都是没用的输入挂,有效代码在最后面)
#include<bits/stdc++.h> using namespace std; #define pb(x) push_back(x) #define mk(x, y) make_pair(x, y) #define pln() putchar('\n') #define cln() (cout << '\n') #define fi first #define se second #define MOD 1000000007LL typedef long long LL; typedef pair<int, int> PII; const int MAXN = 2e5 + 10; /*********************************************************************/ namespace fastIO { #define BUF_SIZE 100000 #define OUT_SIZE 100000 #define ll long long //fread->read bool IOerror = 0; inline char nc() { static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE; if (p1 == pend) { p1 = buf; pend = buf + fread(buf, 1, BUF_SIZE, stdin); if (pend == p1) {IOerror = 1; return -1;} //{printf("IO error!\n");system("pause");for (;;);exit(0);} } return *p1++; } inline bool blank(char ch) {return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';} inline void read(int &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()); if (IOerror)return; if (ch == '-')sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc())x = x * 10 + ch - '0'; if (sign)x = -x; } inline void read(ll &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()); if (IOerror)return; if (ch == '-')sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc())x = x * 10 + ch - '0'; if (sign)x = -x; } inline void read(double &x) { bool sign = 0; char ch = nc(); x = 0; for (; blank(ch); ch = nc()); if (IOerror)return; if (ch == '-')sign = 1, ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc())x = x * 10 + ch - '0'; if (ch == '.') { double tmp = 1; ch = nc(); for (; ch >= '0' && ch <= '9'; ch = nc())tmp /= 10.0, x += tmp * (ch - '0'); } if (sign)x = -x; } inline void read(char *s) { char ch = nc(); for (; blank(ch); ch = nc()); if (IOerror)return; for (; !blank(ch) && !IOerror; ch = nc()) * s++ = ch; *s = 0; } inline void read(char &c) { for (c = nc(); blank(c); c = nc()); if (IOerror) {c = -1; return;} } //getchar->read inline void read1(int &x) { char ch; int bo = 0; x = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())if (ch == '-')bo = 1; for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar()); if (bo)x = -x; } inline void read1(ll &x) { char ch; int bo = 0; x = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())if (ch == '-')bo = 1; for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar()); if (bo)x = -x; } inline void read1(double &x) { char ch; int bo = 0; x = 0; for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar())if (ch == '-')bo = 1; for (; ch >= '0' && ch <= '9'; x = x * 10 + ch - '0', ch = getchar()); if (ch == '.') { double tmp = 1; for (ch = getchar(); ch >= '0' && ch <= '9'; tmp /= 10.0, x += tmp * (ch - '0'), ch = getchar()); } if (bo)x = -x; } inline void read1(char *s) { char ch = getchar(); for (; blank(ch); ch = getchar()); for (; !blank(ch); ch = getchar()) * s++ = ch; *s = 0; } inline void read1(char &c) {for (c = getchar(); blank(c); c = getchar());} //scanf->read inline void read2(int &x) {scanf("%d", &x);} inline void read2(ll &x) { #ifdef _WIN32 scanf("%I64d", &x); #else #ifdef __linux scanf("%lld", &x); #else puts("error:can't recognize the system!"); #endif #endif } inline void read2(double &x) {scanf("%lf", &x);} inline void read2(char *s) {scanf("%s", s);} inline void read2(char &c) {scanf(" %c", &c);} inline void readln2(char *s) {gets(s);} //fwrite->write struct Ostream_fwrite { char *buf, *p1, *pend; Ostream_fwrite() {buf = new char[BUF_SIZE]; p1 = buf; pend = buf + BUF_SIZE;} void out(char ch) { if (p1 == pend) { fwrite(buf, 1, BUF_SIZE, stdout); p1 = buf; } *p1++ = ch; } void print(int x) { static char s[15], *s1; s1 = s; if (!x)*s1++ = '0'; if (x < 0)out('-'), x = -x; while(x)*s1++ = x % 10 + '0', x /= 10; while(s1-- != s)out(*s1); } void println(int x) { static char s[15], *s1; s1 = s; if (!x)*s1++ = '0'; if (x < 0)out('-'), x = -x; while(x)*s1++ = x % 10 + '0', x /= 10; while(s1-- != s)out(*s1); out('\n'); } void print(ll x) { static char s[25], *s1; s1 = s; if (!x)*s1++ = '0'; if (x < 0)out('-'), x = -x; while(x)*s1++ = x % 10 + '0', x /= 10; while(s1-- != s)out(*s1); } void println(ll x) { static char s[25], *s1; s1 = s; if (!x)*s1++ = '0'; if (x < 0)out('-'), x = -x; while(x)*s1++ = x % 10 + '0', x /= 10; while(s1-- != s)out(*s1); out('\n'); } void print(double x, int y) { static ll mul[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL, 100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL }; if (x < -1e-12)out('-'), x = -x; x *= mul[y]; ll x1 = (ll)floor(x); if (x - floor(x) >= 0.5)++x1; ll x2 = x1 / mul[y], x3 = x1 - x2 * mul[y]; print(x2); if (y > 0) {out('.'); for (size_t i = 1; i < y && x3 * mul[i] < mul[y]; out('0'), ++i); print(x3);} } void println(double x, int y) {print(x, y); out('\n');} void print(char *s) {while (*s)out(*s++);} void println(char *s) {while (*s)out(*s++); out('\n');} void flush() {if (p1 != buf) {fwrite(buf, 1, p1 - buf, stdout); p1 = buf;}} ~Ostream_fwrite() {flush();} } Ostream; inline void print(int x) {Ostream.print(x);} inline void println(int x) {Ostream.println(x);} inline void print(char x) {Ostream.out(x);} inline void println(char x) {Ostream.out(x); Ostream.out('\n');} inline void print(ll x) {Ostream.print(x);} inline void println(ll x) {Ostream.println(x);} inline void print(double x, int y) {Ostream.print(x, y);} inline void println(double x, int y) {Ostream.println(x, y);} inline void print(char *s) {Ostream.print(s);} inline void println(char *s) {Ostream.println(s);} inline void println() {Ostream.out('\n');} inline void flush() {Ostream.flush();} //puts->write char Out[OUT_SIZE], *o = Out; inline void print1(int x) { static char buf[15]; char *p1 = buf; if (!x)*p1++ = '0'; if (x < 0)*o++ = '-', x = -x; while(x)*p1++ = x % 10 + '0', x /= 10; while(p1-- != buf)*o++ = *p1; } inline void println1(int x) {print1(x); *o++ = '\n';} inline void print1(ll x) { static char buf[25]; char *p1 = buf; if (!x)*p1++ = '0'; if (x < 0)*o++ = '-', x = -x; while(x)*p1++ = x % 10 + '0', x /= 10; while(p1-- != buf)*o++ = *p1; } inline void println1(ll x) {print1(x); *o++ = '\n';} inline void print1(char c) {*o++ = c;} inline void println1(char c) {*o++ = c; *o++ = '\n';} inline void print1(char *s) {while (*s)*o++ = *s++;} inline void println1(char *s) {print1(s); *o++ = '\n';} inline void println1() {*o++ = '\n';} inline void flush1() {if (o != Out) {if (*(o - 1) == '\n')*--o = 0; puts(Out);}} struct puts_write { ~puts_write() {flush1();} } _puts; inline void print2(int x) {printf("%d", x);} inline void println2(int x) {printf("%d\n", x);} inline void print2(char x) {printf("%c", x);} inline void println2(char x) {printf("%c\n", x);} inline void print2(ll x) { #ifdef _WIN32 printf("%I64d", x); #else #ifdef __linux printf("%lld", x); #else puts("error:can't recognize the system!"); #endif #endif } inline void println2(ll x) {print2(x); printf("\n");} inline void println2() {printf("\n");} #undef ll #undef OUT_SIZE #undef BUF_SIZE }; namespace slowIO { template <class T> inline void read(T &x) { int t; bool flag = false; while((t = getchar()) != '-' && (t < '0' || t > '9')) ; if(t == '-') flag = true, t = getchar(); x = t - '0'; while((t = getchar()) >= '0' && t <= '9') x = x * 10 + t - '0'; if(flag) x = -x; } }; /*********************************************************************/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ /**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/ using namespace fastIO; //using namespace slowIO; LL n, k, a[MAXN], p10[20], len[MAXN]; typedef unordered_map<LL, LL> mp; mp cnt[12]; int calc(int x) { int res = 0; while(x > 0) { res++; x /= 10; } return res; } int main() { #ifndef ONLINE_JUDGE freopen("d.in", "r", stdin); #endif // ONLINE_JUDGE int ml = 0; p10[0] = 1; for(int i = 1; i <= 10; ++i)p10[i] = p10[i - 1] * 10LL; read(n), read(k); for(int i = 0; i < n; ++i) { read(a[i]); len[i] = calc(a[i]); ml = max(ml, (int)len[i]); cnt[len[i]][a[i] % k]++; } LL ans = 0; for(int j = 1; j <= ml; ++j) { for(auto e : cnt[j]) { LL A = e.fi; for(int i = 1; i <= ml; ++i) { LL B = ((k - A * (p10[i] % k)) % k + k) % k; B = (B + k) % k; mp::iterator it2 = cnt[i].find(B); if(it2 != cnt[i].end())ans += e.se * it2->se; } } } for(int i = 0; i < n; ++i) { LL t2 = (((a[i] % k) * (p10[len[i]] % k) % k) + a[i]) % k; if(t2 == 0)ans--; } cout << ans << endl; return 0; } /* 2 27961 1000000000 1000000000 */