[agc011e]increasing numbers

题意:

如果一个十进制非负整数的所有数位从高位到低位是不减的,我们称它为“上升数”,例如1558,11,3,0都是上升数,而10,20170312则不是;

给定整数N,求最小的k使得N能被表示为k个上升数之和。

$1\leq N\leq 10^{500000}$

题解:

一个结论:每个上升数必定能被分解为九个全一数的和;

所谓“全一数”就是指1,1111,11111111这种每一位数都为1的数(包括0),证明显然。

设N可以被分解成K个全一数之和,显然答案$k=\lceil\frac{K}{9}\rceil$;

由于全一数不好处理,我们可以把一个长度为$l$的全一数变成$\frac{(10^{l+1}-1)}{9}$,那么有:

$N=\sum\limits_{i=1}^{K}\frac{(10^x)}{9}$(此处$x$代表不确定的位数)

$9N=\sum\limits_{i=1}^{K}(10^x-1)$

$9N+K=\sum\limits_{i=1}^{K}10^x$

这个式子是什么意思呢?如果不考虑进位,右边每一项都会使数位和+1,那么总体就说明$9N+K$的数位和等于$K$的数位和,此时$K$一定是9的倍数;

如果考虑进位,那么每进一位数位和就会减少9,因此$K$仍然要是9的倍数。

由于答案最多不会超过N的位数,枚举k,写个高精度乱做就行了。。。注意加法的时候没有进位就要break,这样是均摊$O(1)$的,否则是$O(n^2)$的。

代码:

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdio>
 5 #include<cmath>
 6 #include<queue>
 7 #define inf 2147483647
 8 #define eps 1e-9
 9 using namespace std;
10 typedef long long ll;
11 int n,tot,a[800001];
12 char s[500001];
13 void mul(int a[],int &n,int k){
14     int p=0;
15     tot=0;
16     for(int i=1;i<=n;i++){
17         p=a[i]*k+p;
18         a[i]=p%10;
19         tot+=a[i];
20         p/=10;
21     }
22     if(p)a[++n]=p;
23     tot+=p;
24 }
25 void add(int a[],int &n,int k){
26     int p=0;
27     tot-=a[1];
28     a[1]+=k;
29     p=a[1]/10;
30     a[1]%=10;
31     tot+=a[1];
32     for(int i=2;i<=n;i++){
33         tot-=a[i];
34         a[i]+=p;
35         p=a[i]/10;
36         a[i]%=10;
37         tot+=a[i];
38         if(!p)break;
39     }
40     if(p)a[++n]=p;
41     tot+=p;
42 }
43 int main(){
44     scanf("%s",s);
45     n=strlen(s);
46     for(int i=1;i<=n;i++){
47         a[i]=s[n-i]-‘0‘;
48     }
49     mul(a,n,9);
50     for(int i=1;i<=n;i++){
51         add(a,n,9);
52         if(tot%9==0&&i*9>=tot)return printf("%d",i),0;
53     }
54     return 0;
55 }

原文地址:https://www.cnblogs.com/dcdcbigbig/p/9724243.html

时间: 2024-08-02 11:23:53

[agc011e]increasing numbers的相关文章

[AGC011E] Increasing Numbers [数学]

题面 传送门 思路 首先,我们观察一下上升数的性质 可以发现,它一定可以表示为最多9个全是1的数字的和 那么我们设$N$可以被表示成$k$个上升数的和,同时我们设$p_i=\underbrace{111\cdots 11}_{i}$ 我们令$a_{i,j}$表示构成$N$的第$I$个上升数的第$j$个全1数的位数 那么可以写出这样的式子 $N=\sum_{i=1}^k\sum_{j=1}^9 p_{a[i][j]}$ 我们发现,$p_{i,j}$这样子摆在这里非常不好操作,那么我们继续观察$p_

AGC011-E Increasing Numbers

题意 给定一个数\(n\),\(n≤10^{500,000}\),问\(n\)最少可以拆分成几个不降数的和.一个不降数是在十进制位下,从高位往低位看,每个数都不会比高位的数更小的数 做法 不降数可以拆成若干个形似\(1111...111\)的数相加 位数为\(l\)的全\(1\)数可以写成\(\dfrac{10^{l+1}-1}{9}\) \(N=\sum\limits_{i=1}^k \dfrac{10^{a_i}-1}{9}\) 通过手玩可以进一步发现充分条件:\(9|k\) 写成\(N+9

【AtCoder】AGC011 E - Increasing Numbers

题解 题是真的好,我是真的不会做 智商本还是要多开啊QwQ 我们发现一个非下降的数字一定可以用不超过九个1111111111...1111表示 那么我们可以得到这样的一个式子,假如我们用了k个数,那么最多的话可以是这样的 \(N = \sum_{i = 1}^{9k} (10^{r_i} - 1) / 9\) \(9N + 9k = \sum_{i = 1}^{9k} 10^{r_{i}}\) 我们只要每次计算出9N + 9 ,9N + 18...,然后看看十进制下每一位的数字和有没有超过9k,

[AGC 011 E]Increasing Numbers

题意 给出一个数N,要求分成最少数量的"上升数",就是各个数位从高位到低位单调不降的数的和,求最少能分成多少数. \(1\leq N\leq10^{500000}\) 分析 考虑一个所谓的"上升数",一定可以表示为不超过9个形如\(1,11,111,\cdots\)的数之和(数位最多上升9次),那么假设这个\(N\)可以分成不超过\(k\)个"上升数"之和,那么这其实也相当于分成不超过\(9k\)个这样由1组成的数之和.也就是\(N=\sum\l

第六周周赛——AK机会不易得,好好把握题解(出自HDU5650,codeforces 616A,624A,659A,655A,658A)

A题: A题题目链接 题目描写叙述: 位运算 TimeLimit:1000MS  MemoryLimit:65536KB 64-bit integer IO format:%I64d Problem Description 已知一个包括 n 个元素的正整数集合S.设 f(S) 为集合S中全部元素的异或(XOR)的结果. 如:S={1,2,3}, 则 f(S) = 0. 给出集合S,你须要计算 将全部f(s)进行异或后的值, 这里 s?S. Input 多组測试数据.第一行包括一个整数T(T≤20

Speculative store buffer

A speculative?store?buffer is speculatively updated in response to speculative store?memory operations buffered by a?load/store?unit in a microprocessor. Instead of performing dependency checking for?load?memory operations among the?store?memory oper

Codeforces Round #335 (Div. 2) C. Sorting Railway Cars 连续LIS

C. Sorting Railway Cars An infinitely long railway has a train consisting of n cars, numbered from 1 to n (the numbers of all the cars are distinct) and positioned in arbitrary order. David Blaine wants to sort the railway cars in the order of increa

HDU 3153 Pencils from the 19th Century(数学)

主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3153 Problem Description Before "automaton" was a theoretic computer science concept, it meant "mechanical figure or contrivance constructed to act as if by its own motive power; robot." E

HDU 3153 Pencils from the 19th Century(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3153 Problem Description Before "automaton" was a theoretic computer science concept, it meant "mechanical figure or contrivance constructed to act as if by its own motive power; robot." E