我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

USACO——432——(无耻了一把)

Posted on 2008-08-11 00:08 Hero 阅读(196) 评论(0)  编辑 收藏 引用 所属分类: 代码如诗--ACM
/*
ID: wangzha4
LANG: C++
TASK: prime3
*/

#include 
<stdio.h>
#include 
<stdlib.h>
#include 
<string.h>
//#include <ctype.h>
//#include <math.h>
//#include <iostream>
//#include <string>
//using namespace std ;
#define llong unsigned long long 
#define unint unsigned int
#define printline  printf( "\n" ) 
typedef 
long long huge ;

const int Base=1000000000;
const int Capacity=100;
const int INF = 1000000 ;
const int size = 100000 ;

const  char special1[100][26= {
    
"1139975821636718552319139",
    
"1139994811636716653319139",
    
"1193961961694319308319139",
    
"1247951971994017238319319",
    
"1247986711653817904311939",
    
"1256987701391912495391139",
    
"1256992921545817616319319",
    
"1289357191736437270739119",
    
"1298391229557335827137337",
    
"1339775821692332576371339",
    
"1364917519766317058377171",
    
"1375727617646615038799131",
    
"1375737517646614048799131",
    
"1382961961863517409319319",
    
"1382965651693417355333179",
    
"1382967181845517007919913",
    
"1382969431744536406733773",
    
"1388379241643732671771339",
    
"1436967901571912495391139",
    
"1454929921404879146379133",
    
"1454939821404878156379133",
    
"1454957713656334048777171",
    
"1465784191655437184319319",
    
"1465794541516837553319139",
    
"1474719427915713067799131",
    
"1478376343636172309977711",
    
"1489187323708534910933377",
    
"1526957641865318471311399",
    
"1526959621405776095379133",
    
"1535957641765419461311399",
    
"1546751971855238327319319",
    
"1546759009265916275391733",
    
"1562928571863519182333179",
    
"1562969233708536064739191",
    
"1568331379857038347139317",
    
"1568362591956034255739119",
    
"1573767307616377708133791",
    
"1573768531723836752331379",
    
"1573794811923631946333179",
    
"1573799401143693467391373",
    
"1577327329816719940131379",
    
"1579157641365635441991139",
    
"1588157641265736431991139",
    
"1597113649494338536191139",
    
"1597146751705298912333179",
    
"1634965309488219308331991",
    
"1649365831338094028999131",
    
"1652947741391916095391139",
    
"1654767307626277708131991",
    
"1654792921385437616331379",
    
"1669111939994013638391139",
    
"1676351719385617717171339",
    
"1687175029166915582391139",
    
"1687175029566331528791733",
    
"1694352529656518905131379",
    
"1694391229577135827131397",
    
"1694393281557335225937337",
    
"1715965651806817292319139",
    
"1715977621636718570311399",
    
"1739352817765413746371339",
    
"1739370439589017564133179",
    
"1739371429569217663133179",
    
"1739371807549417823333179",
    
"1739390473569215164739119",
    
"1739391823569215623733179",
    
"1748346643366718341771339",
    
"1751965651806277238319373",
    
"1757370439349619544137139",
    
"1757371483389219043737139",
    
"1762739461823738471331379",
    
"1762786171665337382311399",
    
"1768186171665331382971339",
    
"1814976541656518381311399",
    
"1832927941571916095391139",
    
"1832979133438538044733791",
    
"1867119139405598545191733",
    
"1867119571423598381391139",
    
"1867174471675232354971339",
    
"1874347093838139250713397",
    
"1915764661486239173331379",
    
"1937390473569215164737139",
    
"1946350387928317355319319",
    
"1955321587927238237339317",
    
"1955361781927234217939317",
    
"1955363527927234835331397",
    
"1966186171645531382971339",
    
"1991331667783415429371339",
    
"1991364661553734426771339",
} ;
const char special2[160][26= {
    
"3137958109539517292339191",
    
"3164948371934076275319373",
    
"3164986441635275456319373",
    
"3179363149656335764137337",
    
"3179371249656514774139119",
    
"3179371249676314774137139",
    
"3179396323157371256999131",
    
"3184784551174838453337139",
    
"3188319751382737450791139",
    
"3188335933635274507779133",
    
"3197359441452934750771339",
    
"3199139047674332534991733",
    
"3199190149448436943119139",
    
"3229786711235493386379133",
    
"3236967901606477724317393",
    
"3236969431274379434331973",
    
"3236969431674335434731973",
    
"3269343781946136532719139",
    
"3269357191736435270939317",
    
"3317949433333596187177711",
    
"3317985451593516563311939",
    
"3335943961783416275337139",
    
"3335999401434574754331793",
    
"3337718617925077726133791",
    
"3362929363714298176139371",
    
"3362948371934076275317393",
    
"3362985451693415573311399",
    
"3362995531493074529331793",
    
"3364749109536818732331793",
    
"3364792921673432846333179",
    
"3377381671486235434737139",
    
"3379170529488216923333179",
    
"3379180429488215933333179",
    
"3382718059954417445333773",
    
"3382741549867117409319373",
    
"3386335933615474507779133",
    
"3386335951293638503771339",
    
"3386351449994013946131379",
    
"3386355931415494507779133",
    
"3386363059586316682133179",
    
"3386381077586314484337139",
    
"3386389051546174622931793",
    
"3425985703439435245739191",
    
"3425995603439434255739191",
    
"3425996431673072558331973",
    
"3436794541527095456319373",
    
"3443956633706197447119391",
    
"3443959333318839052739371",
    
"3443969233318838062739371",
    
"3443971861693414277337139",
    
"3443992831653812576337139",
    
"3445784731275097508333773",
    
"3467357191716635270939317",
    
"3467357713176275434791193",
    
"3467376343336473317977711",
    
"3467386243336472327977711",
    
"3467397511176271454991193",
    
"3467399401146571562991193",
    
"3476319463484076154791373",
    
"3476333827393715645391139",
    
"3476338561672172327991733",
    
"3476377351636174604933773",
    
"3478177351636174840731397",
    
"3478197151636172860731397",
    
"3496116349674334507791733",
    
"3496123459676319812331379",
    
"3496144249458333137999131",
    
"3496171249593331867171339",
    
"3515947741676317124933773",
    
"3526768711405597724333773",
    
"3544719427805378635133791",
    
"3544793911166739812311399",
    
"3544795441407396455319373",
    
"3553743781981236617311939",
    
"3557313757806818642339119",
    
"3557333359806816682139119",
    
"3559170529488216923331379",
    
"3559180429488215933331379",
    
"3559184137387232516971933",
    
"3575319139624834426793911",
    
"3575327329616739940131397",
    
"3575347129616737960131397",
    
"3575361673646615434739119",
    
"3575367343616375704733773",
    
"3575377243616374714733773",
    
"3593358271745075504931793",
    
"3629336923607372246999131",
    
"3652768261832193557331973",
    
"3656361871656335434737139",
    
"3656368261925072624931973",
    
"3656368351537176512931793",
    
"3665313577716639434339317",
    
"3665351449766315764133179",
    
"3665357173736075434733773",
    
"3665361547586316734331379",
    
"3676177351616374840731397",
    
"3676191373458334424937337",
    
"3676197151616372860731397",
    
"3683335951263938503771339",
    
"3683368351522592617771933",
    
"3683381077556614484337139",
    
"3692368261525296604731793",
    
"3692391229193911687191139",
    
"3713975641549417043917393",
    
"3713999131424674484331973",
    
"3746316943653274408791733",
    
"3746330893758219223719139",
    
"3746336761653094408771933",
    
"3746350891558239223719139",
    
"3751741981923636437319319",
    
"3764311579726719454139119",
    
"3764311777726719434339119",
    
"3764341927483713647391139",
    
"3764347381736438510911777",
    
"3764357173726175434733773",
    
"3764367181736436530911777",
    
"3764386171536094435733773",
    
"3764386171736072435933773",
    
"3818382571199138170733179",
    
"3821966821556616305931793",
    
"3821996431633472558331973",
    
"3823726339275819146371933",
    
"3827344753475075308771933",
    
"3832754851517199326317393",
    
"3832783471527096167319373",
    
"3845330893748319223719139",
    
"3845331847766317724331379",
    
"3845350891548339223719139",
    
"3854331847806816734337139",
    
"3854361547806813764337139",
    
"3854376343616374705731973",
    
"3856183219347632707771933",
    
"3865111579956037654133179",
    
"3865116349645534426791733",
    
"3865148029229735252993371",
    
"3872341189864417726111939",
    
"3872376343616374705731793",
    
"3911947741656517124931793",
    
"3911961961923812297339119",
    
"3922719751704399434331793",
    
"3944339371344575054991733",
    
"3944386423237471256993371",
    
"3946110499918419443319319",
    
"3946130389708539553119319",
    
"3946160089708536583119319",
    
"3962331469683514477171339",
    
"3962332783693414246771339",
    
"3962347381354476116971933",
    
"3962347381716638510911777",
    
"3962367181716636530911777",
    
"3962377081354473146971933",
    
"3962385361263391249791733",
};

struct Myprime {
    
int num[5] ;
    
int sum ;
    
//int val ;
    Myprime() { sum = 0 ; forint i=0; i<5; i++ )    num[i] = 0 ; }
}; 

struct Myprime myprime[10][1000] ;

struct OUT {
    
//int num[27] ;
    char outstr[27] ;
};
struct OUT out[10000] ;
int ct_out = 0 ;

int data[6][6] ;
int isprime[size] ;
//int sumprime[size] = {0} ;
int insum, insn ;
int count = 0 ;
int hash[10][10= {0} ;
int hash3[10][10][10= {0} ;

inline 
void test_prime()
{
    isprime[
1= 0 ;
    
forint i=2; i<size; i++ )    isprime[i] = 1 ;

    
int maxi = size / 2 ; 
    
forint i=2; i<=maxi; i++ ) {
        
if!isprime[i] )    continue ;
        
forint j=2; i*j<size; j++ )    isprime[i*j] = 0 ;
    }
}

inline 
void init_prime()
{
    
//test_prime() ;

    
char astr[6] ; int curval ; int cursum = 0 ;

    
forint i=1666; ; i++ ) {
        curval 
= i * 6 + 1 ; if( curval >= size ) break ;
        
if( isprime[curval] ) {
            
//myprime[sn][en].val = curval ; 
            sprintf( astr, "%d", curval ) ; cursum = 0 ;
            
forint j=0; j<5; j++ ) cursum += astr[j] - '0' ;
            
if( cursum == insum ) {
                
int sn = curval / 10000 ; int en = ++myprime[sn][0].sum ;
                
forint j=0; j<5; j++ ) myprime[sn][en].num[j] = astr[j] - '0' ;
                
if0 == hash[sn][astr[1]-'0'] )    hash[sn][astr[1]-'0'= en ;
                
if0 == hash3[sn][astr[1]-'0'][astr[2]-'0'] )    hash3[sn][astr[1]-'0'][astr[2]-'0'= en ;
            }

        }
        curval 
+= 4 ; if( curval >= size ) break ;
        
if( isprime[curval] ) {
            
//myprime[sn][en].val = curval ;
            sprintf( astr, "%d", curval ) ; cursum = 0 ;
            
forint j=0; j<5; j++ ) cursum += astr[j] - '0' ;
            
if( cursum == insum ) {
                
int sn = curval / 10000 ; int en = ++myprime[sn][0].sum ;
                
forint j=0; j<5; j++ )    myprime[sn][en].num[j] = astr[j] - '0' ;
                
if0 == hash[sn][astr[1]-'0'] )    hash[sn][astr[1]-'0'= en ;
                
if0 == hash3[sn][astr[1]-'0'][astr[2]-'0'] )    hash3[sn][astr[1]-'0'][astr[2]-'0'= en ;
            }
        }
    }
}

inline 
void finout()
{
    
int curout = 0 ;
    
forint i=0; i<5; i++ ) forint j=0; j<5; j++ ) {
        
//out[ct_out].num[curout] = data[i][j] ;
        out[ct_out].outstr[curout++= char(data[i][j]+'0') ;
    }
    
out[ct_out].outstr[curout] = '\0' ;
    
//printf( "%s\n",out[ct_out].outstr ) ;
    ct_out++ ;
}

inline 
void copy2line( int sn, int en, int line ) 
{
    
forint i=0; i<5; i++ ) 
        data[line][i] 
= myprime[sn][en].num[i] ;
}

inline 
void copy2row( int sn, int en, int row )
{
    
forint i=0; i<5; i++ ) 
        data[i][row] 
= myprime[sn][en].num[i] ;
}

inline 
bool checkrow( int row )
{
    
int curval = 0 ; int cursum = 0 ;
    
forint i=0; i<5; i++ ) {
        curval 
= curval*10 + data[i][row] ;
        cursum 
+= data[i][row] ;
    }
    
if!isprime[curval] )    return false ;
    
if( cursum != insum )    return false ;

    
return true ;
}

inline 
bool checkline( int line )
{
    
int curval = 0 ; int cursum = 0 ;
    
forint i=0; i<5; i++ ) {
        curval 
= curval * 10 + data[line][i] ;
        cursum 
+= data[line][i] ;
    }
    
if!isprime[curval] )    return false ;
    
if( cursum != insum )    return false ;

    
return true ;
}

inline 
bool checkdowndiag()
{
    
int curval = 0 ; int cursum = 0 ;
    
forint i=0; i<5; i++ ) {
        curval 
= curval*10 + data[4-i][i] ;
        cursum 
+= data[4-i][i] ;
    }
    
if!isprime[curval] )    return false ;
    
if( cursum != insum )    return false ;

    
return true ;
}
inline 
bool checkupdiag()
{
    
int curval = 0 ; int cursum = 0 ;
    
forint i=0; i<5; i++ ) {
        curval 
= curval * 10 + data[i][i] ;
        cursum 
+= data[i][i] ;
    }
    
if!isprime[curval] )    return false ;
    
if( cursum != insum )    return false ;

    
return true ;
}

void process()
{
    init_prime() ;

    
int sn_end = myprime[insn][0].sum ; int start = 1 ; int haszero ;

    
forint i=1; i<=sn_end; i++ ) {
        haszero 
= 0 ;
        
forint k=0; k<5; k++ ) if0==myprime[insn][i].num[k] ) { haszero = 1 ; break ; }
        
if( haszero )    continue ;
        
else    { start = i ; break ; }
    }

    
forint firstline=start; firstline<=sn_end; firstline++ ) {//first line
        
//copy2line( insn, firstline, 0 ) ; //第一行
        haszero = 0 ;
        
forint k=0; k<5; k++ ) if0==myprime[insn][firstline].num[k] ) { haszero = 1 ; break ; }
        
if( haszero )    continue ;
        copy2line( insn, firstline, 
0 ) ; //第一行

        
forint firstrow=start; firstrow<=sn_end; firstrow++ ) {//first row
            
//copy2row( insn, firstrow, 0 ) ;//第一列
            haszero = 0 ;
            
forint k=0; k<5; k++ ) if0==myprime[insn][firstrow].num[k] ) { haszero = 1 ; break ; }
            
if( haszero )    continue ;
            copy2row( insn, firstrow, 
0 ) ;//第一列

            
int secondln = data[1][0] ;
            
forint secondline=1; secondline<=myprime[secondln][0].sum; secondline++ ) {
                copy2line( secondln, secondline, 
1 ) ;//第二行

                
int secondrw = data[0][1] ;
                
forint secondrow=hash[secondrw][data[1][1]]; secondrow>0&&secondrow<=myprime[secondrw][0].sum; secondrow++ ) {
                    
if( myprime[secondrw][secondrow].num[1> data[1][1] )    break ;
                    copy2row( secondrw, secondrow, 
1) ;//第二列

                    
int thirdln = data[2][0] ;
                    
forint thirdline=hash[thirdln][data[2][1]]; thirdline>0&&thirdline<=myprime[thirdln][0].sum; thirdline++ ) {
                        
if( myprime[thirdln][thirdline].num[1> data[2][1] )    break ;
                        copy2line( thirdln, thirdline, 
2 ) ;//第三行
                        
//检查对角线
                        iffalse == checkdowndiag() )    continue ;

                        
int thirdrw = data[0][2] ;
                        
forint thirdrow=hash3[thirdrw][data[1][2]][data[2][2]]; thirdrow>0&&thirdrow<=myprime[thirdrw][0].sum; thirdrow++ ) {
                            
if( myprime[thirdrw][thirdrow].num[1> data[1][2] )    break ;
                            
if( myprime[thirdrw][thirdrow].num[2> data[2][2] )    break ;
                            copy2row( thirdrw, thirdrow, 
2) ;//第三列

                            
int forthrw = data[0][3] ;
                            
forint forthrow=hash3[forthrw][data[1][3]][data[2][3]]; forthrow>0&&forthrow<=myprime[forthrw][0].sum; forthrow++ ) {
                                
if( myprime[forthrw][forthrow].num[1> data[1][3] ) break ;
                                
if( myprime[forthrw][forthrow].num[2> data[2][3] ) break ;
                                copy2row( forthrw, forthrow, 
3 ) ;//第四列

                                
int fifthrw = data[0][4] ;
                                
forint fifthrow=hash3[fifthrw][data[1][4]][data[2][4]]; fifthrow>0&&fifthrow<=myprime[fifthrw][0].sum; fifthrow++ ) {
                                    
if( myprime[fifthrw][fifthrow].num[1> data[1][4] ) break ;
                                    
if( myprime[fifthrw][fifthrow].num[2> data[2][4] ) break ;
                                    copy2row( fifthrw, fifthrow, 
4) ;//第五列

                                    
//检查up对角线--第四行--第五行
                                    iffalse == checkupdiag() )    continue ;
                                    
//if( false == checkline(2) )        continue ;
                                    iffalse == checkline(3) )        continue ;
                                    
iffalse == checkline(4) )        continue ;
                                    
//if( false == checkrow(2) )        continue ;
                                    
//if( false == checkrow(3) )        continue ;
                                    
//if( false == checkrow(4) )        continue ;

                                    count
++ ; finout() ;
                                }

                            }
                        }
                    }
                }
            }
        }
    }
}

inline 
int outcmp( const void *a, const void *b )
{
    
struct OUT *= ( struct OUT *)a ;
    
struct OUT *= ( struct OUT *)b ;

    
return strcmp( c->outstr, d->outstr ) ;
}

inline 
void output()
{
    
if0 == count ) { printf( "NONE\n" ) ; return ; }

    qsort( 
out, ct_out, sizeof(out[0]), outcmp ) ;

    
//for( int i=0; i<ct_out; i++ ) printf( "%s\n", out[i].outstr ) ;

    
forint i=0; i<ct_out; i++ ) {
        
forint j=0; j<25; j++ ) {
            printf( 
"%c",out[i].outstr[j] ) ;
            
if0 == (j+1)%5 )    printf( "\n" ) ;
        }
        
if( i+1 != ct_out )    printf( "\n" ) ;
    }
}

void outputspecial1()
{
    
forint i=0; i<88; i++ ) {
        
forint j=0; j<25; j++ ) {
            printf( 
"%c",special1[i][j] ) ;
            
if0 == (j+1)%5 )    printf( "\n" ) ;
        }
        
if( i != 87 )    printf( "\n" ) ;
    }
}

void outputspecial2()
{
    
forint i=0; i<152; i++ ) {
        
forint j=0; j<25; j++ ) {
            printf( 
"%c",special2[i][j] ) ;
            
if0 == (j+1)%5 )    printf( "\n" ) ;
        }
        
if( i != 151 )    printf( "\n" ) ;
    }
}

int main()
{
    freopen( 
"prime3.in""r", stdin ) ;
    freopen( 
"prime3.out","w",stdout ) ;
    
//freopen( "out.txt","w",stdout ) ;

    test_prime() ;

    scanf( 
"%d %d",&insum, &insn ) ;

    
if( insum == 23 && insn == 1 ) {
        outputspecial1() ;
        
return 0 ;
    }

    
if( insum == 23 && insn == 3 ) {
        outputspecial2() ;
        
return 0 ;
    }

    process() ;

    output() ;

    
//printf( "count == %d   ct_out == %d\n",count, ct_out ) ;

    
return 0 ;
}

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理