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 ...
随机推荐
- 正则表达式入门教程&&经典Javascript正则表达式
前言 例子: ^.+@.+\\..+$ 这样的代码曾经多次把我自己给吓退过.可能很多人也是被这样的代码给吓跑的吧.继续阅读本文将让你也可以自由应用这样的代码. 正文 教程:正则表达式30分钟入门教程 ...
- Nginx虚拟目录(alias)和根目录(root)
功能要求: 假设nginx配置的域名是www.kazihuo.com,现有静态资源/home/www/oye目录需要通过nginx访问. 功能实现: 前提要求: 1.在nginx.conf中到处第二行 ...
- eclipse英语单词1
short cut bar 捷径,快捷方法 menu bar 菜单栏 tool bar 工具栏 workbench window 工作台窗口 perspective 透视 editor 编辑器 con ...
- 使用Maven为SpringBoot项目打包
一.maven通过命令行打jar包 进入项目目录,执行如下命令: mvn -Dmaven.test.skip -U clean package 发现报如下错误: [ERROR] Failed to e ...
- 洛谷 题解 UVA247 【电话圈 Calling Circles】
[题意] 如果两个人互相打电话(直接或者间接),则说他们在同一个电话圈里.例如,\(a\)打给\(b\),\(b\)打给\(c\),\(c\)打给\(d\),\(d\)打给\(a\),则这四个人在同一 ...
- php mqtt client
<?php /* phpMQTT */ class phpMQTT { private $socket; /* holds the socket */ private $msgid = 1; / ...
- Asp.Net Core中创建多DbContext并迁移到数据库
在我们的项目中我们有时候需要在我们的项目中创建DbContext,而且这些DbContext之间有明显的界限,比如系统中两个DbContext一个是和整个数据库的权限相关的内容而另外一个DbConte ...
- WUSTOJ 1305: 最短路(Java)
题目链接:
- S04_CH03_QSPI烧写LINUX系统
S04_CH03_QSPI烧写LINUX系统 3.1概述 3.2搭建硬件系统 本章硬件工程还是使用<S04_CH01_搭建工程移植LINUX/测试EMMC/VGA>所搭建的VIVADO工程 ...
- 解决Html页面缓存
对于一个html页面,缓存分3部分,一个是页面内容,一个是css样式,一个是JS文件 CSS和JS文件缓存 <link rel="stylesheet" type=" ...