Find The Multiple (DFS递归)
题意:输入一个不超过200的数 n,然后求得一个数字k,数字满足:能被n整除,每一位只有0,1。这样的数字k会有很多个,然以输出一个就可以。
注意unsigned __int64的范围,-(10^19)~(10^20)所以步数不能超过19次。 (摘)
附:同余模定理:
(a*b)%n = (a%n *b%n)%n;
(a+b)%n = (a%n +b%n)%n;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std; int n,flag; void DFS(int step,long long num)
{
if(step== || flag) return; //64位 最多加19步
if(num%n==)
{
cout<<num<<endl;
flag=; //如果得到一个数了,就可以全退出
return;
}
DFS(step+,num*);
DFS(step+,num*+);
} int main()
{
ios::sync_with_stdio(false);
while(cin>>n && n!=)
{
flag=;
DFS(,);
}
}
Find The Multiple (DFS递归)的更多相关文章
- POJ 1321-棋盘问题(DFS 递归)
POJ 1321-棋盘问题 K - DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- 数据结构作业——图的存储及遍历(邻接矩阵、邻接表+DFS递归、非递归+BFS)
邻接矩阵存图 /* * @Author: WZY * @School: HPU * @Date: 2018-11-02 18:35:27 * @Last Modified by: WZY * @Las ...
- Timus 1329. Galactic History。LCA最近公共祖先或dfs递归离线处理!
1329. Galactic History 比赛的时候看到学弟A了这题然后跟榜做,结果在LCA的道路上一去不复返,这个题是很像LCA求最近公共祖先的,不过三个人都没学过LCA,只能拿着资料看着像然后 ...
- [LeetCode] Combinations (bfs bad、dfs 递归 accept)
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 图的DFS递归和非递归
看以前写的文章: 图的BFS:http://www.cnblogs.com/youxin/p/3284016.html DFS:http://www.cnblogs.com/youxin/archiv ...
- 深度优先搜索(DFS)递归形式改为非递归形式
DFS将递归改为非递归这个方法的需求来自于一道三维积木组合的题目,还在苦苦调试中,暂且不提. 普通的认识对于递归向非递归的转化无非是使用栈,但是结合到深度搜索如何将栈很好利用,如何很好保存现场,都不是 ...
- 八皇后问题 dfs/递归
#include <bits/stdc++.h> using namespace std; const int maxn = 55; int ans=0; int vis_Q[maxn]; ...
- (N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- (二叉树 DFS 递归) leetcode 112. Path Sum
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- 关于css清除元素浮动的方法总结(overflow clear floatfix)
在前两天的一个面试中考官问我web中清除浮动的一些css常用方法,我很轻松的答出了: 1.overflow:hidden 2.clear:both 3.floatfix类 然后问题就来了,考官接着问' ...
- ffmpeg.编译(20191129)
1.一步步实现windows版ijkplayer系列文章之一——Windows10平台编译ffmpeg 4.0.2,生成ffplay - HarlanC - 博客园.html(https://www. ...
- socket编程(二)
TCP下粘包问题 两种情况下会发生粘包. 1.发送端需要等缓冲区满才发送出去,造成粘包(发送数据时间间隔很短,数据了很小,会合到一起,产生粘包) 发送方:AB #其实放在缓存里没发送 发送方:B #其 ...
- Nginx启动和停止
启动nginx [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx ...
- 035 Android Volley框架进行网络请求
1.volley入门介绍 开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行HTTP通 ...
- 033 Android App启动的闪屏效果+新手向导(多个图片滑动效果)+ViewPager使用
1.目标效果 App启动时,出现闪屏效果(利用动画实现). App新手使用时,会出现新手向导效果. 2.XML页面布局 (1)闪屏页面 <?xml version="1.0" ...
- 汉字转拼音js工具:
/ JavaScript Document var PinYin = { "a": "\u554a\u963f\u9515", "ai": ...
- Spyder中报错: Check failed: PyBfloat16_Type.tp_base != nullptr
报错问题: 问题1:tensorflow/python/lib/core/bfloat16.cc:675] Check failed: PyBfloat16_Type.tp_base != nullp ...
- Spring Boot使用@ConfigurationProperties注解获取配置文件中的属性值
注意:这种方式要提供属性的getter/setter方法—— 如果idea报错,提示没有相应的执行器,就需要在maven中添加: (虽然不配置代码也能正常运行,作用在下面会说明) 配置了该执行器后,在 ...
- Selenium 调用IEDriverServer打开IE浏览器
Selenium 调用IEDriverServer打开IE浏览器 2016年03月30日 09:49:37 标签: selenium 14836 Selenium 调用IEDriverServer打开 ...