zoukankan      html  css  js  c++  java
  • CodeForces D. Concatenated Multiples

    http://codeforces.com/contest/1029/problem/D

    You are given an array aa, consisting of nn positive integers.

    Let's call a concatenation of numbers xx and yy the number that is obtained by writing down numbers xx and yy one right after another without changing the order. For example, a concatenation of numbers 1212 and 34563456 is a number 123456123456.

    Count the number of ordered pairs of positions (i,j)(i,j) (iji≠j) in array aa such that the concatenation of aiai and ajaj is divisible by kk.

    Input

    The first line contains two integers nn and kk (1n21051≤n≤2⋅105, 2k1092≤k≤109).

    The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109).

    Output

    Print a single integer — the number of ordered pairs of positions (i,j)(i,j) (iji≠j) in array aa such that the concatenation of aiai and ajaj is divisible by kk.

    Examples
    input
    Copy
    6 11
    45 1 10 12 11 7
    output
    Copy
    7
    input
    Copy
    4 2
    2 78 4 10
    output
    Copy
    12
    input
    Copy
    5 2
    3 7 19 3 3
    output
    Copy
    0

    代码:

    #include <bits/stdc++.h>
    using namespace std;
    
    const int maxn = 2e5 + 10;
    int N, K;
    int num[maxn];
    map<long long, long long> mp[15];
    
    int main() {
        scanf("%d%d", &N, &K);
        for(int i = 1; i <= N; i ++) {
            scanf("%d", &num[i]);
            long long a = num[i];
            for(int j = 1; j <= 10; j ++) {
                a *= 10;
                a %= K;
                mp[j][a] ++;
            }
        }
        long long cnt = 0;
        for(int i = 1; i <= N; i ++) {
            int t = num[i] % K;
            int len = log10(num[i]) + 1;
            cnt += mp[len][(K - t) % K];
            long long x = 1;
            for(int j = 1; j <= len; j ++)
                x = (x * 10) % K;
            if(((num[i] * x) % K + num[i] % K) % K == 0)
                cnt --;
        }
        printf("%I64d
    ", cnt);
        return 0;
    }
    

      

  • 相关阅读:
    .NET开发微信公众号之创建自定义菜单
    FOR XML PATH 可以将查询结果根据行输出成XML格式
    Node入门
    javascript客户端检测技术
    理解OAuth 2.0(转)
    RESTful API 设计指南(转)
    forever让nodejs应用后台执行
    Git使用教程
    NodeJS基础教程
    windows系统下简单nodejs安装及环境配置
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/9822824.html
Copyright © 2011-2022 走看看