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 ...
随机推荐
- Gym - 100801D:Distribution in Metagonia (数学)
题意:给定一个N,让你把它拆成若干个只含素因子2和3的数之和,且两两之间没有倍数关系,比如10=4+6. 思路:即是2因子的幂递增,3因子的幂递减:或者反之. 对于当前N,我们拆分出的数为num=2^ ...
- powermock, 强力模拟
1. powermock是基于mockito或者easymock,TestNG之上的mock: 2. 提供了对于静态函数,私有函数的mock 3. 下载地址:https://github.com/po ...
- WPF如何更改系统控件的默认高亮颜色 (Highlight brush)
我们在用WPF时, 经常会对系统控件的默认高亮等等颜色进行更改. 以前通常是用controlTemplate来实现. 今天发现一个更合理或者简单的方法: 用系统默认颜色的key, 比如 SystemC ...
- iOS获取设备型号的方法
1. [UIDevice currentDevice].model 自己写的看只抓到模拟器和iPhone.暂时不推荐. 2.自己写的找的方法再添加.直接 NSString * deviceMod ...
- k8s 基础(4) k8s安装
转自 http://www.cnblogs.com/informatics/p/7389806.html 安装和配置 从github.com/kubernetes/kubernetes上下载1.6.8 ...
- linux命令-bzip2压缩
gzip和bzip2 都是压缩命令 区别是压缩算法不一样 一般bzip2压缩的效率高 都不能压缩目录 bzip2 压缩 [root@wangshaojun ~]# bzip2 install.l ...
- maven可用镜像
<mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...
- 动态Result配置
步骤一:建立DynaAction,主要代码如下: package com.asm; public class DynaAction extends ActionSupport { private St ...
- C++之string类
1.String对象的初始化 string s1; 默认构造函数,s1为空串 string s4(n, 'c'); 将s4初始化为字符c的n个副本 string s ...
- 使用PM2管理nodejs进程分享
摘要:pm2 是一个带有负载均衡功能的Node应用的进程管理器.本文主要介绍了详解使用PM2管理nodejs进程,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧,希望能帮助 ...