uva 524(素数环)

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.

Input

n (0 < n <= 16)

Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements.

You are to write a program that completes above process.

Sample Input

6
8

Sample Output

Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

Miguel A. Revilla

1999-01-11

题意:
输入正整数n,把1,2,3,...,n组成一个环,使得相邻两个正数之和均为素数,输出时按从整数1开始逆时针排列,同一个环恰好输出一次。
思路:
用dfs进行回溯。
代码:
#include<cstdio>
#include<algorithm>
using namespace std;
int S[20];
int main()
{
    int n,casex=1;
    long long ans,a;
    while(scanf("%d",&n)!=EOF)
    {
       ans=0;
        for(int i=1;i<=n;i++)
            scanf("%d",&S[i]);
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                    a=1;
                for( int k=i;k<=j;k++)
                {
                    a*=S[k];
                }
                ans=max(ans,a);

            }
        }
            printf("Case #%d: The maximum product is %lld.\n\n",casex,ans);
            casex++;
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-02 06:53:25

uva 524(素数环)的相关文章

UVA 524 素数环 【dfs/回溯法】

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 alwa

UVa 524 - Prime Ring Problem

题目:把1-n,连续的放到一个环里,使相邻的数字和为素数,输出所有结果. 分析:搜索+剪枝.如果裸搜,用dancing-links那种拆装的链表,应该差不多满足16的数据量. 这里利用一个性质进行剪枝:相邻的数字一定是奇偶性不同的数字. (如果上述假设不成立,则存在相邻的奇数或偶数,那么他们的和一定是大于2的偶数,不是素数) 根据上面的假设,还有一条推论:只有n为偶数时才有解. (n为奇数,一直至少有一对奇数相邻,同上,矛盾(鸽巢原理)) 因此,每次搜索的数据其实是n/2,时间复杂度为O((n/

UVA - 524 Prime Ring Problem(dfs回溯法)

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

[2016-02-19][UVA][524][Prime Ring Problem]

UVA - 524 Prime Ring Problem Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the

nyoj 488 素数环(深搜)

素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输入 有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束. 输出 每组第一行输出对应的Case序号,从1开始. 如果存在满足题意叙述的素数环,从小到大输出. 否则输出No Answer. 样例输入 6 8 3 0 样

Uva 524 Prime Ring

如果用全排列生成之后,在判断是否是素数环是会超时的,应该用回溯. 回溯的时候  首先要注意 递归边界 ,结束的时候别忘记判断最后一个和第一个元素能否成立  还有要记得vis的使用和递归之后的清理. 1 #include <cstdio> 2 #include <cstring> 3 using namespace std; 4 int n,p=0; 5 int a[50+10],vis [50+10]; 6 bool isprime[50+10]; 7 void dfs(int c

素数环 与 算法 全排列

在说起全排列前,先说一下昨天碰到的一个题目(答案不是我做出来的,但是我感觉有好多个亮点,贴出来方便日后的学习): 素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输入 有多组测试数据,每组输入一个n(0<n<20),n=0表示输入结束. 输出 每组第一行输出对应的Case序号,从

HDU 1016 素数环(深搜)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25134 Accepted Submission(s): 11222 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1,

素数环——搜索与回溯

题目描述 Description 从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数. 输入输出格式 Input/output 输入格式:无输入输出格式:一个整数(第几号素数环),一列数字(表示这个素数环) 输入输出样例 Sample input/output 样例测试点#1 输入样例: 无 输出样例: 这里就不演示了(…) 思路:代码很清楚,此处无需讲~~ 代码如下: 1 #include <stdio.h> 2 #include <math.h> 3 int a[3

HDU 1016 Prime Ring Problem(素数环问题)

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 63806    Accepted Submission(s): 27457 Problem Description A ring is compos