2068. 整数拼接
思路:
取模操作大多都可以打表
对于每个 A i A j A_{i}A_{j} AiAj 都可以拆分为 A i ∗ 1 0 c n t [ j ] + A j A_{i} * 10^{cnt[j]} + A_{j} Ai∗10cnt[j]+Aj, c n t [ j ] cnt[j] cnt[j]记为 A j A_{j} Aj的位数
由此可以先哈希打表每个 A i ∗ 1 0 j A_{i} * 10^{j} Ai∗10j,每次查询有多少个 A j A_{j} Aj的可拼接数,查表即可
!判重
样例输入:
4 2
1 2 3 4
样例输出:
6
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n, m;
int a[12][N], b[N];
int main()
{
cin >> n >> m;
for(int i = 1; i <= n; i ++ )
{
int x;
cin >> x;
b[i] = x;
for(int j = 1; j <= 10; j ++ )
{
x = ((LL)x * 10) % m; // 1.开LL防止爆int 2.每次乘法都取余数不然爆LL
a[j][x % m] ++;
}
}
LL res = 0;
for(int i = 1; i <= n; i ++ )
{
int len = to_string(b[i]).size(); //string-tip
int t = (m - (b[i] % m)) % m;
if(t == 0) res += a[len][t] - 1;
else res += a[len][t];
}
cout << res << endl;
}