A - Digit Sum of 2x
300分的题卡了好久。真弱啊
题目
For a positive integer x, let f(x) be the sum of its digit. For example, f(144)=1+4+4=9 and f(1)=1.
You are given a positive integer N. Find the following positive integers M and x:
The maximum positive integer M for which there exists a positive integer x such that f(x)=N and f(2x)=M.
The minimum positive integer x such that f(x)=N and f(2x)=M for the M above.
1≤N≤1e5
题意
定义f(x) 等于x各位上的数字总和。求:
M: 在f(x) = N的条件下,使得f(2x) = M的值最大。
x:在f(x) = N 和 f(2x) = M的条件下,x最小是多少。
思路
x和M都是位置的,首先考虑求最大的M,我们假定x个位上全是1,当x乘2的时候,各个位置上就全是2,所以M的值就是2*N。
M是各位数的和取最大,那么所以只要出现进位就会折损一定的贡献。保证不进位就行。即x各个位上值要<=4,才能保证2x各个位置不会进位。
代码
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> v;
int main()
{
cin >> n;
cout << 2 * n << endl;
while (n > 0)
{
v.push_back(min(4, n));
n -= 4;
}
reverse(v.begin(), v.end());
for(int it : v) cout << it;
return 0;
}