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)<10→dr(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)≥10→dr(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 k−1 个 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;
}