2068. 整数拼接

题目链接

2068. 整数拼接

给定一个长度为 n 的数组 A1,A2,,An

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

例如 12345 可以拼成 1234534512

注意交换 AiAj 的顺序总是被视为 2 种拼法,即便是 Ai=Aj 时。

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

输入格式

第一行包含 2 个整数 nK

第二行包含 n 个整数 A1,A2,,An

输出格式

一个整数代表答案。

数据范围

1n105,
1K105,
1Ai109

输入样例:

4 2 1 2 3 4

输出样例:

6

解题思路

模拟

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

  • 时间复杂度:O(11×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; }

__EOF__

本文作者acwing_zyy
本文链接https://www.cnblogs.com/zyyun/p/15921184.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zyy2001  阅读(105)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示