codeforce div3 506

D. Concatenated Multiples(1029D)

题目链接:http://codeforces.com/contest/1029/problem/D
我咋个想都是 O ( n 2 ) 的复杂度T_T,还是太挫了
这道题看懂了之后就是跟以前很经典的一道题一个意思:给 n 个数,让找出两个数的和等于 K 的对数有几对,以前就是咋个想对数 n 2 的复杂度,结果,别人是直接找某个数的互补的那个数有几个就行了,用map比较方便,这道题的思路也是一样的,只不过变了一下,这道题是说两个数 A , B 要接起来变成 A B ,这道题就是找 B 的互补有几个。

这道题具体做法:预处理每个数后面添加几个0并取模,这样就很方便的找了,比如说找 123 作为 B ,对 11 取模的个数, 123 % 11 = 2 ,那我就找他互补的 11 2 = 9 ,也就是说找个 A 后面添加了 3 0 取模之后是等于 9 的有几个就行了,而这个是之前预处理出来了的,查找非常快,思路就是酱~

但是这道题。。。。被卡超时卡得很恼火,减少取模次数,然后又改单组,最后靠一个优秀的输入挂刚好过的T_T

#include"bits/stdc++.h"
#define out(x) cout<<#x<<"="<<x
#define C(n,m) (m>n?0:(long long)fac[(n)]*invf[(m)]%MOD*invf[(n)-(m)]%MOD)
using namespace std;
typedef long long LL;
const int maxn=2e5+5;
const int MOD=1e9+7;
map<LL,LL>Mp[20];
LL a[maxn];
LL P[20]={1}; 
namespace fastIO {
    #define BUF_SIZE 100000
    //fread -> read
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend) {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1) {
                IOerror = 1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    inline void read(LL &x) {
        char ch;
        while(blank(ch = nc()));
        if(IOerror) return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    #undef BUF_SIZE
};
using namespace fastIO;
int main()
{
    for(int i=1;i<=15;i++)P[i]=P[i-1]*10;
    LL N,K;
    read(N);
    read(K);
    for(int i=1;i<=N;i++)
    {
        read(a[i]);
        LL t=a[i];
        for(int j=1;j<=10;j++)
        {
            t*=10;
            t%=K;
            Mp[j][t]++;
        }
    }
    LL ans=0;
    for(int i=1;i<=N;i++)
    {
        LL t=a[i]%K;
        int len=log10(a[i])+1;
        ans+=Mp[len][(K-a[i]%K)%K];
        if((t*P[len]+t)%K==0LL)ans--;//减掉自己又当A又当B的情况 
    }
    cout<<ans<<endl;
}

猜你喜欢

转载自blog.csdn.net/SwustLpf/article/details/82181870