1. 程式人生 > >【題解】codeforces1029D[Codeforces Round #506 (Div. 3)]D.Concatenated Multiples 排序

【題解】codeforces1029D[Codeforces Round #506 (Div. 3)]D.Concatenated Multiples 排序

Description

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)(ij)(i,j) (i≠j) in array a such that the concatenation of aia_i and aja_j is divisible by kk.

Input

The first line contains two integers nn and kk (1n2×105,2k109)(1≤n≤2\times10^5, 2≤k≤10^9)

.

The second line contains nn integers a1,a2, ,ana_1,a_2,\cdots,a_n (1ai109)(1≤a_i≤10^9).

Output

Print a single integer — the number of ordered pairs of positions (i,j)(ij)(i,j) (i≠j) in array a such that the concatenation of aia_i

and aja_j is divisible by kk.

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)(1,2), (1,3), (2,3), (3,1), (3,4), (4,2), (4,3) suffice. They produce numbers 451,4510,110,1045,1012,121,1210451, 4510, 110, 1045, 1012, 121, 1210, respectively, each of them is divisible by 1111.

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

In the third example no pair is sufficient.

記錄每一個位數出現的模數,排序後統計每一個位數出現的需要的模數的個數,注意不要重複計算。

#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=2e5+10;
int n,k,fac[11],len[N];
ll cnt,a[N];
vector<int>vx[11];
int main()
{
	//freopen("in.txt","r",stdin);
	scanf("%d%d",&n,&k);fac[0]=1;
	for(int i=1;i<=10;i++)fac[i]=fac[i-1]*10%k;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a[i]);
		len[i]=log10(a[i])+1;
		vx[len[i]].push_back(a[i]%k);
	}
	for(int i=0;i<=10;i++)sort(vx[i].begin(),vx[i].end());
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=10;j++)
		{
			int mod=(a[i]*fac[j])%k;
			int key=(k-mod)%k;
			int l=lower_bound(vx[j].begin(),vx[j].end(),key)-vx[j].begin();
			int r=upper_bound(vx[j].begin(),vx[j].end(),key)-vx[j].begin();
			cnt+=r-l;
			if(len[i]==j&&(mod+a[i]%k)%k==0)cnt--;//去掉多算的 
		}
	}
	printf("%lld\n",cnt);
	return 0;
}

總結

這題看資料範圍發現暴力不可解,可以在每一個位數下記錄出現的 kk 模數,再進行計算。可以通過位數和模數來判斷是否把自己給算進去了。