当前位置:网站首页>洛谷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;
}
转载请说明出处
边栏推荐
猜你喜欢

Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed

Fundamentals of C language 9: Functions

Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied

You deserve this high-value open-source third-party Netease cloud music player
![Ble of Jerry [chapter]](/img/ce/127b597cdd238bb0c37326f5cf8265.png)
Ble of Jerry [chapter]

1091: two or three things in childhood (multi instance test)

Leetcode 78: subset

Oracle column to row -- a field is converted to multiple rows according to the specified separator

Seriously recommend several machine learning official account

mysql如何合并数据
随机推荐
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
Introduction to the basics of network security
Relevant introduction of clip image
杰理之BLE【篇】
#systemverilog# 可綜合模型的結構總結
Typescript interface properties
学go之路(二)基本类型及变量、常量
【线上问题处理】因代码造成mysql表死锁的问题,如何杀掉对应的进程
Chrome view page FPS
Multi attribute object detection on rare aircraft data sets: experimental process using yolov5
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
JDBC learning notes
Word delete the contents in brackets
【mysql学习笔记30】锁(非教程)
Configure raspberry pie access network
剪映的相关介绍
OpenJudge NOI 2.1 1749:数字方格
CF1036C Classy Numbers 题解
智能终端设备加密防护的意义和措施
Oracle column to row -- a field is converted to multiple rows according to the specified separator