Description
You are given an array , consisting of positive integers.
Let’s call a concatenation of numbers and the number that is obtained by writing down numbers and one right after another without changing the order. For example, a concatenation of numbers and is a number .
Count the number of ordered pairs of positions in array a such that the concatenation of and is divisible by .
Input
The first line contains two integers and .
The second line contains integers .
Output
Print a single integer — the number of ordered pairs of positions in array a such that the concatenation of and is divisible by .
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 suffice. They produce numbers , respectively, each of them is divisible by .
In the second example all 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;
}
总结
这题看数据范围发现暴力不可解,可以在每一个位数下记录出现的 模数,再进行计算。可以通过位数和模数来判断是否把自己给算进去了。