[CF]D2. Submarine in the Rybinsk Sea (hard edition)

D2. Submarine in the Rybinsk Sea (hard edition)

Description

This problem differs from the previous one only in the absence of the constraint on the equal length of all numbers a1,a2,,ana1,a2,…,an.

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(a1a2ap1ap,b1b2bq1bq)f(a1a2…ap−1ap,b1b2…bq−1bq), where a1apa1…ap and b1bqb1…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)=12121212f(1111,2222)=12121212
f(7777,888)=7787878f(7777,888)=7787878
f(33,44444)=4443434f(33,44444)=4443434
f(555,6)=5556f(555,6)=5556
f(111,2222)=2121212f(111,2222)=2121212

 

Formally,

  • if pqp≥q then f(a1ap,b1bq)=a1a2apq+1b1apq+2b2ap1bq1apbqf(a1…ap,b1…bq)=a1a2…ap−q+1b1ap−q+2b2…ap−1bq−1apbq;
  • if p<qp<q then f(a1ap,b1bq)=b1b2bqpa1bqp+1a2ap1bq1apbqf(a1…ap,b1…bq)=b1b2…bq−pa1bq−p+1a2…ap−1bq−1apbq.

Mishanya gives you an array consisting of nn integers aiai, your task is to help students to calculate ni=1nj=1f(ai,aj)∑i=1n∑j=1nf(ai,aj) modulo 998244353998244353.

Input

The first line of the input contains a single integer nn (1n1000001≤n≤100000) — the number of elements in the array. The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — the elements of the array.

output

Print the answer modulo 998244353998244353.

Examples

Input

3
12 3 45

Output

12330

正确解法:

把数字都组合起来求他们的值。

我们发现 12 3 就是 222+33

也就是说,若12 这个2的位置 小于等于3的位数,那就变成 22

1这个位置大于3的位置,那就变成 200

所以我们统计一下所有数的位数,对于每个数来说,先加上他自己组合他自己的情况

再枚举每个数字,再枚举所有数的位数。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <stack>
11 #define test printf("kaike nb!\n");
12 typedef long long ll;
13 const int N=100000+100;
14 const int mod=998244353;
15 const ll inf=(1<<31)-1;
16 using namespace std;
17 int n,cnt[N];
18 ll a[N],ans=0,p10[30];
19 void init(ll x,int i)
20 {
21     int ans=0;
22     while(x)
23     {
24         ans++;
25         x/=10;
26     }
27     cnt[ans]++;
28 }
29 int main()
30 {
31     scanf("%d",&n);
32     p10[1]=1;
33     for(int i=2;i<30;i++)
34         p10[i]=(p10[i-1]*10)%mod;
35     for(int i=1;i<=n;i++)
36     {
37         scanf("%lld",&a[i]);
38         init(a[i],i);
39     }
40     for(int i=1;i<=n;i++)
41     {
42         ll b=a[i];
43         int pos=0;
44         while(b)
45         {
46             ll c=b%10;
47             b=b/10;
48             pos++;
49             for(int j=1;j<=10;j++)
50             {
51                 if(j>=pos)
52                 {
53                     ans+=c*cnt[j]*p10[pos*2];
54                     ans+=c*cnt[j]*p10[pos*2-1];
55                 }
56                 else
57                 {
58                     ans+=2*c*cnt[j]*p10[pos+j];
59                 }
60             }
61             ans%=mod;
62             //cout<<ans<<endl;
63         }
64         //cout<<ans<<endl;
65     }
66     printf("%lld\n",ans);
67  
68  
69     return 0;
70 }
View Code

 

posted @ 2019-07-21 09:01  kaike  阅读(443)  评论(0编辑  收藏  举报