您的位置:首页 > 其它

[Codeforces] 355A - Vasya and Digital Root

2014-08-06 08:10 645 查看
根据数字根的特性,数字A的数字根 = A%9 (当A为9的倍数的时候,数字根不是0而是9)。

已知题意,K位数字,构造数字根为d,那么我们可以先构造出K-1位的9,最后一位是d。

举个例子:

95的数字根 = 9 + 5 = 14 = 5

也就是说,任何数字+9 的数字根不变。

显然K-1位9的数字根+最后一位数字d 的数字根 = d。就可以通过构造出数字根是1---9的所有情况。

我们再考虑一下无解的情况,当输入2 0,是没解的,因为我们可以推断正数的数字根的范围是[1,9]。

只有0的数字根是0,题目要求不能有前导零,所以当d为0时,K>1无解。

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
using namespace std;

int main()
{
int k, d, ans;
while(~scanf("%d %d", &k, &d))
{
if(k > 1 && d == 0)
{
printf("No solution\n");
continue;
}
for(int i = 0; i < k - 1; i++)
{
printf("9");
}
printf("%d\n", d);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ACM codeforces