UVA 1341 - Different Digits(数论)
UVA 1341 - Different Digits
题意:给定一个正整数n。求一个kn使得kn上用的数字最少。假设同样,则输出值最小的
思路:
首先利用鸽笼原理证明最多须要2个数字去组成
设一个数字k。组成k,kk,kkk,kkkk... %n之后余数必定在0 - (n - 1)之间,所以必定能选出两个余数相等的数字相减为0,这个数字就是由0和k组成的。
因此仅仅要考虑一个数字和两个数字的情况,去bfs。记忆化余数。由于余数反复必定形成周期了
代码:
#include <stdio.h>
#include <string.h> const int INF = 0x3f3f3f3f;
const int N = 66666;
int n, num[2], vis[N], ans, save[N];
struct Queue {
int mod, pre, num, len;
Queue(){}
Queue(int mod, int pre, int num, int len) {
this->mod = mod;
this->pre = pre;
this->num = num;
this->len = len;
}
} q[N * 2]; void out(int now, int d) {
if (now == -1) return;
out(q[now].pre, d - 1);
save[d] = q[now].num;
} int judge(int now, int d) {
if (now == -1) return 0;
int tmp = judge(q[now].pre, d - 1);
if (tmp != 0) return tmp;
if (save[d] == q[now].num) return 0;
else if (q[now].num < save[d]) return -1;
else return 1;
} void bfs() {
int head = 0, tail = 0;
if (num[0] != 0) {
q[tail++] = Queue(num[0] % n, -1, num[0], 1);
vis[num[0] % n] = 1;
}
if (num[1] != -1 && num[1] != 0) {
q[tail++] = Queue(num[1] % n, -1, num[1], 1);
vis[num[1] % n] = 1;
}
while (head < tail) {
Queue now = q[head];
if (now.len > ans) return;
if (now.mod == 0) {
if (now.len <= ans) {
if (now.len != ans || judge(head, ans - 1) < 0) {
ans = now.len;
out(head, ans - 1);
}
}
}
Queue next;
for (int i = 0; i < 2; i++) {
if (num[i] == -1) continue;
next = Queue((now.mod * 10 + num[i]) % n, head, num[i], now.len + 1);
if (vis[next.mod]) continue;
vis[next.mod] = 1;
q[tail++] = next;
}
head++;
}
} int main() {
while (~scanf("%d", &n) && n) {
ans = INF;
for (int i = 1; i < 10; i++) {
num[0] = i; num[1] = -1;
memset(vis, 0, sizeof(vis));
bfs();
}
if (ans == INF) {
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
num[0] = i; num[1] = j;
memset(vis, 0, sizeof(vis));
bfs();
}
}
}
for (int i = 0; i < ans; i++)
printf("%d", save[i]);
printf("\n");
}
return 0;
}
UVA 1341 - Different Digits(数论)的更多相关文章
- Uva 11198 - Dancing Digits
		Problem D Dancing Digits 题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid ... 
- UVA 10627 - Infinite Race(数论)
		UVA 10627 - Infinite Race option=com_onlinejudge&Itemid=8&page=show_problem&category=516 ... 
- uva 10555 - Dead Fraction)(数论)
		option=com_onlinejudge&Itemid=8&category=516&page=show_problem&problem=1496" st ... 
- uva 10560 - Minimum Weight(数论)
		题目连接:uva 10560 - Minimum Weight 题目大意:给出n,问说至少须要多少个不同重量的砝码才干称量1~n德重量,给出所选的砝码重量,而且给出k,表示有k个重量须要用上述所选的砝 ... 
- UVA 11754 - Code Feat(数论)
		UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ... 
- UVA 718 - Skyscraper Floors(数论)
		UVA 718 - Skyscraper Floors 题目链接 题意:在一个f层高的楼上,有e个电梯,每一个电梯有x,y表示y + k * x层都能够到,如今要问从a层是否能到达b层(中间怎么换乘电 ... 
- uva 10692 - Huge Mods(数论)
		题目链接:uva 10692 - Huge Mods 题目大意:给出一个数的次方形式,就它模掉M的值. 解题思路:依据剩余系的性质,最后一定是行成周期的,所以就有ab=abmod(phi[M])+ph ... 
- UVA 12009 - Avaricious Maryanna(数论)
		UVA 12009 - Avaricious Maryanna 题目链接 题意:给定一个n.求出n个数位组成的数字x,x^2的前面|x|位为x 思路:自己先暴力打了前几组数据,发现除了1中有0和1以外 ... 
- uva 11728 - Alternate Task(数论)
		版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/36409469 option=com_onli ... 
随机推荐
- 【Linux】VMware中为CentOS设置静态IP(非动态获取IP)
			在VMware上安装好Linux后,默认设置的动态IP,每次启动的IP都不同,远程连接挺费劲的. 于是,需要设置静态的IP,至少我从远程工具连接上去方便多了.另外,为了安装一些软件,也需要访问互联网. ... 
- Oracle PLSQL Demo - 10.For Loop遍历游标[FOR LOOP CURSOR]
			declare cursor cur_emp is select t.* from scott.emp t; begin for r_emp in cur_emp loop dbms_output.p ... 
- Django视图层之路由配置系统(urls)
			视图层之路由配置系统(urls) URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于这个U ... 
- cocos2d-x 3.0点击响应
			迄今为止,发现cocos2d-x 3.0最让人惊艳的地方就是更改了点击事件机制.(ps:迄今只看了点击事件这块,捂嘴笑~~~) cocos2d-x 2.0 只有CCLayer有点击事件处理,需要注册, ... 
- Hbase1.1.x Java版之批量查删操作
			import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; import org.apache.had ... 
- LeetCode: Divide Two Integers 解题报告
			Divide Two Integers Divide two integers without using multiplication, division and mod operator. SOL ... 
- 在django中访问静态文件(js css img)
			刚开始参考的是别的文章,后来参考文章<各种 django 静态文件的配置总结>才看到原来没有但是没有注意到版本,折腾了一晚上,浪费了很多很多时间.后来终于知道搜索django1.7访问静态 ... 
- spark sql 入门
			package cn.my.sparksql import cn.my.sparkStream.LogLevel import org.apache.spark.{SparkConf, SparkCo ... 
- 【Unity笔记】常用插件
			记录一些常见插件,随时补充. iTween动画插件 原理:插值法,给出初始值和终点值,自动算出中间值. DoTween Tween动画 Playmaker $45 Playmaker由第三方软件商Hu ... 
- Hibernate- hibernate二级缓存
			原文地址:http://www.iteye.com/topic/18904 很多人对二级缓存都不太了解,或者是有错误的认识,我一直想写一篇文章介绍一下hibernate的二级缓存的,今天终于忍不住了. ... 
