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 ...
随机推荐
- LOJ2719 「NOI2018」冒泡排序
「NOI2018」冒泡排序 题目描述 最近,小S 对冒泡排序产生了浓厚的兴趣.为了问题简单,小 S 只研究对 1 到n 的排列的冒泡排序. 下面是对冒泡排序的算法描述. 输入:一个长度为n 的排列p[ ...
- log4net 使用
1. 代码中使用配置文件: log4net.Config.DOMConfigurator.Configure(new FileInfo("log4netConfig.xml")); ...
- JavaScript-Tool:template
ylbtech-JavaScript-Tool: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http:/ ...
- Python-通过socket实现一个小型的端口检测工具
实验机器IP:192.168.220.139,端口开放情况 代码 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import ...
- .NET牛人养成计划
六大喜讯:(1)对于小型平板等授权免费(2)编译平台Rosly开源,ASP.NET全系平台开源(ASP.NET,Web API):ASP.NET跨平台,Mono,让ASP.NET运行在Linux和Un ...
- 《Spring实战》系列之Bean的装配-Days02
2.1 回顾 对于我第一天在bean的装配中写的,是一些基本的语法或者是Spring本身的一些规定,但是我没有对此进行深究.接下来就让我们仔细的讨论一下细节问题.和传统的类的定义和方法的调用做一些比较 ...
- C++经典问题:狐狸找兔子
问题描述: 围绕着山顶有10个洞,一只狐狸和一只兔子住在各自的洞里.狐狸想吃掉兔子.一天,兔子对狐狸说:"你想吃我有一个条件,先把洞从1-10编上号,你从10号洞出发,先到1号洞找我:第二次 ...
- JVM原理解析
JVM主要的功能: 内存分配 程序调度 内存释放(栈等自动释放.堆垃圾回收) 异常处理 https://www.cnblogs.com/dingyingsi/p/3760447.html https: ...
- R语言 arules包 apriori()函数中文帮助文档(中英文对照)
apriori(arules) apriori()所属R语言包:arules Mining Associations w ...
- 6、git和github
参考:http://www.cnblogs.com/qianmojing/p/6484162.htmlhttp://www.jb51.net/article/70729.htmhttp://www.c ...