URAL 1495. One-two, One-two 2

找一个最小的数 不超过30位 只能由1 2组成的并且是n的倍数

先算出15位 dp[i]表示余数为i的最小的数 dp2[i]表示长度正好是15位余数为i的最小的数

#include <cstdio>
#include <cstring>
#include <map>
#include <cstdlib>
using namespace std;
typedef long long LL;
LL dp[1000010], dp2[1000010];
void dfs(int i, LL x, LL n)
{
	LL tmp = x%n;
	if(!dp[tmp] || dp[tmp] > x)
	{
		dp[tmp] = x;
	}
	if(i == 15)
	{
		if(!dp2[tmp] || dp2[tmp] > x)
				dp2[tmp] = x;
		return;
	}
	dfs(i+1, x*10+1, n);
	dfs(i+1, x*10+2, n);
}
int main()
{
	int n;
	while(scanf("%d", &n) != EOF)
	{
		if(n == 1 || n == 2)
		{
			printf("%d\n", n);
			continue;
		}
		memset(dp, 0, sizeof(dp));
		dfs(0, 0, n);
		if(dp[0])
		{
			printf("%lld\n", dp[0]);
			continue;
		}
		LL tmp1 = -1, tmp2 = -1;

		for(int i = 1; i < n; i++)
		{
			if(!dp[i])
				continue;
			LL x = i;
			for(int i = 0; i < 15; i++)
				x = x*10%n;

			int tmp = n-x;
			if(!dp2[tmp])
				continue;
			if(tmp1 == -1 || tmp1 > dp[i])
			{
				tmp1 = dp[i];
				tmp2 = dp2[tmp];
				//printf("%lld %lld %lld\n", x, dp[tmp], (x+tmp)%n);
			}
		}
		if(tmp1 == -1)
			puts("Impossible");
		else
			printf("%lld%lld\n", tmp1, tmp2);
	}
	return 0;
}
/*
10007
211222212122
999997
2112221121222222212212222

*/
时间: 2024-07-29 23:57:32

URAL 1495. One-two, One-two 2的相关文章

Regionals 2012 :: Chengdu

题目连接 排行榜 A和I都是签到题 按位BFS K Yet Another Multiple Problem 题意:给一些可以用的数字,求最小的数,它由特定的数字组成且是n的倍数 分析:暴力枚举不可行,因为数字可能非常大.考虑到大数取模为0,BFS每一层即数位,递归输出路径. #include <bits/stdc++.h> const int N = 1e4 + 5; bool no[10]; std::pair<int, int> pre[N]; int dir[10]; bo

Ural 1081 Binary Lexicographic Sequence(DP)

题目地址:Ural 1081 先用dp求出每个长度下的合法序列(开头为1)的个数.然后求前缀和.会发现正好是一个斐波那契数列.然后每次判断是否大于此时长度下的最少个数,若大于,说明这一位肯定是1,若小于,则肯定是0.就这样不断输出出来即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #in

URAL 1684. Jack&#39;s Last Word KMP

题目来源:URAL 1684. Jack's Last Word 题意:输入a b 把b分成若干段 每一段都是a的前缀 思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前 如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab 因为已经记录了到i位置的最大匹配长度 根据长度从末尾倒退 每次倒退的时候只要是最大的匹配的长度 因为如果在某一次的递推 记录的最大匹配的前缀

51 nod 1495 中国好区间

1495 中国好区间 基准时间限制:0.7 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 阿尔法在玩一个游戏,阿尔法给出了一个长度为n的序列,他认为,一段好的区间,它的长度是>=k的,且该区间的第k大的那个数,一定大于等于T.那么问题来了,阿尔法想知道有多少好的区间. 由于阿尔法的序列长度实在是太大了,无法在规定时间内读入. 他想了一个绝妙的方法. 读入a[0],b,c,p,则a[i]=(a[i-1]*b+c)mod p. 样例解释: a1~a5分别为47,135,247,3

ural 1272. Non-Yekaterinburg Subway

1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is

ural 1273. Tie

1273. Tie Time limit: 1.0 secondMemory limit: 64 MB The subway constructors are not angels. The work under the ground and… Well, they are not angels. And where have you seen angels? It is all in a lifetime! Show me first somebody who has never… and t

ural 1269. Obscene Words Filter

1269. Obscene Words Filter Time limit: 0.5 secondMemory limit: 8 MB There is a problem to check messages of web-board visitors for the obscene words. Your elder colleagues commit this problem to you. You are to write a program, which check if there i

ural 1218. Episode N-th: The Jedi Tournament

1218. Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Knights to organize a tournament once. To know, accumulates who the largest amount of Force. Brought each Jedi his lightsaber with him to the tourn

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar