1018 Subnumbers (35 分)
解题思路:
实在不会咱就n2暴力吧,应该能拿一点分哈哈。
算是思维找规律吧,这种题多写写就不难发现规律,对于题目的1021我们重新推演:
对于第一个数字开头的4个数为:
1
10
102
1021
变换一下:
1111
000
22
1
可以发现第i位开头的数字和是从第i位开始往后每个数字一直重复到结尾的数字(记为数字拓展)和,这是第一步。
然后我们在往下推:
对于第三个数字(第二个数字为0开头不计算)开头的2个数为:
2
21
变换一下:
22
1
同样对于第四个数字为:
1
我们就可以发现,每一位非0数字都会把该位之后的数字拓展(数字拓展即从该位往后填充重复数字直到末尾)重复一次,例如第一位数字拓展是1111,出现1次,第三位数字拓展是22,出现2次,第四位数字拓展是1,出现3次。所以答案是1*1111+2*22+3*1=1158。
所以我们先预处理出9个数字所有拓展情况下mod1e9+7的结果,然后遍历字符串,维护cnt(跳过0开头的情况),累加答案即可
代码:
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mp make_pair
#define pb push_back
#define G 6.67430*1e-11
#define rd read()
#define pi 3.1415926535
using namespace std;
const ll mod = 1e9 + 7;
const int MAXN = 30000005;
const int MAX2 = 300005;
inline ll read() {
ll x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch>'9') {
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
ll fpow(ll a, ll b)
{
ll ans = 1;
while (b)
{
if (b & 1)ans = ans * a % mod;
b >>= 1;
a = a * a % mod;
}
return ans;
}
int x[10][100005];
signed main()
{
int i;
for (int i = 1; i < 10; i++)
{
ll now = 0;
for (int j = 1; j <= 100000; j++)
{
now = (now * 10 + i) % mod;
x[i][j] = now;
}
}
string s;
cin >> s;
ll ans = 0;
ll cnt = 1;
for (i = 0; i < s.size(); i++)
{
s[i] -= '0';
if (s[i])
{
//cout << (int)s[i] << ' ' << s.size() - i << endl;
ans = (ans+cnt * x[s[i]][s.size() - i]%mod)%mod;
cnt++;
}
}
cout << ans << endl;
return 0;
}