Codeforces Round #506 (Div. 3) - D. Concatenated Multiples(思维拼接求是否为k的倍数)
题意
给你N个数字和一个K,问一共有几种拼接数字的方式使得到的数字是K的倍数,拼接:“234”和“123”拼接得到“234123”
分析:
- N <= 2e5,简单的暴力O(N^2)枚举肯定超时
- 数字A和B拼接,B的位数最多10位,如果我们知道位数为(1-10)的数字和A拼接满足是K的倍数这样的数字有几个,就可以在N*10的复杂度下完成所有的拼接
- 在读入数据的时候,我们可以统计出数字的位数和对K取余的结果,这样我们就可以在O(1)的时间内得到所有满足的情况
#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#define ll long long
#define ull unsigned long long
#define N 200005
using namespace std; ll a[N];
// 获取长度
int getlen(int x) {
int sum = ;
while (x) {
++sum;
x /= ;
}
return sum;
}
map<int, int> mp[];
int main() { int n, k;
while (scanf("%d%d", &n, &k) != EOF) {
for (int i = ; i < n; ++i) {
scanf("%lld", &a[i]);
int len = getlen(a[i]);
mp[len][a[i] % k]++;
}
ll ans = ;
// 枚举N个数字
for (int i = ; i < n; ++i) {
ll mul = ;
int len = getlen(a[i]);
mp[len][a[i] % k]--;
// 对每个数字,分别对位数为 j 的数字进行拼接
for (int j = ; j <= ; ++j) {
mul *= ;
ull temp = a[i] * mul;
if (mp[j].find((k - temp % k) % k) != mp[j].end())
ans += mp[j][(k - temp % k) % k];
}
mp[len][a[i] % k]++;
}
printf("%I64d\n", ans);
for (int i = ; i < ; ++i)
mp[i].clear();
}
return ;
}
Codeforces Round #506 (Div. 3) - D. Concatenated Multiples(思维拼接求是否为k的倍数)的更多相关文章
- 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 #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 #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) 1029 D. Concatenated Multiples
题意: 给定n个数字,和一个模数k,从中选出两个数,直接拼接,问拼接成的数字是k的倍数的组合有多少个. 思路: 对于a,b两个数,假定len = length of (b),那么a,b满足条件就是a ...
- Codeforces Round #506 (Div. 3)
题解: div3水的没有什么意思 abc就不说了 d题比较显然的就是用hash 但是不能直接搞 所以我们要枚举他后面那个数的位数 然后用map判断就可以了 刚开始没搞清楚数据范围写了快速乘竟然被hac ...
- Codeforces Round #506 (Div. 3) C. Maximal Intersection
C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- 【Codeforces Round #506 (Div. 3) 】
A:https://www.cnblogs.com/myx12345/p/9844334.html B:https://www.cnblogs.com/myx12345/p/9844368.html ...
- Codeforces Round #506 (Div. 3)B.Creating the Contest(dp)
B. Creating the Contest time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- bzoj 4537: [Hnoi2016]最小公倍数 分块+并查集
题目大意: 给定一张n个点m条边的无向图,每条边有两种权.每次询问某两个点之间是否存在一条路径上的边的两种权的最大值分别等于给定值. n,q <= 50000. m <= 100000 题 ...
- JS undefined
undefined表示"缺少值",就是此处应该有一个值,但是还没有定义.典型用法是: (1)变量被声明了,但没有赋值时,就等于undefined. (2) 调用函数时,应该提供的参 ...
- asp.net分页asp.net无刷新分页高效率分页
项目中经常会用到分页的功能类似的项目做过无数个了,今个把自己常用的分页代码分享一下. 首先说说服务端处理的代码: 下面代码中重点是分页的sql语句的写法,其中的参数@n是当前的页码,总的来说本服务端主 ...
- requests 的使用
1.1.实例引入 # 引入Requests库 import requests # 发起GET请求 response = requests.get('https://www.baidu.com/') ...
- Python模块-sys模块
sys.version 获取Python解释程序的版本信息 >>> sys.version '2.7.12 (default, Dec 4 2017, 14:50:18 ...
- sql查询将列里面的值替换为别的值但是实际值不变
数据库有一张表BUG(缺陷记录表) 里面有字段severity(严重程度): severity的值实际为1,2,3,4,但希望在查询结果中将severity的1,2,3,4值显示为其他的值,但seve ...
- hibernate 数据关联多对多
多对多,必须有一张关系表来维持关系 数据库student,teacher student_teacher 三张表 但是在pojo中只需要建立student和teacher两个类,除非关系表也代表某种业 ...
- C#自定义控件 绘制框
上几张测试的 效果 虽然全是用.net 的绘图库画的,但是手动双缓冲,不会闪烁,感觉还不错,源码开放了,喜欢的拿去扩展吧; 用于撤销的存放图像的数据结构我设置为10个,怕是内存崩了,我看mspaint ...
- ubuntu下root用户默认密码及修改方法
[ubuntu下root用户默认密码及修改方法] 很多朋友用ubuntu,一般都是装完ubuntu系统,马上就修改root密码了,那么root用户的默认密码是多少,当忘记root用户密码时如何找回呢, ...
- 使用python ftplib包递归下载文件夹及文件
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2018-06-11 09:35:49 # @Author : Yaheng Wang ...