Codeforces #506 D. Concatenated Multiples

D. Concatenated Multiples

time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output

You are given an array a, consisting of n positive integers.

Let's call a concatenation of numbers x and y the number that is obtained by writing down numbers x and y one right after another without changing the order. For example, a concatenation of numbers 12 and 3456 is a number 123456.

Count the number of ordered pairs of positions (i,j) (i≠j) in array a such that the concatenation of ai and aj is divisible by k.

Input

The first line contains two integers n and k (1≤n≤2⋅10^5, 2≤k≤10^9).

The second line contains n integers a1,a2,…,an (1≤ai≤10^9).

Output

Print a single integer — the number of ordered pairs of positions (i,j) (i≠j) in array a such that the concatenation of ai and aj is divisible by k.

Examples

input

6 11
45 1 10 12 11 7

output

7

input

4 2
2 78 4 10

output

12

input

5 2
3 7 19 3 3

output

0

Note

In the first example pairs (1,2), (1,3), (2,3), (3,1), (3,4), (4,2), (4,3) suffice. They produce numbers 451, 4510, 110, 1045, 1012, 121, 1210, respectively, each of them is divisible by 11.

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

In the third example no pair is sufficient.

Overview

    Given a set of numbers, you can concatenate any two of the numbers numerically, and ask how many splicing methods there are if the concatenated number can be divisible by k.

Ideas

    There is nothing that can be mathematically simplified about this problem. There is only one way to traverse the calculation.

    Suppose a is spliced ​​to the left of b with bn digits, the resulting number is a*10^bn+b. If a*10^bn+b mod k == 0, the count is +1.

    However, brute force traversal is O(n^2), and the difficulty is how to reduce the time complexity to O(nlogn).

    When traversing a, we can see from a*10^bn+b mod k == 0 that if bn is fixed, the value of b will be fixed. Ifsearch b according to bn classification, we only need to find the fixed value b for each bn.

    When it comes to finding the number of fixed values, it is easy to think of two methods: hash table and ordered structure, but k is too large, so hash table is not considered.

    The time complexity of multiset insertion and search is O(logn). After using multiset optimization, the worst-case insertion and traversal complexity does not exceed O(n*(logn+9log(n/9))), but the constant may be too large and cause TLE. Just use vector sorting instead.

  • 0<bn<11, precalculating the result of 10^bn can save time

code

#include<bits\stdc++.h> 
typedef long long ll;
using namespace std; 
const int MAX=2e5+5;

int a[MAX];        //input
int len[MAX];      //len of ai
int mt[11];        //(10^i)%k
vector<int> d[11]; //a's of len i

int main(){
	int n,k;
	cin>>n>>k;
	int t=10%k;
	for(int i=1;i<11;i++){
		mt[i]=t;
		t=(10*t)%k;
	}
	for(int i=0;i<n;i++){
		int t,c=0;
		cin>>t;
		a[i]=t%k;
		while(t){
			c++;
			t/=10;
		}
		len[i]=c;
		d[c].push_back(a[i]);
	}
	ll cnt=0;
	for(int i=1;i<11;i++)
		sort(d[i].begin(),d[i].end());
	for(int i=0;i<n;i++){
		for(int j=1;j<11;j++){
			int t=k-((ll)a[i]*mt[j])%k;
			if(t==k) t=0;
			cnt+=(upper_bound(d[j].begin(),d[j].end(),t)-lower_bound(d[j].begin(),d[j].end(),t));
			if(len[i]==j && a[i]==t) cnt--;
		}
	}
	cout<<cnt;
	return 0;
}

http://www.cnblogs.com/hizcard/  Please indicate the source for reprinting 

Guess you like

Origin blog.csdn.net/hizcard/article/details/82113707