Codeforces Round #506 (Div. 3) D. Concatenated Multiples

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/tianwei0822/article/details/82077792

题目:点击打开链接

题意:给你n个数,求任意两个数组合而成能整除k的对数。

分析:根据除法的定义暴力就行了,用map或者unordered_map存一下余数就行。主要通过这题,对map和unordered_map有了一个新的认识,当用键值访问的时候就插入了,然后mp[key](unordered_map[key])自动赋值为0,导致查询的节点变多了效率变低了,所以可以用count函数先判断键是否存在,如果存在再访问,在有很多空键值需要访问的时候,用count函数预先判断可以有效的降低时间复杂度。听大佬说,unordered_map可能会退化成O(n),要慎用!另外,hash_map不是标准库的,不支持ll和string直接用,要写点东西才能用。

代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<complex>
#include<cstdlib>
#include<cstring>
#include<cassert>
#include<iomanip>
#include<string>
#include<cstdio>
#include<bitset>
#include<vector>
#include<cctype>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<deque>
#include<list>
#include<set>
#include<map>
using namespace std;
#define pt(a) cout<<a<<endl
#define debug test
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
///#define ll long long
#define ll unsigned long long
#define pb push_back
#define mp make_pair
#define inf 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
typedef pair<int,int> PII;
const ll mod = 1e9+7;
const int N = 1e6+10;

ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qp(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int to[4][2]={{-1,0},{1,0},{0,-1},{0,1}};

int n,k,a[N],b[N];
map<int,int> ma[12];

int main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>k;
    rep(i,1,n) {
        cin>>a[i];
        ll tp=a[i],ct=0;
        while(tp) tp/=10,ct++;
        ma[ct][a[i]%k]++;
        b[i]=ct;
    }
    ll ans=0;
    rep(i,1,n) {
        ma[b[i]][a[i]%k]--;///除去本身的贡献
        ll tp=a[i];
        rep(j,1,10) {
            tp=tp*10%k;
            ///ans += ma[j][k-tp];
            if(ma[j].count(k-tp)) ans += ma[j][k-tp];///查找互补的个数
            if(tp==0&&ma[j].count(0)) ans += ma[j][0];
        }
        ma[b[i]][a[i]%k]++;
    }
    cout<<ans<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tianwei0822/article/details/82077792