题意就是找出一个长度为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. scikit-learn杂记

    1.数据预处理 二值化 import numpy as np from sklearn import preprocessing X = np.array([[1., -1., 2.], [2., 0 ...

  2. Codeforces Round #568 (Div. 2)A

    A. Ropewalkers 题目链接:http://codeforces.com/contest/1185/problem/A 题目: Polycarp decided to relax on hi ...

  3. Axure中怎么制作锚点

    实现目标 点击导航条不同的菜单,页面跳到菜单相应的位置,实现页面自动滚动. 1.  拖入编辑区3个矩形,并相应命名为菜单1,菜单2 ,菜单3: 2.  再新建3个想要制作的文本框准备进行锚点滚动: 3 ...

  4. PATA 1036. Boys vs Girls (25)

    https://www.patest.cn/contests/pat-a-practise/1036 #include <bits/stdc++.h> using namespace st ...

  5. zabbix自定义监控项数据类型错误

    问题描述 监控cpu使用率,脚本获取的值是浮点型  zabbix创建监控项时没有选数据类型,导致监控数据有问题. 查看 zabbix-server 日志: ::203016.768 error rea ...

  6. DFS(二):骑士游历问题

    在国际象棋的棋盘(8行×8列)上放置一个马,按照“马走日字”的规则,马要遍历棋盘,即到达棋盘上的每一格,并且每格只到达一次.例如,下图给出了骑士从坐标(1,5)出发,游历棋盘的一种可能情况. [例1] ...

  7. python 查询 elasticsearch 常用方法(Query DSL)

    1. 建立连接 from elasticsearch import Elasticsearch es = Elasticsearch(["localhost:9200"]) 2. ...

  8. Appium+python自动化(十七)- 你难道猴哥失散多年的混血弟弟 - Monkey简介之开山篇(超详解)

    简介 今天由宏哥给小伙伴们来介绍猴哥的混血弟弟=Monkey.Monkey 是Android SDK提供的一个命令行工具, 可以简单,方便地运行在任何版本的Android模拟器和实体设备上. Monk ...

  9. 图片懒加载,Selenium,PhantomJS

    引入 今日概要 图片懒加载 selenium phantomJs 谷歌无头浏览器 知识点回顾 验证码处理流程 今日详情 动态数据加载处理 一.图片懒加载 什么是图片懒加载? 案例分析:抓取站长素材ht ...

  10. scrapy基础知识之 pycharm 调试小技巧:

    在项目根目录下新建main.py文件,用于调试 from scrapy.cmdline import executeexecute(["scrapy","crawl&qu ...