[ABC427B]B - Sum of Digits Sequence题解
Time Limit: 2 sec / Memory Limit: 1024 MiB
Score : 200 points
Problem Statement
For a positive integer x, define f(x) as the sum of the digits in the decimal representation of x. For example, f(123)=1+2+3=6.
Define an infinite sequence A=(A0,A1,A2,…) by the following formula:
- A0=1
- For i≥1, Ai=j=0∑i−1f(Aj)
You are given a positive integer N. Find the value of AN.
有道 翻译
问题陈述
对于正整数 x ,定义 f(x) 为 x 的十进制表示的数字之和。例如, f(123)=1+2+3=6 。
用下面的公式定义无穷数列 A=(A0,A1,A2,…) :
—— A0=1
—对于 i≥1 , Ai=j=0∑i−1f(Aj)
你得到一个正整数 N 。求 AN 的值。
Constraints
- N is an integer between 1 and 100, inclusive.
有道 翻译
# #约束
— N 是一个介于 1 和 100 之间的整数。
Input
The input is given from Standard Input in the following format:
N
有道 翻译
# #输入
输入来自标准输入,格式如下:
N
Output
Print the answer.
有道 翻译
# #输出
打印答案。
Sample Input 1
Copy6
Sample Output 1
Copy23
- A0=1
- A1=f(A0)=1
- A2=f(A0)+f(A1)=2
- A3=f(A0)+f(A1)+f(A2)=4
- A4=f(A0)+f(A1)+f(A2)+f(A3)=8
- A5=f(A0)+f(A1)+f(A2)+f(A3)+f(A4)=16
- A6=f(A0)+f(A1)+f(A2)+f(A3)+f(A4)+f(A5)=23
Thus, A6=23.
有道 翻译
###输出示例
23
—— A0=1
—— A1=f(A0)=1
—— A2=f(A0)+f(A1)=2
—— A3=f(A0)+f(A1)+f(A2)=4
—— A4=f(A0)+f(A1)+f(A2)+f(A3)=8
—— A5=f(A0)+f(A1)+f(A2)+f(A3)+f(A4)=16
—— A6=f(A0)+f(A1)+f(A2)+f(A3)+f(A4)+f(A5)=23
因此, A6=23 。
Sample Input 2
Copy45
Sample Output 2
Copy427
有道 翻译
###示例输出
427
思路
暴力。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long n,f[105];
long long abc(long long a1){
long long op=0;
while(a1!=0){
op+=a1%10;
a1/=10;
}
return op;
}
int main(){
cin>>n;
f[0]=1;
for(int i=1;i<=n;i++){
for(int j=0;j<=i-1;j++){
f[i]+=abc(f[j]);
}
}
cout<<f[n]<<endl;
return 0;
}

浙公网安备 33010602011771号