Codeforces Round #506 D. Concatenated Multiples题解
一、传送门
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;
;
/*********************************************************************/
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
//fread->read
;
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, , BUF_SIZE, stdin);
; ;}
//{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) {
; ;
for (; blank(ch); ch = nc());
if (IOerror)return;
, ch = nc();
+ ch - ';
if (sign)x = -x;
}
inline void read(ll &x) {
; ;
for (; blank(ch); ch = nc());
if (IOerror)return;
, ch = nc();
+ ch - ';
if (sign)x = -x;
}
inline void read(double &x) {
; ;
for (; blank(ch); ch = nc());
if (IOerror)return;
, ch = nc();
+ ch - ';
if (ch == '.') {
; ch = nc();
');
}
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 = ;
}
inline void read(char &c) {
for (c = nc(); blank(c); c = nc());
; return;}
}
//getchar->read
inline void read1(int &x) {
; x = ;
;
+ ch - ', ch = getchar());
if (bo)x = -x;
}
inline void read1(ll &x) {
; x = ;
;
+ ch - ', ch = getchar());
if (bo)x = -x;
}
inline void read1(double &x) {
; x = ;
;
+ ch - ', ch = getchar());
if (ch == '.') {
;
'), 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 = ;
}
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, , BUF_SIZE, stdout); p1 = buf;
}
*p1++ = ch;
}
void print(int x) {
], *s1; s1 = s;
';
)out('-'), x = -x;
+ ;
while(s1-- != s)out(*s1);
}
void println(int x) {
], *s1; s1 = s;
';
)out('-'), x = -x;
+ ;
while(s1-- != s)out(*s1); out('\n');
}
void print(ll x) {
], *s1; s1 = s;
';
)out('-'), x = -x;
+ ;
while(s1-- != s)out(*s1);
}
void println(ll x) {
], *s1; s1 = s;
';
)out('-'), x = -x;
+ ;
while(s1-- != s)out(*s1); out('\n');
}
void print(double x, int y) {
, , , , , , , , ,
, 10000000000LL, 100000000000LL, 1000000000000LL, 10000000000000LL,
100000000000000LL, 1000000000000000LL, 10000000000000000LL, 100000000000000000LL
};
)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);
) {; i < y && x3 * mul[i] < mul[y]; '), ++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');}
, 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) {
];
char *p1 = buf;
';
)*o++ = '-', x = -x;
+ ;
while(p1-- != buf)*o++ = *p1;
}
inline void println1(int x) {print1(x); *o++ = '\n';}
inline void print1(ll x) {
];
char *p1 = buf;
';
)*o++ = '-', x = -x;
+ ;
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 ) == ; 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;
')) ;
';
+ t - ';
if(flag) x = -x;
}
};
/*********************************************************************/
/**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/
/**$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$**/
/**$$$$$$$$$$$$$$$$$$$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mp cnt[];
int calc(int x) {
;
) {
res++;
x /= ;
}
return res;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("d.in", "r", stdin);
#endif // ONLINE_JUDGE
;
p10[] = ;
; i <= ; ++i)p10[i] = p10[i - ] * 10LL;
read(n), read(k);
; 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 = ;
; j <= ml; ++j) {
for(auto e : cnt[j]) {
LL A = e.fi;
; 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;
}
}
}
; i < n; ++i) {
LL t2 = (((a[i] % k) * (p10[len[i]] % k) % k) + a[i]) % k;
)ans--;
}
cout << ans << endl;
;
}
/*
2 27961
1000000000 1000000000
*/
Codeforces Round #506 D. Concatenated Multiples题解的更多相关文章
- Codeforces Round #506 (Div. 3) 题解
Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...
- Codeforces Round #506 (Div. 3) D-F
Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给 ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #506 (Div. 3) E
Codeforces Round #506 (Div. 3) E dfs+贪心 #include<bits/stdc++.h> using namespace std; typedef l ...
- Codeforces Round #506 (Div. 3) D. Concatenated Multiples
D. Concatenated Multiples You are given an array aa, consisting of nn positive integers. Let's call ...
- 【Codeforces Round】 #431 (Div. 2) 题解
Codeforces Round #431 (Div. 2) A. Odds and Ends time limit per test 1 second memory limit per test ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
随机推荐
- 使用kbmMW#1轻松实现REST
使用kbmMW很容易创建REST服务器. 首先,我们制作服务器应用程序(或服务......取决于您). 在这种情况下,我们将添加一个简单的Form,为我们的kbmMW组件提供GUI和位置. 在Delp ...
- SWIFT推送之本地推送(UILocalNotification)之二带按钮的消息
上一篇讲到的本地推送是普通的消息推送,本篇要讲一下带按钮动作的推送消息,先上个图瞅瞅: 继上一篇的内容进行小小的改动: 在didFinishLaunchingWithOptions方法内进行以下修改 ...
- 那些年,追寻JMeter的足迹,免费送……
我们测试技术部武汉团队自16年引入jmeter以来,利用jmeter做了很多事情.首先运用jmeter进行接口测试,后续实现jmeter自动化冒烟测试,顺带也实现了线上环境的巡检,节省了大量人力.j ...
- C高级第四次作业
作业要求一 最简单的wordcount 具体要求:http://www.cnblogs.com/xinz/p/7426280.html 1.设计思路: 0.0版本设计思路: 第一步:读入用户想要操作的 ...
- Struts2重新学习1
一:框架 1:框架的意义在于可以大大提高我们的开发效率.框架时一种主动设计,使用框架必须遵守框架指定好的开发流程. 2:框架的强大之处不是源自它能让你做什么,而是它不能让你做什么.有规有矩,方可成方圆 ...
- 【转】python3中bytes和string之间的互相转换
问题: 比对算法测试脚本在python2.7上跑的没问题,在python3上报错,将base64转码之后的串打印出来发现,2.7版本和3是不一样的:2.7就是字符串类型的,但是3是bytes类型的,形 ...
- USB速率识别
低速设备D-上有一个1.5k欧的上拉电阻.高速和全速设别在D+上有一1.5k欧上拉电阻.连接后通过检测电压变化来了解设备是否为低速设别. 低速下:D+为“0”,D-为“1”是为“J”状态,“K”状态相 ...
- stenciljs 学习三 组件生命周期
stenciljs 组件包含好多生命周期方法, will did load update unload 实现生命周期的方法比价简单类似 componentWillLoad ....,使用typescr ...
- smarty学习——基本概念
学习一种框架,我们最基本的就是掌握框架的思想,同时了解框架的基本语法. 1.对于定界符的了解 有的smarty模板标签都被加上了定界符. 默认情况下是 { 和},但它们是可被改变的.例如,我们假定你在 ...
- 【MVC】Controller的使用
1,控制器中所有的动作方法必须声明为public,如声明为private或protected,将不被视为动作方法. 如果将Action声明为private,或者是添加[NonAction]属性,则不对 ...