USACO section 1.5 Superprime Rib(深搜)
2012-08-13 14:24
495 查看
SuperprimeRib
ButcheringFarmerJohn'scowsalwaysyieldsthebestprimerib.Youcantellprimeribsbylookingatthedigitslovinglystampedacrossthem,onebyone,byFJandtheUSDA.FarmerJohnensuresthatapurchaserofhisprimeribsgetsreallyprimeribsbecausewhenslicedfromtheright,thenumbersontheribscontinuetostayprimerightdowntothelastrib,e.g.: [code]7331
Thesetofribsdenotedby7331isprime;thethreeribs733areprime;thetworibs73areprime,and,ofcourse,thelastrib,7,isprime.Thenumber7331iscalledasuperprimeoflength4.
WriteaprogramthatacceptsanumberN1<=N<=8ofribsandprintsallthesuperprimesofthatlength.
Thenumber1(byitself)isnotaprimenumber.
PROGRAMNAME:sprime
INPUTFORMAT
AsinglelinewiththenumberN.SAMPLEINPUT(filesprime.in)
4
OUTPUTFORMAT
ThesuperprimeribsoflengthN,printedinascendingorderoneperline.SAMPLEOUTPUT(filesprime.out)
2333 2339 2393 2399 2939 3119 3137 3733 3739 3793 3797 5939 7193 7331 7333 7393
[/code]
/*
ID:nealgav1
PROG:sprime
LANG:C++
*/
#include<cstdio>
#include<cmath>
intm;
boolisprim(intnum)//判断质数
{
if(num==2)return1;
if(num&1)
{
intn=(int)sqrt(num);
for(inti=3;i<=n;i+=2)
if(!(num%i))
return0;
return1;
}
return0;
}
voiddfs(intnum,intn)
{
intans;
if(m==n)
{
printf("%d\n",num);
return;
}
for(inti=1;i<=9;i+=2)
{
ans=num*10+i;
if(isprim(ans))
dfs(ans,n+1);
}
}
intmain()
{
freopen("sprime.out","w",stdout);
freopen("sprime.in","r",stdin);
scanf("%d",&m);
dfs(2,1);dfs(3,1);dfs(5,1);dfs(7,1);
}
USER:NealGavinGavin[nealgav1]
TASK:sprime
LANG:C++
Compiling...
Compile:OK
Executing...
Test1:TESTOK[0.000secs,3344KB]
Test2:TESTOK[0.000secs,3344KB]
Test3:TESTOK[0.000secs,3344KB]
Test4:TESTOK[0.000secs,3344KB]
Test5:TESTOK[0.000secs,3344KB]
AlltestsOK.
Yourprogram('sprime')producedallcorrectanswers!Thisisyour
submission#2forthisproblem.Congratulations!
Herearethetestdatainputs:
-------test1----
4-------test2----
5
-------test3----
6
-------test4----
7
-------test5----
8
Keepupthegoodwork!
Thanksforyoursubmission!
SuperPrimeRibRussCox
Weusearecursivesearchtobuildsuperprimesoflengthnfromsuperprimesoflengthn-1byaddinga1,3,7,or9.(Numbersendinginanyotherdigitaredivisibleby2or5.)Sincetherearesofewnumbersbeingtested,asimpleprimalitytestsuffices.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
FILE*fout;
int
isprime(intn)
{
inti;
if(n==2)
return1;
if(n%2==0)
return0;
for(i=3;i*i<=n;i+=2)
if(n%i==0)
return0;
return1;
}
/*printallsprimespossiblebyaddingndigitdigitstothenumbern*/
void
sprime(intn,intndigit)
{
if(ndigit==0){
fprintf(fout,"%d\n",n);
return;
}
n*=10;
if(isprime(n+1))
sprime(n+1,ndigit-1);
if(isprime(n+3))
sprime(n+3,ndigit-1);
if(isprime(n+7))
sprime(n+7,ndigit-1);
if(isprime(n+9))
sprime(n+9,ndigit-1);
}
void
main(void)
{
intn;
FILE*fin;
fin=fopen("sprime.in","r");
assert(fin!=NULL);
fout=fopen("sprime.out","w");
assert(fout!=NULL);
fscanf(fin,"%d",&n);
sprime(2,n-1);
sprime(3,n-1);
sprime(5,n-1);
sprime(7,n-1);
exit(0);
}
相关文章推荐
- USACO-Section 1.5 Superprime Rib(Miller-Rabin)
- USACO section 1.5 Superprime Rib(深搜)
- USACO-Section1.6 Superprime Rib (枚举)
- USACO 1.5 SuperPrime Rib (sprime)
- luogu1218 [USACO1.5] Superprime Rib(搜索+素数判断)
- Superprime Rib_特殊的质数肋骨_usaco1.5_codevs2080_dfs
- USACO 1.5 Superprime Rib (DFS)
- 洛谷P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
- 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib
- usaco1.5.3 SuperPrime Rib
- USACO Section 1.5 Superprime Rib 解题报告
- usaco Superprime Rib<dfs找回文>
- USACO - Chapter1 Section 1.5 - Prime Palindromes
- USACO - Chapter1 Section 1.5 - Superprime Rib
- Superprime Rib chapter 1.5
- USACO Section1.5 Prime Palindromes 解题报告
- USACO-Section1.5 Superprime Rib【深度优先搜索】
- Superprime Rib(usaco)
- USACO-section1.5 Superprime Rib[深搜]
- USACO Section1.5 Superprime Rib 解题报告