2068. 整数拼接

题目链接

2068. 整数拼接

给定一个长度为 \(n\) 的数组 \(A_1,A_2,⋅⋅⋅,A_n\)

你可以从中选出两个数 \(A_i\)\(A_j\)(\(i\) 不等于 \(j\)),然后将 \(A_i\)\(A_j\) 一前一后拼成一个新的整数。

例如 \(12\)\(345\) 可以拼成 \(12345\)\(34512\)

注意交换 \(A_i\)\(A_j\) 的顺序总是被视为 \(2\) 种拼法,即便是 \(A_i=A_j\) 时。

请你计算有多少种拼法满足拼出的整数是 \(K\) 的倍数。

输入格式

第一行包含 \(2\) 个整数 \(n\)\(K\)

第二行包含 \(n\) 个整数 \(A_1,A_2,⋅⋅⋅,A_n\)

输出格式

一个整数代表答案。

数据范围

\(1≤n≤10^5,\)
\(1≤K≤10^5,\)
\(1≤A_i≤10^9\)

输入样例:

4 2
1 2 3 4

输出样例:

6

解题思路

模拟

\(A\)\(B\) 拼凑在一起的数为 \(A\times 10^{log_{10}B}+B\),模 \(k\)\((A\times 10^{log_{10}B}\%k+B\%k)\%k\),这里只需要枚举 \(B\) 即可,由于位数较小,可以预处理 \((A\times 10^{log_{10}B}\%k+B\%k)\%k\),然后统计余数出现的次数,注意不合法的现象

  • 时间复杂度:\(O(11\times n)\)

代码

// Problem: 整数拼接
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/2070/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
// #define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e5+5;
int a[N],n,k,cnt[N][11];
LL c[11];
int log10(int x)
{
	int res=0;
	while(x)res++,x/=10;
	return res;
}
int main()
{
    cin>>n>>k;
    c[0]=1;
    for(int i=1;i<=10;i++)c[i]=10*c[i-1];
    for(int i=1;i<=n;i++)
    {
    	cin>>a[i];
    	for(int j=0;j<=10;j++)cnt[(a[i]%k*(c[j]%k))%k][j]++;
    }
    LL res=0;
    for(int i=1;i<=n;i++)
	{
		res+=cnt[(k-a[i]%k)%k][log10(a[i])];
		if((k-a[i]%k)%k==a[i]%k&&cnt[a[i]%k][log10(a[i])])res--;	
	}
    cout<<res;
    return 0;
}
posted @ 2022-02-21 23:06  zyy2001  阅读(104)  评论(0编辑  收藏  举报