USACO 4.3.2 prime3
2012-06-29 22:39
246 查看
题目:
http://ace.delos.com/usacoprob2?a=0H14tt2CoyP&S=prime3
http://pingce.ayyz.cn:9000/usaco/data/20110129214306/index.html#Section_5.1
ThePrimes
IOI'94
Inthesquarebelow,eachrow,eachcolumnandthetwodiagonalscanbereadasafivedigitprimenumber.Therowsarereadfromlefttoright.Thecolumnsarereadfromtoptobottom.Bothdiagonalsarereadfromlefttoright.
Theprimenumbers'digitsmustsumtothesamenumber.
Thedigitinthetopleft-handcornerofthesquareispre-determined(1intheexample).
Aprimenumbermaybeusedmorethanonceinthesamesquare.
Ifthereareseveralsolutions,allmustbepresented(sortedinnumericalorderasifthe25digitswereallonelongnumber).
Afivedigitprimenumbercannotbeginwithazero(e.g.,00003isNOTafivedigitprimenumber).
ViewCode
ThePrimes
IOI'94
Inthesquarebelow,eachrow,eachcolumnandthetwodiagonalscanbereadasafivedigitprimenumber.Therowsarereadfromlefttoright.Thecolumnsarereadfromtoptobottom.Bothdiagonalsarereadfromlefttoright.
+---+---+---+---+---+ |1|1|3|5|1| +---+---+---+---+---+ |3|3|2|0|3| +---+---+---+---+---+ |3|0|3|2|3| +---+---+---+---+---+ |1|4|0|3|3| +---+---+---+---+---+ |3|3|3|1|1| +---+---+---+---+---+
Theprimenumbers'digitsmustsumtothesamenumber.
Thedigitinthetopleft-handcornerofthesquareispre-determined(1intheexample).
Aprimenumbermaybeusedmorethanonceinthesamesquare.
Ifthereareseveralsolutions,allmustbepresented(sortedinnumericalorderasifthe25digitswereallonelongnumber).
Afivedigitprimenumbercannotbeginwithazero(e.g.,00003isNOTafivedigitprimenumber).
PROGRAMNAME:prime3
INPUTFORMAT
Asinglelinewithtwospace-separatedintegers:thesumofthedigitsandthedigitintheupperlefthandcornerofthesquare.SAMPLEINPUT(fileprime3.in)
111
OUTPUTFORMAT
Fivelinesoffivecharacterseachforeachsolutionfound,whereeachlineinturnconsistsofafivedigitprimenumber.Printablanklinebetweensolutions.Iftherearenoprimesquaresfortheinputdata,outputasinglelinecontaining"NONE".SAMPLEOUTPUT(fileprime3.out)
Theaboveexamplehas3solutions.11351 14033 30323 53201 13313 11351 33203 30323 14033 33311 13313 13043 32303 50231 13331 题解: 看着很和谐的搜索题又来了一道,但是这道题却也是最恶心的一道……………… 首先把所有可能素数处理出来这是没说的,然后就开始搜索了………………网上MS有14重循环结束战斗的…………………………我觉得太可怕便换了种写法。 考虑以下将情况: abcde fg hi jk lmnop 则有b,c,d,e,f,h,j,l!=0l,m,n,o,p,e,g,i,k!=0,2,4,5,6,8,而它们共同形成了四个质数,于是我们便可以把除首位外没有偶数的质数,和每一位均不为0,2,4,5,6,8的质数处理出来,这样就可以把周围一层的四个质数全部处理出来,于是只剩下了中间的3*3de方阵。 abc def ghi 此时枚举a,b,然后c就出来了,然后通过解六元六次方程便可以把剩下的数全部解出来了。
ViewCode
/* ID:zhongha1 PROB:prime3 LANG:C++ */ #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> usingnamespacestd; intn,sum,l1,l2,l3,prime1[100000],prime2[100000],prime3[100000],map[6][6],ansl; boolfirst=true,able[20][20]; structnode { node*next[10]; node() { for(inta=0;a<=9;a++) next[a]=NULL; } }*root; structans_node { intmap[6][6]; voidprint() { if(!first)printf("\n"); first=false; for(inta=1;a<=5;a++) { for(intb=1;b<=5;b++) printf("%d",map[a][b]); printf("\n"); } } booloperator<(constans_node&c)const { for(inta=1;a<=5;a++) for(intb=1;b<=5;b++) if(map[a][b]!=c.map[a][b])returnmap[a][b]<c.map[a][b]; returnfalse; } }ans[10000]; boolprime(intnow) { intorz=now,nowsum=0; while(orz) { nowsum+=orz%10; orz/=10; } if(nowsum!=sum)returnfalse; for(inta=2;a*a<=now;a++) if(now%a==0)returnfalse; able[now/10000][now%10]=true; returntrue; } voidcheck1(intnow) { l1++; prime1[l1]=now; intorz=0; while(now) { orz=orz*10+now%10; now/=10; } /*for(inta=1;a<=5;a++) { inthehe=orz%10; if(p->next[hehe]==NULL)p->next[hehe]=newnode; p=p->next[hehe]; orz/=10; }*/ } voidcheck2(intnow) { if(now/10000!=n)return; intl=0,r=l1; while(l+1!=r) { intm=(l+r)>>1; if(prime1[m]>=now)r=m; elsel=m; } if(prime1[r]!=now)return; l2++; prime2[l2]=now; } voidcheck3(intnow) { intorz=now; while(orz) { inthehe=orz%10; if(hehe%2==0||hehe==5)return; orz/=10; } l3++; prime3[l3]=now; } boolcheck4(inta,intb,intc) { if(b%10!=c/10000)returnfalse; for(intd=1;d<=5;d++) { if(!able[a%10][c%10])returnfalse; a/=10; c/=10; } returntrue; } voidfill(inta,intb,intc,intd) { for(inte=1;e<=5;e++) { map[1][6-e]=a%10; a/=10; } for(inte=1;e<=5;e++) { map[6-e][1]=b%10; b/=10; } for(inte=1;e<=5;e++) { map[5][6-e]=c%10; c/=10; } for(inte=1;e<=5;e++) { map[6-e][5]=d%10; d/=10; } } boolexist(intnow) { intl=0,r=l1; while(l+1!=r) { intm=(l+r)>>1; if(prime1[m]>=now)r=m; elsel=m; } if(prime1[r]!=now)returnfalse; elsereturntrue; } boolcheck_map() { for(inta=1;a<=5;a++) for(intb=1;b<=5;b++) if(map[a][b]<0||map[a][b]>9)returnfalse; for(inta=1;a<=5;a++) { intnow=0; for(intb=1;b<=5;b++) now=now*10+map[a][b]; if(!exist(now))returnfalse; } for(inta=1;a<=5;a++) { intnow=0; for(intb=1;b<=5;b++) now=now*10+map[b][a]; if(!exist(now))returnfalse; } intnow=0; for(inta=1;a<=5;a++) now=now*10+map[a][a]; if(!exist(now))returnfalse; now=0; for(inta=1;a<=5;a++) now=now*10+map[6-a][a]; if(!exist(now))returnfalse; returntrue; } voidget_add_ans() { ansl++; for(inta=1;a<=5;a++) for(intb=1;b<=5;b++) ans[ansl].map[a][b]=map[a][b]; } intmain() { freopen("prime3.in","r",stdin); freopen("prime3.out","w",stdout); scanf("%d%d",&sum,&n); root=newnode; for(inta=10000;a<100000;a++) if(prime(a)) { check1(a); check3(a); } for(inta=1;a<=l1;a++) check2(prime1[a]); for(inta=1;a<=l2;a++) for(intb=1;b<=l2;b++) if(able[prime2[b]%10][prime2[a]%10]) { for(intc=1;c<=l3;c++) if(able [prime3[c]%10]&&check4(prime2[a],prime2[b],prime3[c])) { for(intd=1;d<=l3;d++) if(prime3[d]%10==prime3[c]%10&&check4(prime2[b],prime2[a],prime3[d])) { fill(prime2[a],prime2[b],prime3[c],prime3[d]); for(map[2][2]=0;map[2][2]<=9;map[2][2]++) for(map[3][2]=0;map[3][2]<=9;map[3][2]++) { map[4][2]=sum-map[1][2]-map[2][2]-map[3][2]-map[5][2]; intnow=0; for(inta=1;a<=5;a++) now=now*10+map[a][2]; if(!exist(now))continue; now=(sum-map[2][1]-map[2][2]-map[2][5])+2*(sum-map[3][1]-map[3][2]-map[3][5])+(sum-map[4][1]-map[4][2]-map[4][5])+(sum-map[5][1]-map[4][2]-map[1][5])+(sum-map[1][1]-map[2][2]-map[5][5])-(sum-map[1][3]-map[5][3])-2*(sum-map[1][4]-map[5][4]); if(now%3!=0)continue; map[3][3]=now/3; map[4][4]=sum-map[1][1]-map[2][2]-map[3][3]-map[5][5]; map[4][3]=sum-map[4][1]-map[4][2]-map[4][4]-map[4][5]; map[2][3]=sum-map[1][3]-map[3][3]-map[4][3]-map[5][3]; map[3][4]=sum-map[3][1]-map[3][2]-map[3][3]-map[3][5]; map[2][4]=sum-map[2][1]-map[2][2]-map[2][3]-map[2][5]; if(check_map())get_add_ans(); } } } } sort(ans+1,ans+ansl+1); for(inta=1;a<=ansl;a++) ans[a].print(); return0; }
相关文章推荐
- usaco Superprime Rib<dfs找回文>
- 【USACO1.3】牛式 Prime Cryptarithm
- 【枚举】洛谷 P1217 [USACO1.5]回文质数 Prime Palindromes
- USACO - Chapter1 Section 1.5 - Prime Palindromes
- 裸干usaco prime3
- USACO-Section1.3 Prime Cryptarithm
- USACO之Prime Cryptarithm
- <NOIP> 25 . P1217 [USACO1.5]回文质数 Prime Palindromes
- 数组去重 usaco Prime Palindromes
- USACO 1.3-Prime Cryptarithm
- USACO-Section1.6 Superprime Rib (枚举)
- USACO1.52 Prime Palindromes
- [USACO 1.3.4] Prime Cryptarithm
- USACO Prime Palindromes
- USACO 1.3.5 Prime Cryptarithm
- USACO SuperPrime Rib
- [USACO1.3]牛式 Prime Cryptarithm
- 【USACO-Chapter1-1.3】【模拟】Prime Cryptarithm
- USACO section 1.5 Superprime Rib(深搜)
- 【USACO 1.5】Prime Palindromes