AcWing 2068. 整数拼接
原题链接
中等
作者:
fxzcloud
,
2020-10-15 14:53:56
,
所有人可见
,
阅读 679
import java.util.Scanner;
/* a[i]*10的(a[j]+"")次方+a[j]为k的倍数
* 即(a[i]*10的(a[j]+"")次方%k+a[k]%k)%k=0
*/
public class Main {
static int n,k;
static long ans;
static long[] arr ;
static int[][] book = new int[11][100005];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = (int) sc.nextLong();
k = (int) sc.nextLong();
arr = new long[n];
for(int i=0;i<n;i++) {
arr[i] = sc.nextLong();
for(int j=1;j<=10;j++) {
int t =(int) (arr[i]* Math.pow(10, j)%k);
book[j][t]++;
}
}
for(int i=0;i<n;i++) {
int len =(arr[i]+"").length();
int t = (int) (arr[i]%k);
int tt = (int) (arr[i]* Math.pow(10, len)%k);
if(t==0) {
if(tt!=0) {
ans+=book[len][0];
}else {
ans+=book[len][0]-1;
}
}else {
if(tt+t!=k) {
ans+=book[len][k-t];
}else {
ans+=book[len][tt]-1;
}
}
}
System.out.println(ans);
}
}