CF355A Vasya and Digital Root 的题解

CF355A Vasya and Digital Root 的题解

洛谷传送门
CF传送门

题目大意

现有一个函数 S ( n ) S(n) S(n) 表示 n n n 各个数位之和。

又有 d r ( n ) dr(n) dr(n) 满足:

  • S ( n ) < 10 → d r ( n ) = S ( n ) S(n)<10 \rightarrow dr(n)=S(n) S(n)<10dr(n)=S(n)

  • S ( n ) ≥ 10 → d r ( n ) = d r ( S ( n ) ) S(n)\ge 10\rightarrow dr(n)=dr(S(n)) S(n)10dr(n)=dr(S(n))

给定 k k k d d d,求一个 k k k 位数 n n n 满足:

d r ( n ) = d dr(n)=d dr(n)=d

若没有数满足条件,请输出 No solution

思路

一题简单的构造题,关于构造题可以看看这篇文章:link

如果 k > 1 k > 1 k>1 d = 0 d = 0 d=0,那么就无解,直接输出 No solution

否则输出 d d d,再输出 k − 1 k - 1 k1 0 0 0

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <climits>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <ctime>
#include <string>
#include <cstring>
#define lowbit(x) x & (-x)
#define endl "\n"
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
namespace fastIO {
	inline int read() {
		register int x = 0, f = 1;
		register char c = getchar();
		while (c < '0' || c > '9') {
			if(c == '-') f = -1;
			c = getchar();
		}
		while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
		return x * f;
	}
	inline void write(int x) {
		if(x < 0) putchar('-'), x = -x;
		if(x > 9) write(x / 10);
		putchar(x % 10 + '0');
		return;
	}
}
using namespace fastIO;
int main() {
	//freopen("Vasya and Digital Root.in","r",stdin);
    //freopen("Vasya and Digital Root.out","w",stdout);
	ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int k, d;
    cin >> k >> d;
    if(d == 0 && k > 1) { // d == 0 也可写成 !d
        cout << "No solution"; // 无解情况
    }
    else{
        cout << d;
        k --;
        while(k --) {
            cout << 0;
        }
    }
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值