A - Digit Sum of 2x

本文解析了一道关于求解正整数x满足f(x)=N且f(2x)=M的最大M和对应的最小x的题目,通过理解数字和规则并运用优化策略,给出代码实现。关键在于控制进位避免损失,最大M为2N,x的最小值由位数限制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

善良的大铁牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值