Uva 10825 Anagram and Multiplication dfs 爆搜排列

题目链接:点击打开链接

题意:

给定m( 3<=m<=6), n ( 4<=n<=400)

构造一个m位n进制的数。

使得这个数*2, *3, *i ( 2<=i<=m)的结果是 这个数的排列组合。

如:

m = 6, n = 10;

则 这个数可能为 142857

2*142857 = 285714

3*142857 = 428571

4*142857 = 571428

5*142857 = 714285

6*142857 = 857142

输出:

题目保证至多只有一个解,若有这样的解则输出这个数(每位数间有一个空格)若无输出"Not found."

思路:

枚举个位,然后不断用 (2~m)去乘个位得到其他位,则这样得到的结果的某种排列才有可能是答案,

判断所有排列是否可行。

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.Queue;

public class Main {
	int m, n;
	boolean ok;
	int[] ans = new int[7], b = new int[500], a = new int[500];
	boolean check(int x, int[] y){
		for(int i = 0; i < n; i++)a[i] = b[i];
		int tmp = 0;
		for(int i = m; i >= 1; i--){
			tmp = tmp + x*y[i];
			int k = tmp %n;
			if(--a[k] < 0)return false;
			tmp /= n;
		}
		return tmp == 0;
	}
	boolean[] use = new boolean[7];
	int[] v = new int[7];
	boolean dfs(int pos){
		if(pos > m){
			for(int i = 2; i <= m; i++)
				if(false == check(i,v))return false;
			for(int i = 1; i <= m; i++)ans[i] = v[i];
			return true;
		}
		else {
			for(int i = 1; i <= m; i++){
				if(use[i])continue;
				use[i] = true;
				v[pos] = ans[i];
				if(true == dfs(pos+1))return true;
				use[i] = false;
			}
			return false;
		}
	}
	boolean judge(int x){
		int tmp = 0;
		for(int i = 0; i < n; i++)b[i] = 0;
		for(int i = 1; i <= m; i++){
			tmp = (x + tmp)%n;
			ans[i] = tmp;
			b[ans[i]]++;
			use[i] = false;
		}
		return dfs(1);
	}
	void put(int x[]){
		out.print(x[1]);
		for(int i = 2; i <= m; i++)out.print(" "+x[i]);
		out.println();
	}
	void work() {
		while(true){
		  	m = cin.nextInt(); n = cin.nextInt();
			if(m == 0 && n == 0)break;
			ok = false;
			for (int i = 1; i < n && ok == false; i++)
				if (judge(i))
					ok = true;
			if(ok)
				put(ans);
			else
				out.println("Not found.");
		}
	}
	Main() {
		cin = new Scanner(System.in);
		out = new PrintWriter(System.out);
	}

	public static void main(String[] args) {
		Main e = new Main();
		e.work();
		out.close();
	}

	public Scanner cin;
	public static PrintWriter out;
}
时间: 2024-10-12 00:32:33

Uva 10825 Anagram and Multiplication dfs 爆搜排列的相关文章

uva 10825 - Anagram and Multiplication(暴力)

题目链接:uva 10825 - Anagram and Multiplication 题目大意:给出m和n,要求找一个m位的n进制数,要求说该数乘以2~m中的任意一个数的结果是原先数各个位上数值的一个排序. 解题思路:枚举最后一位数,然后用这个数去乘以2~m并对n取模,然后得到的数一定就是这个数的组成,暴力搜索一下并判断. #include <cstdio> #include <cstring> #include <algorithm> using namespace

HDU 4403 A very hard Aoshu problem(dfs爆搜)

http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比较小,直接dfs爆搜答案即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 using nam

【BZOJ-1060】时态同步 树形DP (DFS爆搜)

1060: [ZJOI2007]时态同步 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2101  Solved: 595[Submit][Status][Discuss] Description 小Q在电子工艺实习课上学习焊接电路板.一块电路板由若干个元件组成,我们不妨称之为节点,并将其用数 字1,2,3….进行标号.电路板的各个节点由若干不相交的导线相连接,且对于电路板的任何两个节点,都存在且仅 存在一条通路(通路指连接两个元件的导线序列).

hdu 5506 GT and set(dfs爆搜)

Problem Description You are given N sets.The i−th set has Ai numbers.You should divide the sets into L parts.And each part should have at least one number in common.If there is at least one solution,print YES,otherwise print NO. Input In the first li

Ancient Go---hdu5546(dfs爆搜CCPC题目)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5546 题意就是两个人下围棋,问在下一颗x是否能杀死o,'.'是空位子: 枚举所有的点,判断是否合法即可: #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <vector> #include <algorithm> #

爆搜解hdu1572下沙小面的(2)

#include<iostream> #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<vector> #include<algorithm> using namespace std; in

有一种恐怖,叫大爆搜

到目前这个阶段,大爆搜做了好几个,有必要做一下小的总结了. 玛雅游戏:出门左转 http://www.cnblogs.com/Loser-of-Life/p/7247413.html的A 斗地主:出门右转http://www.cnblogs.com/Loser-of-Life/p/7259858.html的B 天鹅会面:出门直行http://www.cnblogs.com/Loser-of-Life/p/7295770.html的A 引水入城:链接:http://cogs.pro/cogs/pr

HDU 4735 DLX爆搜

Little Wish~ lyrical step~ Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 417    Accepted Submission(s): 109 Problem Description N children are living in a tree with exactly N nodes, on each n

hdu 5031 Lines 爆搜

其实嘞,这个线可以只延伸一端 然后嘞,爆搜一次就可以 最后嘞,600-800ms过 本弱就是弱啊,你来打我呀-- #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int a[100][100]; int n,m,ans; bool dfs(int step) { int i,j,t,ii,jj,x,y,cnt,tx,t