Codeforces Round #506 (Div. 3) 1029 D. Concatenated Multiples
题意:
给定n个数字,和一个模数k,从中选出两个数,直接拼接,问拼接成的数字是k的倍数的组合有多少个。
思路:
对于a,b两个数,假定$len = length of (b)$,那么a,b满足条件就是$a * (len个10) + b $是k的倍数,相当于$a * (len个10)% k + b % k = k$;
那么我们可以预处理出每个数字%k的结果,用map计数。然后枚举每个数字,每个数字都有10种可能,因为len最大为10。每一次查找map中的数字要用find函数,不要用【】运算,find是二分,【】查找的复杂度高。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <cmath> #include <queue> #include <list> #include <map> #include <set> using namespace std; //#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } // #define _DEBUG; //*// #ifdef _DEBUG freopen("input", "r", stdin); // freopen("output.txt", "w", stdout); #endif /*-----------------------showtime----------------------*/ const int maxn = 2e5+9; ll a[maxn],lo[maxn]; map<ll,ll>mp[11]; ll n,k; int main(){ scanf("%I64d%I64d", &n, &k); for(int i=1; i<=n; i++){ scanf("%I64d", &a[i]); ll tmp = 1ll*a[i],len = 0; while(tmp > 0){ tmp/=10; len++; } // debug(len); lo[i] = len; mp[len][a[i]%k]++; } ll ans = 0; for(int i=1; i<=n; i++){ ll tmp = 1ll * a[i]; // debug(tmp); for(int j=1; j<=10; j++){ tmp = (tmp * 10ll) % k; ll c = (k-tmp); if(c >= k) c = c % k; //ans += mp[j][c]; auto po = mp[j].find(c); if (po != mp[j].end()) ans += po->se; if(lo[i] == j && a[i]%k == c)ans--; } } printf("%I64d\n",ans); return 0; }
skr