CF102B Sum of Digits 题解
Content
给定一个数 \(n\),每次操作可以将 \(n\) 变成 \(n\) 各位数之和。问你几次操作之后可以将 \(n\) 变为一位数。
数据范围:\(1\leqslant n\leqslant 10^{10^5}\)。
Solution
一看这么大个数字我们就不能够用 int
,long 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 题解的更多相关文章
- 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 ...
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- 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 ...
- 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 ...
- 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 ...
- Hdu3022 Sum of Digits
Sum of Digits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- .NET 开源免费图表组件库,Winform,WPF 通用
大家好, 我是等天黑, 今天给大家介绍一个功能完善, 性能强悍的图表组件库 ScottPlot, 当我第一次在 github 上看到这个库, 我看不懂,但我大受震撼, 这么好的项目当然要分享出来了. ...
- 智能 Request 推荐,K8s 资源利用率提升 252%
作者 王孝威,FinOps 认证从业者,腾讯云容器服务产品经理,热衷于为客户提供高效的 Kubernetes 使用方式,为客户极致降本增效服务. 余宇飞,FinOps 认证从业者,腾讯云专家工程师,从 ...
- nginx安装与配置1-nginx安装
反向代理: 客户端不需要配置就可以访问,将请求发送到反向代理服务器, 由反向代理服务器选择目标服务器获取数据,再返回客户端,对外暴露代理服务器地址,隐藏真实ip 负载均衡: 客户端请求nginx等服务 ...
- ABC 210
A 按题意模拟. scanf("%lld%lld%lld%lld",&n,&a,&x,&y); std::cout<<n * x - ( ...
- Codeforces 772D - Varying Kibibits(高维差分+二项式定理维护 k 次方和)
Codeforces 题目传送门 & 洛谷题目传送门 首先很容易注意到一件事,那就是对于所有 \(f(S)\) 可能成为 \(x\) 的集合 \(S\),必定有 \(\forall y\in ...
- 使用FastqCount统计fastq文件基本信息?
目录 1. FastqCount简介 2. 使用 3. 结果 1. FastqCount简介 快速实用小工具:FastqCount https://github.com/zhimenggan/Fast ...
- Linux文件系统属性和权限概念详解(包含inode、block、文件权限、文件软硬链接等)
Linux中的文件属性 ls -lih 包括:索引节点(inode),文件类型,权限属性,硬链接数,所归属的用户和用户组,文件大小,最近修改时间,文件名等等 索引节点:相当于身份证号,系统唯一,系统读 ...
- vector去重--unique
具体实现见中间源码 function template <algorithm> std::unique equality (1) template <class ForwardIte ...
- kubernetes部署kube-scheduler服务
同样的分非认证授权和认证授权: 非认证授权: cat > /lib/systemd/system/kube-scheduler.service <<EOF [Unit] Descri ...
- 日常Java 2021/9/27
题目: 在某个比赛中,有6个评委为参赛的选手打分,分数为1-100的随机整数.选手的最后得分为:除去最高分和最低分后的4个评委分值的平均值(不考虑小数部分). package m; import ja ...