D1. Submarine in the Rybinsk Sea (easy edition)

题目

D1. Submarine in the Rybinsk Sea (easy edition)
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
This problem differs from the next one only in the presence of the constraint on the equal length of all numbers a1,a2,…,ana1,a2,…,an. Actually, this problem is a subtask of the problem D2 from the same contest and the solution of D2 solves this subtask too.

A team of SIS students is going to make a trip on a submarine. Their target is an ancient treasure in a sunken ship lying on the bottom of the Great Rybinsk sea. Unfortunately, the students don’t know the coordinates of the ship, so they asked Meshanya (who is a hereditary mage) to help them. He agreed to help them, but only if they solve his problem.

Let’s denote a function that alternates digits of two numbers f(a1a2…ap−1ap,b1b2…bq−1bq)f(a1a2…ap−1ap,b1b2…bq−1bq), where a1…apa1…ap and b1…bqb1…bq are digits of two integers written in the decimal notation without leading zeros.

In other words, the function f(x,y)f(x,y) alternately shuffles the digits of the numbers xx and yy by writing them from the lowest digits to the older ones, starting with the number yy. The result of the function is also built from right to left (that is, from the lower digits to the older ones). If the digits of one of the arguments have ended, then the remaining digits of the other argument are written out. Familiarize with examples and formal definitions of the function below.

For example:
f(1111,2222)=12121212
f(1111,2222)=12121212
f(7777,888)=7787878
f(7777,888)=7787878
f(33,44444)=4443434
f(33,44444)=4443434
f(555,6)=5556
f(555,6)=5556
f(111,2222)=2121212
f(111,2222)=2121212
Formally,

if p≥qp≥q then f(a1…ap,b1…bq)=a1a2…ap−q+1b1ap−q+2b2…ap−1bq−1apbqf(a1…ap,b1…bq)=a1a2…ap−q+1b1ap−q+2b2…ap−1bq−1apbq;
if p<qp<q then f(a1…ap,b1…bq)=b1b2…bq−pa1bq−p+1a2…ap−1bq−1apbqf(a1…ap,b1…bq)=b1b2…bq−pa1bq−p+1a2…ap−1bq−1apbq.
Mishanya gives you an array consisting of nn integers aiai. All numbers in this array are of equal length (that is, they consist of the same number of digits). Your task is to help students to calculate ∑ni=1∑nj=1f(ai,aj)∑i=1n∑j=1nf(ai,aj) modulo 998244353998244353.

Input
The first line of the input contains a single integer nn (1≤n≤1000001≤n≤100000) — the number of elements in the array. The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the elements of the array. All numbers a1,a2,…,ana1,a2,…,an are of equal length (that is, they consist of the same number of digits).

Output
Print the answer modulo 998244353998244353.

Examples
inputCopy
3
12 33 45
outputCopy
26730
inputCopy
2
123 456
outputCopy
1115598
inputCopy
1
1
outputCopy
11
inputCopy
5
1000000000 1000000000 1000000000 1000000000 1000000000
outputCopy
265359409

题意:用给的公式计算新组成的数字的和。
思路:暴力做的,,超时了。只会暴力的渣崽无可奈何。
查大佬们的博客发现通过维护每位数的贡献值来操作。
比如12,34.可以形成1122,3344,1324,3142.第k位数在2k和2k-1位都贡献一次(用自己形成新数的2k位或2k-1位)。n个数一共贡献n次,算出每个数的贡献值,加和n。(例如12,贡献值为1122n)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
ll a[100010];
int b[20],n;
ll findx(ll x)
{
    int len=0;
    while(x)
    {
        b[++len]=x%10;
        x/=10;
    }
    ll res=0;
    ll k=1;
    for(int i=1;i<=len;i++)
    {
        res=(res+b[i]*k)%mod;
        k=k*10%mod;
        res=(res+b[i]*k)%mod;
        k=k*10%mod;
    }
    return res*n%mod;
}
int main()
{
    int i;
    ll ans;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    ans=0;
    for(i=1;i<=n;i++)
    {
        ans+=findx(a[i]);
        ans=ans%mod;
    }
    cout<<ans<<endl;
    return 0;
}

还发现了另一种思路
f(a,b)+f(b,a)=f(a,a)+f(b,b);
详见
https://blog.csdn.net/douglasconnor/article/details/97568354

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值