题意就是找出一个长度为k的整数,使得它的root为d,k的可能取值为1-1000。

第一眼看到这个题,无从下手,想到那么长的数,暴力肯定超时。其实不然,题目要求只要输出任何一个满足条件的即可,因为任何数的root都是0-9,所以这样的数还是很多的,算一下枚举次数的期望,大概就是5,不知道算的对不对。

//cf 355A
//2013-10-15-10.48
#include <stdio.h>
#include <string.h> int num[1005];
int k, d;
int tot; int getroot(int x)
{
if (x < 10)
return x;
int sum = 0;
while (x)
{
sum += (x%10);
x /= 10;
}
return getroot(sum);
} void print()
{
for (int i = 1; i <= k; i++)
printf("%d", num[i]);
puts("");
} void add(int deep)
{ if (getroot(tot) == d)
{
print();
return ;
}
else
{
if (num[deep] < 9)
{
tot += 1;
num[deep] += 1;
add(deep);
}
else
add(deep+1);
}
} int main()
{
while (scanf("%d %d", &k, &d) != EOF)
{
if (k == 1)
{
printf("%d\n", d);
continue;
}
if (d == 0 && k > 1)
{
puts("No solution");
continue;
}
for (int i = 1; i <= k; i++)
num[i] = 1;
tot = k;
add(1);
}
return 0;
}

codeforces 355A Vasya and Digital Root的更多相关文章

  1. codeforces A. Vasya and Digital Root 解题报告

    题目链接:http://codeforces.com/problemset/problem/355/A 题目意思:找出某个经过最多四次dr(n)操作等于d的k位数.   千万不要想得太复杂,想得越简单 ...

  2. 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...

  3. Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    #include <iostream> using namespace std; int main(){ int k,d; cin >> k >>d; ) { k ...

  4. codeforces Vasya and Digital Root

    /* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ /** * 当时比赛时,想得复杂了,也想偏了, * 1).写出来之后,结果达到了预期 ...

  5. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  6. codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...

  7. Digital root(数根)

    关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...

  8. 数字根(digital root)

    来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digi ...

  9. 【HDOJ】4351 Digital root

    digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...

随机推荐

  1. yii框架widget和注册asset的例子

    yii框架是一个基于组件的框架,这样代码的重用性就非常的高,如我们想在网站的多个地方调用编辑器,这样我们就可以自定义一个组件,来供我们调用使用 下面以Ueditor组件为例: 1.下载ueditor到 ...

  2. Python操作ElasticSearch

    Python批量向ElasticSearch插入数据 Python 2的多进程不能序列化类方法, 所以改为函数的形式. 直接上代码: #!/usr/bin/python # -*- coding:ut ...

  3. C/C++应用程序内存泄漏检查统计方案

    一.前绪 C/C++程序给某些程序员的几大印象之一就是内存自己管理容易泄漏容易崩,笔者曾经在一个产品中使用C语言开发维护部分模块,只要产品有内存泄漏和崩溃的问题,就被甩锅“我的程序是C#开发的内存都是 ...

  4. Oracle Awr报告_生成

    AWR的概念 Oracle数据库是一个使用量很多的数据库,关于Oracle数据库的性能.Oracle10g以后,Oracle提供了一个性能检测的工具:AWR(Automatic Workload Re ...

  5. AD域控制器安装使用

    AD域控制器安装使用 一. 在服务器上安装域控制器 二. 将此服务器提升为域控制器 三. 将主机加入到我们创建的域中 在AD域控制器上查看加入的主机

  6. flume1.9 用户指南(中文版)

    概述 Apache Flume是一个分布式,可靠且可用的系统,用于有效地从许多不同的source收集,聚合和移动大量日志数据到集中式数据存储. Apache Flume的使用不仅限于日志数据聚合.由于 ...

  7. 记2017沈阳ICPC

    2017沈阳ICPC 10月20日 早上十点抵达沈阳,趁着老师还没到,跑去故宫游玩了一下,玩到一点多回到宾馆,顺便吃了群里大佬说很好吃的喜家德虾饺(真的好好吃),回到宾馆后身体有点不舒服了,头晕晕的, ...

  8. leetcode笔记 动态规划在字符串匹配中的应用

    目录 leetcode笔记 动态规划在字符串匹配中的应用 0 参考文献 1. [10. Regular Expression Matching] 1.1 题目 1.2 思路 && 解题 ...

  9. python的简介与简单使用

    简介: python的诞生 创始人:Guido van Rossum(荷兰人:吉多) 时间:1989年 特点: 1.简单明了,容易上手 2.多平台,`全面发展,平衡发展, 3.胶水语言,有大量的库 其 ...

  10. 走近Java之包装器类Integer

    前几天,有个同事问了我一个关于Integer类赋值的问题,很有意思,我们一起来看一下(如果有说的不正确的地方,欢迎大家指正). 如上图,同样是赋值,但是两次比较的结果完全不同.我们走近了解一下. 在I ...