Title Description
N numbers a1, a2, ..., an. EndSaH intend to choose the number 2 stitching.
A splice shows a x, y positive integers two strings as numbers, x y spliced into a new first numeric string after.
As will fight number 1234 with 56, to give a different result if obtained 123456. splicing sequence is not the same, as will the number of 56 and 1234 to fight, to give 561,234.
However, EndSaH not like a multiple of k, so he wants to know how many of a number of (i, j) (1 <= i, j <= n, i! = J) meet a [i] and a [j] after the number of fight, the result is not a multiple of k.
Input Format
The first line of two positive integers n, k, as defined problem.
The second behavior positive integer n, denotes the i-th a [i].
Output Format
Line a positive integer representing the number to meet the requirements of the number of
SAMPLE INPUT
4 11
45 1 10 12
Sample Output
5
data range
1<=n<=10^5,1<=k,a[i]<=10^9
Analytical thinking
We found that a [i] and the number of a [j] after splicing, is obtained:
a [i] * pow (10, len [j]) + a [j], len [j] represents a [j] the number of bits, such as a [j] = 45, len [j] = 2
If the result is a multiple of k, then there must be ((a [i] * pow (10, len [j]))% k + (a [j]% k))% k == 0.
Since a [i] up to 10, 10 so we can start the tub size k, coun [i] [j] indicates the length of i,% k = j is the number of how many, and then enumerate x, so t = a [i] * 10 ^ x% k. then there are coun [x] [kt] and the number is not a [i] stitching, in particular, for t = 0, there are coun [x] [0] a number and not a [i] stitching, so a [i] is the contribution of the answer n-num-1, num represents the number not a [i] stitching. Since k is too large, a map maintenance, O (n) enumerate a [i] to obtain the answer. Note 9 * 10 ^ 10 ^ 10 will burst long long, so to take the edge side mode.
Conferred on the Code
#include<bits/stdc++.h> #define int long long #define ll long long using namespace std; const int N=1e5+5; int n,k; ll ans,a[N]; map<int,int> coun[15]; struct node{ int len,mod; }f[N]; inline int read(int now){ int r=0,t=1,sum=0,c=getchar(); while(c<'0'||c>'9') t=c=='-'?-1:1,c=getchar(); while(c>='0'&&c<='9') r=r*10+c-48,sum++,c=getchar(); f[now].len=sum; return r*t; } signed main(){ //freopen("piece.in","r",stdin); //freopen("piece.out","w",stdout); int maxn=0; n=read(0),k=read(0); for(int i=1;i<=n;i++){ a[i]=read(i); maxn=max(maxn,f[i].len); } for(int i=1;i<=n;i++){ coun[f[i].len][a[i]%k]++; f[i].mod=a[i]%k; } ll sss; for(int i=1;i<=n;i++){ int mt=a[i]%k;int num=0; coun[f[i].len][f[i].mod]--; for(int j=1;j<=maxn;j++){ if(j>=7){ sss=((ll)mt*(ll)pow(10,5))%k; sss=(sss*(ll)pow(10,j-5))%k; } else sss=((ll)mt*(ll)pow(10,j))%k; num+=coun[j][k-sss]; if(sss==0) num+=coun[j][0]; } ans+=n-num-1; coun[f[i].len][f[i].mod]++; } printf("%lld",ans); return 0; }