UVA 524】的更多相关文章

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number…
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1, 2, . . . , n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle sho…
如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候  首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立  还有要记得vis的使用和递归之后的清理. #include <cstdio> #include <cstring> using namespace std; ; +],vis [+]; +]; void dfs(int cur) { ]+a[]]) { printf(]); ;i<n;i++) printf("…
  Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always…
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<stack> #include<vector> #include<map> #includ…
Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1.…
Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Inp…
题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #include<cstdio> using namespace std; const int N = 50; int p[N], vis[N], a[N], n; int isPrime(int k) { for(int i = 2; i * i <= k; ++i) if(k % i == 0)…
题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行判断,可以提高效率. #include <cstdio> #include <cstring> ], vis[]; int n; int is_prime(int n) { ; i*i <= n; i++) ) ; ; } void dfs(int cur) { ]+A[n-])…
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; ]; ]; bool issushu(int n) { ;i<=sqrt(n);i++) { )return false; } return true; } void dfs(int n,int cur) { +ans[cur-] ) ) { ;i<n;i++) )p…
Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,3,...,n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should al…
dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++.h> using namespace std; int bk[110]; int num[110]; int vis[110]; int n; void db() { for(int i=2;i*i<=100;i++) if(!bk[i]) for(int j=i*i;j<=100;j+=i…
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 预处理出所有的答案. 打表输出. [代码] import java.util.ArrayList; import java.util.Iterator; import java.util.Scanner; public class Main { static int a[] = new int[20]; static boolean bo[] = new boolean[20]; static ArrayList<String>…
题意:输入n,把1~n组成个环,相邻两个数之和为素数. 分析:回溯法. #pragma comment(linker, "/STACK:102400000, 102400000") #include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<s…
A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,...,n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always be 1. Input…
[题目链接] https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=524&page=show_problem&problem=660 [题目大意] 给出一个字符串,求出与其循环同构的字符串中,字典序最小的一个. [题解] 以原字符串的两倍建立自动机,按字典序在parent树上搜索, 得到的第一个长度为n的字符串就是答案. [代码] #include <cstdio…
UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524&problem=4104&mosmsg=Submission+received+with+ID+14082913" target="_blank" style="">题目链接 题意:有m种字符(从'A'開始往后数的大写字母),如今有一个字符串,长…
UVA 11888 - Abnormal 89's option=com_onlinejudge&Itemid=8&page=show_problem&category=524&problem=2988&mosmsg=Submission+received+with+ID+14075396" target="_blank" style="">题目链接 题意:给定一个字符串.推断类型.一共三种.两个回文拼接成的,…
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5…
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径. f[i][j][k]从下往上到第i层第j个和为k的方案数 上下转移不一样,分开处理 没必要判断走出沙漏 打印方案倒着找下去行了,尽量往左走   沙茶的忘注释掉文件WA好多次   #include <iostream> #include <cstdio> #include <a…
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求LCS,转移同时维护f[i][j].s为当前状态字典序最小最优解 f[n][n].s的前半部分一定是回文串的前半部分(想想就行了) 当s的长度为奇时要多输出一个(因为这样长度+1,并且字典序保证最小(如axyzb  bzyxa,就是axb|||不全是回文串的原因是后半部分的字典序回文串可能不是最小,多…
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问题 |00|+|01|=|0| 注意序列是环形 // // main.cpp // poj3869 // // Created by Candy on 25/10/2016. // Copyright © 2016 Candy. All rights reserved. // #include <i…
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 // // Created by Candy on 24/10/2016. // Copyright © 2016 Candy. All rights reserved. // #include <iostream> #include <cstdio> #include <cst…
UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll…
UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这次转移会给其他的元素带来的代价,也就是转移前已经出现但还没结束的元素都会代价+1   预处理每种颜色在两个序列中出现的位置bg[i][0/1]和ed[i][0/1], 计算f[i][j]时同时计算w[i][j]为(i,j)这个状态已经出现还没结束的个数 注意bg要先初始化为INF,计算w: if(b…
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4053   Accepted: 1318 Description The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q…
UVA - 11584 Partitioning by Palindromes We say a sequence of char- acters is a palindrome if it is the same written forwards and backwards. For example, ‘racecar’ is a palindrome, but ‘fastcar’ is not. A partition of a sequence of characters is a lis…
UVA - 1025 A Spy in the Metro Secret agent Maria was sent to Algorithms City to carry out an especially dangerous mission. After several thrilling events we find her in the first station of Algorithms City Metro, examining the time table. The Algorit…
UVA - 11134 Fabled Rooks We would like to place n rooks, 1 ≤ n ≤ 5000, on a n × n board subject to the following restrictions The i-th rook can only be placed within the rectan- gle given by its left-upper corner (xli,yli) and its right- lower corner…
UVA - 11987 Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you’re to implement something similar, but not identical. The data structure you need to write is also a collection of disjoint sets, supporting 3 oper…