当前位置:网站首页>洛谷P1836 数页码 题解
洛谷P1836 数页码 题解
2022-07-06 07:23:00 【q779】
洛谷P1836 数页码 题解
题目链接:P1836 数页码
题意:
一本书的页码是从 1 ∼ n 1\sim n 1∼n 编号的连续整数: 1 , 2 , 3 , ⋯ , n 1,2,3,\cdots,n 1,2,3,⋯,n。请你求出全部页码中所有单个数字的和,例如第 123 123 123 页,它的和就是 1 + 2 + 3 = 6 1+2+3=6 1+2+3=6。
1 ≤ n ≤ 1 0 9 1\le n\le 10^9 1≤n≤109
容易发现这个东西可以转化为
1 ∼ n 1 \sim n 1∼n 中每个数字有多少个
考虑数位dp,详见link
然后把每个数字乘一乘就好了
代码:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)()
int x,mi[25],cnt[25],num[25],f[25];
void solve(int x,int *cnt)
{
int len=0;
memset(num,0,sizeof(num));
while(x)
{
num[++len]=x%10;
x/=10;
}
for(int i=len; i>=1; i--)
{
for(int j=0; j<=9; j++)
cnt[j]+=f[i-1]*num[i];
for(int j=0; j<num[i]; j++)
cnt[j]+=mi[i-1];
int res=0;
for(int j=i-1; j>=1; j--)
{
res = res * 10 + num[j];
}
cnt[num[i]]+=res+1;
cnt[0]-=mi[i-1];
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
mi[0]=1;
for(int i=1; i<=18; i++)
{
f[i]=f[i-1]*10+mi[i-1];
mi[i]=10*mi[i-1];
}
cin >> x;
solve(x,cnt);
int res=0;
for(int i=0; i<=9; i++)
res+=cnt[i]*i;
cout << res << '\n';
return 0;
}
转载请说明出处
边栏推荐
猜你喜欢
Cookie技术&Session技术&ServletContext对象
The way to learn go (I) the basic introduction of go to the first HelloWorld
Ble of Jerry [chapter]
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
Internal and external troubles of "boring ape" bayc
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
OpenGL ES 学习初识(1)
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
Week6 weekly report
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
随机推荐
TypeScript接口与泛型的使用
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
智能终端设备加密防护的意义和措施
杰理之AD 系列 MIDI 功能说明【篇】
js對象獲取屬性的方法(.和[]方式)
杰理之BLE【篇】
Bit operation XOR
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Redis builds clusters
Chrome view page FPS
NiO programming introduction
1091: two or three things in childhood (multi instance test)
JDBC学习笔记
Set picture annotation in markdown
Typescript indexable type
杰理之BLE【篇】
How Navicat imports MySQL scripts
The best way to learn SEO: search engine
Internal and external troubles of "boring ape" bayc
可变参数重载时的内存错误