coeforce---Submarine in the Rybinsk Sea

地址:
easy:https://codeforces.com/contest/1195/problem/D
因为每个数的长度相同,所以每个数的每个位对答案的贡献是在答案的2k位,2k-1位

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const LL mod = 998244353;
const int N = 1e5 + 5;
 
LL a[N];
 
LL solve(LL x)
{
    LL res = 0;
    LL index = 1;
    while(x){
        LL r = x % 10LL;
        res = (res + index * 11LL % mod * r % mod) % mod;
        x /= 10LL;
        index = index * 100LL % mod;
    }
    return res % mod;
}
 
int main()
{
    int n;
    scanf("%d",&n);
    LL ans = 0;
    for(int i = 0;i < n;++i){
        scanf("%lld",&a[i]);
        ans = (ans + solve(a[i])) % mod;
    }
    printf("%lld\n",ans * n * 1LL % mod);
    return 0;
}

hard: https://codeforces.com/contest/1195/problem/D2

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int N = 1e5 + 5;
const LL mod = 998244353;

LL val[55],pow10[55];
LL len[N],n;

LL solve(LL x)
{
    int cnt = 0;
    while(x){
        val[cnt + 1] += x % 10LL;
        x /= 10LL;
        cnt++;
    }
    return cnt;
}

int main()
{
    memset(val,0,sizeof(val));
    scanf("%lld",&n);
    for(int i = 1;i <= n;++i){
        LL x;
        scanf("%lld",&x);
        len[i] = solve(x);
    }
    pow10[0] = 1;
    for(int i = 1;i <= 21;++i){
        pow10[i] = 10LL * pow10[i - 1] % mod;
    }
    LL ans = 0;
    for(int j = 10;j >= 1;--j){  //讨论所有数的第j位和,对每个数来说会产生的贡献
        for(int i = 1;i <= n;++i){
            if(len[i] >= j){
                //当j小于等于当前对比的数的长度,那第j位和产生对答案的贡献是对2*k位和2*k-1位
                ans = (ans + pow10[(j - 1) * 2] * 11LL % mod * val[j] % mod) % mod;
            }else{
                //当j大于当前对比的数的长度,那第j位和产生对答案的贡献是len[i]+ j - 1位
                ans = (ans + pow10[len[i] + j - 1] * 2LL % mod * val[j] % mod) % mod;
            }
        }
    }
    printf("%lld\n",ans);
    return 0;
}

发布了269 篇原创文章 · 获赞 33 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/qq_36386435/article/details/96738116