题意就是找出一个长度为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. ZooKeeper学习之路(一)—— ZooKeeper简介及核心概念

    一.Zookeeper简介 Zookeeper是一个开源的分布式协调服务,目前由Apache进行维护.Zookeeper可以用于实现分布式系统中常见的发布/订阅.负载均衡.命令服务.分布式协调/通知. ...

  2. python算法与数据结构-栈(43)

    一.栈的介绍 栈作为一种数据结构,是一种只能在一端进行插入和删除操作.它按照先进后出的原则存储数据,先进入的数据被压入栈底,最后的数据在栈顶,需要读数据的时候从栈顶开始弹出数据(最后一个数据被第一个读 ...

  3. iOS开发(5):设备唯一标识与全局变量

    这里记录两个iOS开发中经常用到的知识点,一个是唯一标识,一个是全局变量. (1)唯一标识 唯一标识一台设备(比如iPhone.iPad等)是一个基本的实现与业务上的需求,因为这个唯一标识在许多场景都 ...

  4. DFS和BFS的比较

    DFS(Depth First Search,深度优先搜索)和BFS(Breadth First Search,广度优先搜索)是两种典型的搜索算法.下面通过一个实例来比较一下深度优先搜索和广度优先搜索 ...

  5. kd树原理及实现

    常用来作空间划分及近邻搜索,是二叉空间划分树的一个特例.通常,对于维度为k,数据点数为N的数据集,kd树适用于N≫2的k次方的情形. 1维数据的查询 假设在数据库的表格T中存储了学生的语文成绩chin ...

  6. JS高级程序设计第2章--精简版

    前言:这次是二刷了,想暑假做一次完整的笔记,但用本子来写笔记的话太贵了,可能哪天还丢了..所以还是博客好== 第二章:在HTML中使用JavaScript 2.1 <script>元素: ...

  7. MacBook强制清除gardle缓存

    背景:在日常的工作开发中,为了方便维护一般采用gardle+Nexus的模式管理jar包,但方便的同时也会存在一些问题 例如:test-1.0.3.jar  jar包提供方修改了一些问题上传到Nexu ...

  8. jsp传值

    是由a1.jsp发出请求然后由a2.jsp转发给ok.jsp,由ok.jsp响应a1.jsp. 但是这个转发过程是在服务端发生的,客户端不知道所以地址是不变的 转发请求的代码: request.get ...

  9. 法国神器"mimikatz"简化版,一键导出结果

    神器之所以称之为神器.那是闹着玩的? 法国神器"mimikatz",那是相当的好使!!! GitHub:https://github.com/gentilkiwi/mimikatz ...

  10. Java实现异步调用

    一.创建线程 @Test public void test0() throws Exception { System.out.println("main函数开始执行"); Thre ...