D. Concatenated Multiples

You are given an array aa, consisting of nn positive integers.

Let's call a concatenation of numbers xx and yy the number that is obtained by writing down numbers xx and yy one right after another without changing the order. For example, a concatenation of numbers 1212 and 34563456 is a number 123456123456.

Count the number of ordered pairs of positions (i,j)(i,j) (i≠ji≠j) in array aa such that the concatenation of aiai and ajaj is divisible by kk.

Input

The first line contains two integers nn and kk (1≤n≤2⋅1051≤n≤2⋅105, 2≤k≤1092≤k≤109).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109).

Output

Print a single integer — the number of ordered pairs of positions (i,j)(i,j) (i≠ji≠j) in array aa such that the concatenation of aiai and ajaj is divisible by kk.

Examples
input

Copy
6 11
45 1 10 12 11 7
output

Copy
7
input

Copy
4 2
2 78 4 10
output

Copy
12
input

Copy
5 2
3 7 19 3 3
output

Copy
0
Note

In the first example pairs (1,2)(1,2), (1,3)(1,3), (2,3)(2,3), (3,1)(3,1), (3,4)(3,4), (4,2)(4,2), (4,3)(4,3) suffice. They produce numbers 451451, 45104510, 110110, 10451045, 10121012, 121121, 12101210, respectively, each of them is divisible by 1111.

In the second example all n(n−1)n(n−1) pairs suffice.

In the third example no pair is sufficient.

 题意:给出n个数,再给一个mod,然后现在有一种方法说是,可以把任意两个数连接起来,问你连接起来的对数取余mod等于0的有多少个

思路:乍一看没什么思路,暴力肯定不行,10^5的数n^2就炸了,我们就要想怎么去优化他,我们可以考虑预处理然后遍历一遍

我们先给出一个例子  给出n个数和一个mod,求多少对加起来mod等于0?

这个n^2也不行,但是我们想一下如果我取余一个数=x 的话   我要什么情况才能让这个数加一个数%mod等于0

我们只有(x+y)%mod == 0     那在我们知道x的情况,我们只要找 mod-y的余数个数有多少即可

同理我们可以推理到这个题:因为是连接 12  34     我们相当于看成是  1200+34即可,就成功转移到了以上问题

因为连接不同的数的时候后面紧跟的0的个数不同,我们只要存下一个0到十个0所有的都用map存下即可

然后特别的,最后判断下自身加自身会不会也可以被mod为0,如果是的话-1

map<ll,ll> mp[11]

mp[j][x]    代表的是  j个0余数为x的个数

#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include<string>
#include<iostream>
#include<queue>
#include<algorithm>
#define mod 1000000007
using namespace std;
typedef long long ll;
int n,m;
int main()
{
int a[];
map<ll,ll> mp[];
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
ll x=a[i];
for(int j=;j<=;j++)//预处理存余数个数
{
x*=;
x%=m;
mp[j][x]++;
}
}
ll sum=;
for(int i=;i<n;i++)
{
int t=a[i]%m;
int len=log10(a[i])+;
sum+=mp[len][(m-t)%m]; //加上与当前数连接余数为0的个数
ll x=;
for(int j=;j<=len;j++) x=(x*)%m; //除去自身
if(((a[i]*x)%m+a[i]%m)%m==) sum--;
}
printf("%I64d",sum);
}

Codeforces Round #506 (Div. 3) D. Concatenated Multiples的更多相关文章

  1. Codeforces Round #506 (Div. 3) - D. Concatenated Multiples(思维拼接求是否为k的倍数)

    题意 给你N个数字和一个K,问一共有几种拼接数字的方式使得到的数字是K的倍数,拼接:“234”和“123”拼接得到“234123” 分析: N <= 2e5,简单的暴力O(N^2)枚举肯定超时 ...

  2. Codeforces Round #506 (Div. 3) 题解

    Codeforces Round #506 (Div. 3) 题目总链接:https://codeforces.com/contest/1029 A. Many Equal Substrings 题意 ...

  3. Codeforces Round #506 (Div. 3) D-F

    Codeforces Round #506 (Div. 3) (中等难度) 自己的做题速度大概只尝试了D题,不过TLE D. Concatenated Multiples 题意 数组a[],长度n,给 ...

  4. Codeforces Round #506 (Div. 3) E

    Codeforces Round #506 (Div. 3) E dfs+贪心 #include<bits/stdc++.h> using namespace std; typedef l ...

  5. Codeforces Round #506 (Div. 3) 1029 D. Concatenated Multiples

    题意: 给定n个数字,和一个模数k,从中选出两个数,直接拼接,问拼接成的数字是k的倍数的组合有多少个. 思路: 对于a,b两个数,假定len = length of (b),那么a,b满足条件就是a ...

  6. Codeforces Round #506 (Div. 3)

    题解: div3水的没有什么意思 abc就不说了 d题比较显然的就是用hash 但是不能直接搞 所以我们要枚举他后面那个数的位数 然后用map判断就可以了 刚开始没搞清楚数据范围写了快速乘竟然被hac ...

  7. 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 ...

  8. 【Codeforces Round #506 (Div. 3) 】

    A:https://www.cnblogs.com/myx12345/p/9844334.html B:https://www.cnblogs.com/myx12345/p/9844368.html ...

  9. 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 ...

随机推荐

  1. 最新的vueWebpack项目

    最近优化了我的vueWebpack多入口框架,感觉清新了好多:http://pan.baidu.com/s/1bNYJp0

  2. IntelliJ IDEA 第一个 Scala 程序

    IntelliJ 安装完成 Scala 插件后,你需要尝试使用 IntelliJ 来创建并且运行第一个程序. 通常这个程序只是简单的输出 Hello World. 创建一个新工程 在文件下面选择新建, ...

  3. 模拟curl函数

    只要需要调用微信的网址,就需要模拟curl请求 $tokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_cr ...

  4. CentOS6.8下实现配置配额

    CentOS6.8下实现配置配额 Linux系统是支持多用户的,即允许多个用户同时使用linux系统,普通用户在/home/目录下均有自己的家目录,在默认状态下,各个用户可以在自己的家目录下任意创建文 ...

  5. Spring配置表友好性优化思路

    Spring配置表需要尽量保证对程序员的友好性,一下提供一种优化思路. 中途未保存,心态炸了,只贴图了,fuuuuuuuuuuuuuck 第一种(最烂,最不友好):以Json的格式保存在配置表中,程序 ...

  6. 『Re』正则表达式模块_常用方法记录

    『Re』知识工程作业_主体识别 一个比较完备的正则表达式介绍 几个基础函数 re.compile(pattern, flags=0) 将正则表达式模式编译成一个正则表达式对象,它可以用于匹配使用它的m ...

  7. php值传递和引用传递

    1,参数传值方式有两种,第一种是值传递,第二种引用传递.值传递比较简单,也就是在php中,数组是当一个普通变量,值传递是要一个实参的一个拷贝副本,跟实参无关,而引用传递后可以改变实参的值而类的对象是无 ...

  8. 解决Eureka Server不踢出已关停的节点的问题

    eureka端: eureka.server.enable-self-preservation (设为false,关闭自我保护主要) eureka.server.eviction-interval-t ...

  9. [NOIP 2015TG D1T3] 斗地主

    题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏.斗地主是一种使用黑桃.红心.梅花.方片的A到K加上大小王的共54张牌来进行的扑克牌游戏.在斗地主中,牌的大小关系根据牌的数码表示如下:3<4< ...

  10. PReLU

    PReLU全名Parametric Rectified Linear Unit. PReLU-nets在ImageNet 2012分类数据集top-5上取得了4.94%的错误率,首次超越了人工分类的错 ...