UVa-524 - Prime Ring Problem

UVA524 素数环 Prime Ring Problem DFS 素数环 阅读详情

好久没有刷UVa了,没想到居然一次AC,代码几乎与rujia没有什么太大的区别

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int a[20],n,cnt;
bool isprime(int m)
{
    for(int i=2;i<m;i++)
        if(m%i==0) return 0;
    return 1;
}
void fuc(int cur,bool used[20])
{
    if(cur==n)
    {
        printf("%d",a[1]);
        for(int i=2;i<=n;i++)
            printf(" %d",a[i]);
        printf("\n");
        return;
    }
    for(int i=2;i<=n;i++)
    {
        if(!used[i]&&isprime(a[cur]+i))
        {
            if((cur==n-1&&isprime(1+i))||cur<n-1)
            {
                a[cur+1]=i;
                used[i]=1;
                fuc(cur+1,used);
                used[i]=0;
            }
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        if(++cnt>1) printf("\n");
        printf("Case %d:\n",cnt);
        bool used[20]={};
        a[1]=1;
        used[1]=1;
        fuc(1,used);
    }
}
保姆级教程:在STM32H750VBT6上跑起来CMSIS-DSP库(基于CubeMX+Keil) 本文详细介绍了在STM32H750VBT6微控制器上配置和运行CMSIS-DSP库的全过程,包括开发环境搭建、CubeMX工程配置、Keil工程设置以及DSP库的实际应用示例。通过FFT频谱分析和音频均衡器等实战案例,展示了如何利用STM32H750VBT6的硬件FPU加速DSP运算,提升实时信号处理性能。 阅读详情

相关推荐

别再死记硬背了!用Amesim HCD库亲手搭建单向阀、锥阀、球阀模型(附详细参数设置)

本文详细介绍了如何使用Amesim HCD库从零构建单向阀、锥阀和球阀的仿真模型,帮助工程师摆脱死记硬背,掌握物理建模的核心技巧。通过实战案例和参数设置指南,读者将学会如何优化阀门动态响应、验证流量特性以及处理旋转运动等独特挑战,提升液压系统设计效率。

weixin_29248611的博客 499

例题7-4 UVA524 Prime Ring Problem(28行AC代码)

紫书刷题进行中,题解系列【GitHub|优快云】 例题7-4 UVA524 Prime Ring Problem(28行AC代码) 题目大意 给定n个数,标号为1,2,3,…,n,问n个数能否构成这样一个环:任意两个相邻数和为素数 思路分析 本质是求2~n的全排列,在构造过程中利用相邻和为素数进行剪枝 因为n最大为16,两数之和最大为31,因此可手动写出2~31的素数,便于查询 AC代码(C++...

努力做个书呆儿 244

PaddleOCR实战:从零开始训练自己的OCR模型(附完整代码与数据集处理技巧)

本文提供了一份详尽的PaddleOCR实战指南,手把手教你从零开始训练专属的OCR模型。内容涵盖高质量训练数据的准备与标注技巧、预训练模型的选型对比、关键参数调优策略,并附有完整的代码示例。通过遵循本文的步骤,开发者可以高效构建并优化贴合自身业务场景的高精度OCR模型,实现从数据到部署的全流程掌握。

weixin_29263201的博客 570

uva 524 Prime Ring Problem

此题比较坑 原因是题目没说明 输出格式 // // main.cpp // uva 524 - Prime Ring Problem // // Created by XD on 15/7/26. // Copyright (c) 2015年 XD. All rights reserved. #include #include #include #include #in

最爱她! 297

C#与发那科机器人通信实战:从SDK配置到机械臂控制

本文详细介绍了如何使用C#与发那科机器人进行通信,从SDK配置到机械臂控制的完整实战流程。内容包括环境准备、通信连接建立、机械臂运动控制、异常处理与状态监控,以及高级功能实战,帮助开发者快速掌握工业机器人编程技术。

weixin_42524276的博客 458

作物生长模型Oryza V3实战1:为什么要研究它

ORYZAV3是水稻生长模拟模型ORYZA2000的最新升级版本(3.5版),作为作物生长模型,它通过数学方法和计算机技术模拟水稻生长发育过程及其与环境条件的相互作用。主要功能包括:模拟不同条件下的水稻生长和产量形成、评估环境胁迫影响、分析土壤碳氮动态以及进行区域尺度模拟。国内应用广泛,科研机构用于气候变化影响评估、灌溉模式比较等研究,并开展参数敏感性分析和应用培训。使用流程分为六个步骤:确定输入参数(气象、土壤、作物三类)、参数校准、设置模拟环境、运行模型、分析结果(输出50个参数)和计算产量(穗干重×收

504

UVA - 524 Prime Ring Problem

题目链接: UVA - 524 Prime Ring Problem Description(素数环) A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 1,2,…,n1, 2, \dots, n into each circle separately, and the

Dextrad_ihacker的博客 791

(6)APB总线协议 —— 逻辑梳理(0.1认知)

这篇大家就差不多看看就行

CAUC_learner的博客 2702

UVa 524 Prime Ring Problem(素数环)

题目链接:UVa 524 题意: 输入正整数n,把正整数1~n组成一个环,使得相邻两个整数之和均为素数。输出时从整数1开始逆时针排列。同一个环应恰好输出一次。n 分析: 简单BFS. 注意: 两测试案例之间有一行空格,输出数据之间有空格,行末无空格 CODE: #include #include #include #include #include #incl

ramay7 723

Agent应用实践之三 - 基础:AgentScope-ReActAgent源码解析

本章通过总结AgentScope的核心类,再通过分析ReActAgent调用大模型的底层源码原理。使我们基本了解了Agent的基本原理,这个实现方式与OpenClaw使用pi-mono的Agent基础框架是一致的。了解完ReActAgent的基本原理,下一章我们会将一下SpringBoot集成方式的代码原理,这两章是我们构建后续内容的基本框架。

代码让AI扣 468

【Java基础】还看不懂JDK7 HashMap环的产生原理你来打我

JDK7对旧table数据重定位到新table的函数transfer如下,其中重点关注部分以标出。 头插法正常情况下: 并发情况下 线程1只执行了Entry<K,V> next = e.next就被挂起了,而线程2正常执行完毕,结果图如下: 线程1接着下面继续执行: 通过逐步分析跟绘图可以知道红色部分会有环产生。 ...

sowhat 4244

UVa 524 Prime Ring Problem

本文解决素数环问题:将1到n的偶数个整数排成环,使相邻数之和为素数。首数固定为1,需输出所有满足条件的排列。采用回溯算法,预先生成素数表进行剪枝优化。算法从位置0开始,依次填入未使用的数字,确保相邻和及首尾和为素数。代码实现中,使用深度优先搜索回溯,按字典序输出结果。对于n≤16的数据规模,通过素数约束有效降低搜索空间复杂度。

寂静山林 196

告别标注地狱!YOLO+CLIP多模态零样本检测全实战:图文联合推理,想检测什么直接输文字,不用训模型

这套方案我给朋友的超市用了快3个月,运行极其稳定,临期商品的检测准确率能到96%,后续加了“货架空货检测”“通道杂物堆积检测”,都是改一行文本就搞定,完全不用重新开发,朋友说省了十几万的定制开发费用。很多做计算机视觉的朋友,总觉得目标检测必须标数据、训模型,其实不是的。现在多模态大模型的能力已经足够强,把YOLO的定位能力和CLIP的图文泛化能力结合起来,就能用极低的成本,搞定绝大多数定制化检测需求。对于我们开发者来说,技术的价值从来不是炫技,而是用最简单的方案,解决最实际的问题。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 515

Prime Ring Problem uva 524

回溯法的应用 # include<iostream> # include<cstring> using namespace std; const int maxn=100+5; bool isprim[maxn]; int a[maxn]; int n; bool vis[maxn]; bool prim(int i) { for(int j=2;j<i;j++...

艾克 248

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Prime Ring Problem UVA - 524

题目连接 :https://vj.ti12z.cn/fd5e336dbfda2775780486ea45633879?v=1557301401 #include<iostream> #include<cstring> #include<cmath> using namespace std; const int MAX = 1005 ; bool isp...

森宝的博客 369

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

Prime Ring Problem UVA - 524(回溯)

打印所有素数环,数值都是1-n。回溯算法,素数筛。先打表。然后dfs:先看看两数只和是不是素数(查表),然后再往下递归,另外已经用过的数字不能出现第二次,用布尔数组下标当做数,布尔值来表示能不能访问。递归完之后别忘了还原之前的数值。

Crushlikearush的博客 177

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

UVa 524 素数环(Prime Ring Problem)

输入正整数n, 把整数1,2,3,……,n组成一个环,使得相邻两个整数之和均为素数。输出时从1开始逆时针排列,同一个环应该只输出一个。 样例输入: 6 样例输出: 1 4 3 2 5 6 1 6 5 2 3 4 因为输入的n不超过16 因此素数的大小应该不会超过 16 + 15 = 31 设置一个素数数组,避免屡次判断超时。 设置一个vis数组,因为素数环每个数字只出现一次,因此用一个 vis数组...

Inuyasha__的博客 347

UVA - 524 Prime Ring Problem

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 s

爆米 745

uva 524 Prime Ring Problem

1 #include #include #include using namespace std; int n,a[25]; int vis[25]; bool is_prime(int x) { for(int i=2;i*i<=x;i++) { if(x%i==0) return false; } return true; } void dfs

qq_33901573的博客 314

UVa 524 - Prime Ring Problem(回溯)

回溯法的简单应用。

ZmiL_ 1371

Prime Ring Problem UVA - 524 回溯

A ring is composed of n (even number) circles as shown in diagram.Put natural numbers 1, 2, . . . , n into each circle separately, and thesum of numbers in two adjacent circles should be a prime.Note:...

lesfhappy的博客 227

UVa 524 Prime Ring Problem(回溯)

传送门 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....

圣·马坡岭皇家码农 246

【UVa】524 - Prime Ring Problem

Problem hereProblemA 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 shoul

Blog 704
上一篇: 有点迷茫
下一篇: UVa-129 - Krypton Factor
windrises
博客等级 码龄12年 7粉丝 39原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值