codeforces 355A Vasya and Digital Root
题意就是找出一个长度为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的更多相关文章
- codeforces A. Vasya and Digital Root 解题报告
题目链接:http://codeforces.com/problemset/problem/355/A 题目意思:找出某个经过最多四次dr(n)操作等于d的k位数. 千万不要想得太复杂,想得越简单 ...
- 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...
- Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
#include <iostream> using namespace std; int main(){ int k,d; cin >> k >>d; ) { k ...
- codeforces Vasya and Digital Root
/* * c.cpp * * Created on: 2013-10-7 * Author: wangzhu */ /** * 当时比赛时,想得复杂了,也想偏了, * 1).写出来之后,结果达到了预期 ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- codeforces 10C Digital Root(非原创)
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...
- Digital root(数根)
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- 【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+..+ ...
随机推荐
- 中转Webshell 绕过安全狗(二)
前言 在实践中转webshell绕过安全狗(一)中,在服务端和客户端均为php.某大佬提示并分享资源后,打算使用python完成中转.部分代码无耻copy. 客户端 本地127.0.0.1,安装pyt ...
- mac下mysql的卸载和安装
1. mysql的卸载 1 sudo rm /usr/local/mysql 2 sudo rm -rf /usr/local/mysql* 3 sudo rm -rf /Library/Startu ...
- IT需求过程管理
IT部门就是为其他业务.内勤部门提供信息化手段的,所以在实施信息化系统的时候首先要做的就是需求调研,但是针对于绝大多数业务部门的人员而言,信息系统是很不熟悉的,我们会经常听到这样的回复“这个应该很快就 ...
- 从零开始一起学习SALM-ICP原理及应用
点"计算机视觉life"关注,星标更快接收干货! ## 小白:师兄,最近忙什么呢,都见不到你人影,我们的课也好久没更新了呢 师兄:抱歉,抱歉,最近忙于俗事.我后面一起补上,学习劲头 ...
- Java基础介绍运行机制笔记
1. 基础知识点图解 编程语言核心结构:变量.基本语法.分支.循环.数组.…… Java面向对象的核心逻辑:OOP.封装.继承.多态.接口…… 开发Java SE高级应用程序:异常.集合.I/O.多线 ...
- 查询IP地址的免费API
1.百度 1.http://sp0.baidu.com/8aQDcjqpAAV3otqbppnN2DJv/api.php?query=192.168.0.0&co=&resource_ ...
- java获取config下文件
private static final String keystore="keystore.jks"; InputStream is=Thread.currentThread() ...
- 页面的隐藏或显示:hidden与visibilityState
我们在很多地方都需要判断用户是不是在当前页面,如果离开了当前页面我们需要捕捉到并进行一些操作. 例如:当视频处于播放状态时,我们需要判断用户是不是在当前页面以继续播放,如果离开了我们需要暂停播放. 有 ...
- 3. Django每日一码 之 Serializers 源码
2019-7-6 今日源码:rest-framework 序列化Serializers 序列化组件Serializers 源码分析 首先,它需要 data .many . instance,其中 in ...
- JDK1.8--体验Stream表达式,从一个对象集合中获取每一个对象的某一个值返回新集合
xl_echo编辑整理,欢迎转载,转载请声明文章来源.更多IT.编程案例.资料请联系QQ:1280023003 百战不败,依不自称常胜,百败不颓,依能奋力前行.——这才是真正的堪称强大!! --- 开 ...