Content

给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和。问你几次操作之后可以将 \(n\) 变为一位数。

数据范围:\(1\leqslant n\leqslant 10^{10^5}\)。

Solution

一看这么大个数字我们就不能够用 intlong long 之类的类型读入了,只能够用字符串、字符数组。然后考虑将所有的数位暴力拆开求和,然后再代入求出操作后的数字,直到变成一位数为止。

Code

请注意下面的代码需要特判是否本来就是一位数。

const int N = 1e5 + 7;
int digit[N], tmp[N], tmp2[N], sum, ans; inline int td(char x) {return x - '0';} int main() {
crstr(s, n);
if(n == 1) return printf("0"), 0;
F(i, 0, n - 1) digit[i + 1] = td(s[i]);
while(1) {
memset(tmp, 0, sizeof(tmp));
memset(tmp2, 0, sizeof(tmp2));
sum = 0; //Clear all!!!
F(i, 1, n) sum += digit[i];
while(sum) {
tmp[++tmp[0]] = sum % 10;
sum /= 10;
}
R(i, tmp[0], 1) tmp2[i] = tmp[tmp[0] - i + 1];
memset(digit, 0, sizeof(digit));
F(i, 1, tmp[0]) digit[i] = tmp2[i];
n = tmp[0], ans++;
if(n == 1) break;
}
return write(ans), 0;
}

CF102B Sum of Digits 题解的更多相关文章

  1. CodeForces 489C Given Length and Sum of Digits... (贪心)

    Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...

  2. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  3. Maximum Sum of Digits(CodeForces 1060B)

    Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...

  4. Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  5. CodeForces 1060 B Maximum Sum of Digits

    Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...

  6. Hdu3022 Sum of Digits

    Sum of Digits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  7. codeforces#277.5 C. Given Length and Sum of Digits

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  8. cf#513 B. Maximum Sum of Digits

    B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

  9. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

随机推荐

  1. gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)

    前言   Excel功能强大,应用广泛.随着web应用的兴起和完善,用户的要求也越来越高.很多Excel的功能都搬到了sass里面.恨不得给他们做个Excel出来...程序员太难了... 去年我遇到了 ...

  2. 学习 NPM 最基础的指令

    什么是 NPM npm的核心是一个软件注册表(software registry). registry /ˈredʒɪstri/ n. 注册表:登记处:挂号处.注册表就像是信息登记表或者数据库. np ...

  3. 洛谷 P4135 作诗(分块)

    题目链接 题意:\(n\) 个数,每个数都在 \([1,c]\) 中,\(m\) 次询问,每次问在 \([l,r]\) 中有多少个数出现偶数次.强制在线. \(1 \leq n,m,c \leq 10 ...

  4. [CCC​2019] Tourism题解

    我们先考虑一下拿部分分: subtask1 考虑因为 \(n < 2k\) ,那么我们的划分一定是从中间某个地方裁开,且满足 \(k\) 的条件的,我们发现当划分点在 \([n\ mod\ k, ...

  5. 洛谷 P3287 - [SCOI2014]方伯伯的玉米田(BIT 优化 DP)

    洛谷题面传送门 怎么题解区全是 2log 的做法/jk,这里提供一种 1log 并且代码更短(bushi)的做法. 首先考虑对于一个序列 \(a\) 怎样计算将其变成单调不降的最小代价.对于这类涉及区 ...

  6. UOJ #129 / BZOJ 4197 / 洛谷 P2150 - [NOI2015]寿司晚宴 (状压dp+数论+容斥)

    题面传送门 题意: 你有一个集合 \(S={2,3,\dots,n}\) 你要选择两个集合 \(A\) 和 \(B\),满足: \(A \subseteq S\),\(B \subseteq S\), ...

  7. Redis队列跟MQ的区别

    Redis队列:Redis队列是一个Key-Value的NoSQL数据库,开发维护很活跃,虽然是一个Key-Value数据库存储系统,但它本身支持MQ功能,所以完全可以当做一个轻量级的队列服务来使用 ...

  8. python-django使用ORM模型增删改查CRUD

    from weibo.models import WeiboUser as User user_obj = User.objects.get(pk=1) user_obj.pk Out[4]: 1 u ...

  9. Spark3学习【基于Java】3. Spark-Sql常用API

    学习一门开源技术一般有两种入门方法,一种是去看官网文档,比如Getting Started - Spark 3.2.0 Documentation (apache.org),另一种是去看官网的例子,也 ...

  10. 前端2 — CSS — 更新完毕

    1.CSS是什么? 指:Cascading Style Sheet  --- 层叠样式表 CSS 即:美化网页( 在HTML不是说过W3C规定网页为三种标准嘛,结构层HTML已经玩了,而这个CSS就是 ...