UVA10624 - Super Number(dfs)

题目:UVA10624 - Super Number(dfs)

题目大意:给你n和m要求找出这样的m位数,从第n位到第m位都满足前i位是可以被i整除,如果没有这样的数,输出-1.有多个就输出字典序最小的那个。

解题思路:将每个位置都用0..9枚举一下,注意第一个字符不能是0,然后dfs判断每个位置是否都满足要求。注意这里是会爆long long的,所以要取模一下。本来以为这样的做法会超时的,结果竟然过了,估计是样例数少,而且找不到的情况也比较少。

代码:

#include <cstdio>
#include <cstring>

const int N = 35;
typedef unsigned long long ll;

int num[N];
int m, n;

bool judge (int cur) {

	ll sum = 0;
	for (int i = 0; i <= cur; i++) {
		sum = sum * 10 + num[i];
		if (i > 17)
			sum = sum % (cur + 1);
	}
	sum = sum % (cur + 1);
	return sum ? false : true;
}

bool dfs (int cur) {

	if (cur == m)
		return true;
	for (int i = 0; i < 10; i++) {

		if (!cur && !i)
			continue;

		num[cur] = i;
		if (cur < n - 1 || judge(cur)) {
			if (dfs (cur + 1))
				return true;
		}
	}
	return false;
}

int main () {

	int t;
	scanf ("%d", &t);
	for (int i = 1; i <= t; i++) {

		scanf ("%d%d", &n, &m);
		printf ("Case %d: ", i);
		if (!dfs(0))
			printf ("-1\n");
		else {
			for (int j = 0; j < m; j++)
				printf ("%d", num[j]);
			printf ("\n");
		}
	}
	return 0;
}
时间: 2024-08-07 11:00:14

UVA10624 - Super Number(dfs)的相关文章

10624 - Super Number

题目链接 题意:给出n到m的范围,求出一个数在前i位数组成的数字能被i整除,如果存在输出这个数,如果不存在,输出-1. 思路:回溯,每次放第i位,然后判断是否符合题意.这题踩着时间过去的2.6s(看了下别人的题解,可以减少取模次数来节省时间). 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const

湖南省第六届省赛题 Biggest Number (dfs+bfs,好题)

Biggest Number 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You have a maze with obstacles and non-zero digits in it: You can start from any square, walk in the maze, and finally stop at some square. Each step, you may only walk into one of the four neighb

Leetcode 17 Letter Combinations of a Phone Number - DFS思想

Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae", &q

ZOJ - 3816 Generalized Palindromic Number dfs

Generalized Palindromic Number Time Limit: 2 Seconds                                     Memory Limit: 65536 KB A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palind

ZOJ 3436 July Number(DFS)

题意   把一个数替换为这个数相邻数字差组成的数  知道这个数只剩一位数  若最后的一位数是7  则称原来的数为 July Number  给你一个区间  求这个区间中July Number的个数 从7开始DFS  位数多的数总能由位数小的数推出 #include <bits/stdc++.h> using namespace std; const int N = 1e6; int july[N], n; set<int> ans; int pw[] = {1, 10, 100,

ZOJ3816-Generalized Palindromic Number(DFS数位搜索)

Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number. We call a number 

UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的点的个数,若当前数字的长度加上个数仍小于目前最优答案的长度,则剪去:若长度相等,则将所有还能到达的数字按从大到小排序后连到当前数字上,如果还比目前最优解小,则减去.找出所有还能到达的点的过程用BFS实现. 1 #pragma comment(linker, "/STACK:1024000000,10

UVA - 10624 Super Number

题目大意: 给定两个数 n 和 m ,如果长度为 m 的数满足对于每个 i (n <= i <= m),数字的前 i 位都能被 i 整除,那么这个数就是超级数,求出字典序最小的符合要求的超级数. 解题思路:直接暴力就行,如果每次进行整除判断的时候,对当前数每位都进行取余运算,那么将会超时,因此每 18 位进行一次取余(long long的数据范围为:-9223372036854775808..9223372036854775807,这样在 1S 左右就可以AC了. #include <c

ZOJ 3816 Generalized Palindromic Number dfs+暴力枚举

题目链接:点击打开链接 题意: 给定一个数n 找一个最大的数u使得u<n && u为回文. 枚举前面有多少位是一样的.然后分类讨论.啪啦啪啦 #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <vector> using namespace std; typedef long long ll; cons