2018年东北农业大学春季校赛 B wyh的矩阵【找规律】

链接:https://www.nowcoder.com/acm/contest/93/B
来源:牛客网

题目描述

给你一个n*n矩阵,按照顺序填入1到n*n的数,例如n=5,该矩阵如下

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

现在让你连接相邻两条边的中点,然后只保留他们围成封闭图形区域的数字,那么这个矩阵变为

 

 

3

 

 

 

7

8

9

 

11

12

13

14

15

 

17

18

19

 

 

 

23

 

 

现在你们涵哥让你求变化后的矩阵的所有元素的和为多少

输入描述:

输入第一行一个整数T(1<=T<=100)
接下来有T组测试数据,每组测试数据输入一个整数n(3<=n<=10000)
保证输入的n为奇数

输出描述:

对于每组测试数据,输出对应答案
示例1

输入

2
3
5

输出

25
169
【分析】:不能数组存然后一行行相加,数组存不下会MLE。。。
#include<bits/stdc++.h>
 
using namespace std;
 
#define ll long long
#define N 10005
int a[N][N], sum;
int main()
{
    int t;
    int n;
    cin>>t;
    while(t--)
    {
        sum = 0;
        int tot = 1;
        cin >> n;
        for(int i = 1; i<=n; i++){
            for(int j=1; j<=n; j++){
                a[i][j] = tot++;
            }
        }
        int mid = n / 2 + 1; // 4列
        //3~5 2~6(mid-- n-mid+1)
        for(int i=1; i<=n; i++){
            int t = mid;
            for(int j=1; j<=n; j++){
                if(j<t || j>n-t+1){
                      a[i][j] = 0;
                }
            }
            if(i<=mid) mid--;
            else mid++;
        }
/*
        for(int i = 1; i<=n; i++){
            for(int j=1; j<=n; j++){
                printf("%2d ",a[i][j]);
            }
            cout<<endl;
        }
*/
        for(int i = 1; i<=n; i++){
            for(int j=1; j<=n; j++){
               sum += a[i][j];
            }
        }
        cout<<sum<<endl;
    }
}
/*
 
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49
 
*/
MLE代码

后来尝试打表,把3~10000的输入再文件流输出答案,这下空间复杂度10000 时间O(1)总行了吧?然而更惨直接WA,不知道什么毛病

#include<bits/stdc++.h>
 
using namespace std;
 
#define ll long long
#define N 10005
ll a[N][N];
ll sum;
ll ans[]={25,169,234,800,975,2419,2788,5734,6405,11645,12750,21244,22939,35815,38280,56834,60273,85969,90610,125080,131175,176219,184044,241630,251485,323749,335958,425204,440115,548815,566800,697594,719049,874745,900090,1083664,1113343,1327939,1362420,1611350,1651125,1937869,1983454,2311660,2363595,2737079,2795928,3218674,3285025,3761185,3835650,4369544,4452759,5048875,5141500,5804494,5907213,6641909,6755430,7566820,7691875,8585119,8722464,9702890,9853305,10926409,11090698,12262144,12441135,13716755,13911300,15297094,15508069,17010205,17238510,18863324,19109883,20863879,21129640,23019490,23305425,25337969,25645074,27827320,28156615,30495739,30848268,33351614,33728445,36403525,36805750,39660244,40088979,43130735,43587120,46824154,47309353,50749849,51265050,54917360,55463775,59336419,59915284,64016950,64629525,68969069,69616638,74203084,74886955,79729495,80451000,85558994,86319489,91702465,92503330,98170984,99013623,104975819,105861660,112128430,113058925,119640469,120617094,127523780,128548035,135790399,136863808,144452554,145576665,153522665,154699050,163013344,164243599,172937395,174223140,183307814,184650693,194137789,195539470,205440700,206902875,217230119,218754504,229519810,231108145,242323729,243977778,255656024,257377575,269531035,271321900,283963294,285825309,298967525,300902550,314558644,316568563,330751759,332838480,347562170,349727625,365005369,367251514,383097040,385425855,401853059,404266548,421289494,423789685,441422605,444011550,462268844,464948619,483844855,486617560,506167474,509035233,529253729,532218690,553120840,556185175,577786219,580952124,603267470,606537165,629582389,632958118,656748964,660232995,684785375,688380000,713709994,717417529,743541385,747364170,774298304,778238703,805999699,810060100,838664710,842847525,872312669,876620334,906963100,911398075,942635719,947200488,979350434,984047505,1017127345,1021959250,1055986744,1060956039,1095949115,1101058380,1137035134,1142286973,1179265669,1184662710,1222661780,1228206675,1267244719,1272940144,1313035930,1318884585,1360057049,1366061658,1408329904,1414493215,1457876515,1464201300,1508719094,1515208149,1560880045,1567536190,1614381964,1621208043,1669247639,1676246520,1725500050,1732674625,1783162369,1790515554,1842257960,1849792695,1902810379,1910529628,1964843374,1972750125,2028380885,2036478150,2093447044,2101737859,2160066175,2168553600,2228262794,2236949913,2298061609,2306951530,2369487520,2378583375,2442565619,2451870564,2517321190,2526838405,2593779709,2603512398,2671966844,2681918235,2751908455,2762081800,2833630594,2844029169,2917159505,2927786610,3002521624,3013380583,3089743579,3100837740,3178852190,3190184925,3269874469,3281449174,3362837620,3374657715,3457769039,3469837968,3554696314,3567017545,3653647225,3666224250,3754649744,3767486079,3857732035,3870831220,3962922454,3976288053,4070249549,4083885150,4179742060,4193651275,4291428919,4305615384,4405339250,4419806625,4521502369,4536254338,4639947784,4654988055,4760705195,4776037500,4883804494,4899432589,5009275765,5025203430,5137149284,5153380323,5267455519,5283993760,5400225130,5417074425,5535488969,5552653194,5673278080,5690761135,5813623699,5831429508,5956557254,5974689765,6102110365,6120573550,6250314844,6269112699,6401202695,6420339240,6554806114,6574285393,6711157489,6730983570,6870289400,6890466375,7032234619,7052766604,7197026110,7217917245,7364697029,7385951478,7535280724,7556902675,7708810735,7730804400,7885320794,7907690409,8064844825,8087594650,8247416944,8270551263,8433071459,8456594580,8621842870,8645759125,8813765869,8838079614,9008875340,9033590955,9207206359,9232328248,9408794194,9434326785,9613674305,9639622050,9821882344,9848249719,10033454155,10060245660,10248425774,10275645933,10466833429,10494486790,10688713540,10716804675,10914102719,10942636224,11143037770,11172018265,11375555689,11404987818,11611693664,11641582095,11851489075,11881838500,12094979494,12125794629,12342202685,12373488270,12593196604,12624957403,12847999399,12880240200,13106649410,13139375025,13369185169,13402400434,13635645400,13669355175,13906069019,13940278188,14180495134,14215208605,14458963045,14494185750,14741512244,14777249139,15028182415,15064438480,15319013434,15355793673,15614045369,15651354810,15913318480,15951162175,16216873219,16255256244,16524750230,16563677685,16836990349,16876467358,17153634604,17193666315,17474724215,17515315800,17800300594,17841457249,18130405345,18172132290,18465080264,18507382743,18804367339,18847250620,19148308750,19191778125,19496946869,19541007654,19850324260,19894981795,20208483679,20253743328,20571468074,20617335225,20939320585,20985800650,21312084544,21359182959,21689803475,21737525700,22072521094,22120872613,22460281309,22509267630,22853128220,22902754875,23251106119,23301378664,23654259490,23705183505,24062633009,24114214098,24476271544,24528515335,24895220155,24948132300,25319524094,25373110269,25749228805,25803494710,26184379924,26239331283,26625023279,26680665840,27071204890,27127544425,27522970969,27580013274,27980367920,28038118815,28443442339,28501907668,28912241014,28971426645,29386810925,29446722750,29867199244,29927843179,30353453335,30414835320,30845620754,30907746753,31343749249,31406625250,31847886760,31911518775,32358081419,32422475484,32874381550,32939543725,33396835669,33462772038,33925492484,33992209155,34460400895,34527904000,35001609994,35069905689,35549169065,35618263530,36103127584,36173027023,36663535219,36734245860,37230441830,37301969925,37803897469,37876249294,38383952380,38457134235,38970656999,39044675208,39564061954,39638922865,40164218065,40239928050,40771176344,40847741799,41384987995,41462415340,42005704414,42084000093,42633377189,42712547670,43268058100,43348109875,43909799119,43990738704,44558652410,44640486345,45214670329,45297405178,45877905424,45961547775,46548410435,46632966900,47226238294,47311715509,47911442125,47997846750,48604075244,48691413963,49304191159,49392470680,50011843570,50101070625,50727086369,50817267714,51449973640,51541116055,52180559659,52272669948,52918898894,53011983885,53665046005,53759112550,54419055844,54514110819,55180983455,55277033760,55950884074,56047936633,56728813129,56826874890,57514826240,57613904175,58308979219,58409080324,59111328070,59212459365,59921928989,60024097518,60740838364,60844051195,61568112775,61672377000,62403808994,62509131729,63247983985,63354372370,64100694904,64208156103,64961999099,65070540300,65831954110,65941582525,66710617669,66821340534,67598047700,67709872275,68494302319,68607235888,69399439834,69513489705,70313518745,70428692250,71236597744,71352902239,72168735715,72286178580,73109991734,73228580373,74060425069,74180166910,75020095180,75140997675,75989061719,76111132344,76967384530,77090630785,77955123649,78079553058,78952339304,79077959415,79959091915,80085910300,80975442094,81103466349,82001450645,82130688390,83037178564,83167637443,84082687039,84214374720,85138037450,85270961625,86203291369,86337459754,87278510560,87413930895,88363756979,88500437028,89459092774,89597040325,90564580285,90703803150,91680282044,91820788059,92806260775,92948057800,93942579394,94085675313,95089301009,95233703730,96246488920,96392206375,97414206619,97561246764,98592517790,98740888605,99781486309,99931195798,100981176244,101132232435,102191651855,102344062800,103412977594,103566751369,104645218105,104800362810,105888438224,106044961983,107142702979,107300613940,108408077590,108567383925,109684627469,109845337374,110972418220,111134539915,112271515639,112435057368,113581985714,113746955745,114903894625,115070301250,116237308744,116405160279,117582294635,117751599420,118938919054,119109685453,120307248949,120479485350,121687351460,121861066275,123079293919,123254495584,124483143850,124659840825,125898968969,126077169738,127326837184,127506550255,128766816595,128948050500,130218975494,130401738789,131683382365,131867683630,133160105884,133345953723,134649214919,134836617960,136150778530,136339745425,137664865969,137855405394,139191546680,139383667335,140730890299,140924600908,142282966654,142478275965,143847845765,144044762550,145425597844,145624130899,147016293295,147216451440,148620002714,148821794793,150236796889,150440231770,151866746800,152071833375,153509923619,153716670804,155166398710,155374815445,156836243629,157046338878,158519530124,158731312875,160216330135,160429809400,161926715794,162141900609,163650759425,163867658850,165388533544,165607156663,167140110859,167360466780,168905564270,169127662125,170684966869,170908815814,172478391940,172704001155,174285912959,174513291648,176107603594,176336760985,177943537705,178174483050,179793789344,180026531919,181658432755,181892981860,183537542374,183773907333,185431192829,185669382990,187339458940,187579483675,189262415719,189504284424,191200138370,191443860465,193152702289,193398287218,195120183064,195367640295,197102656475,197351995500,199100198494,199351428829,201112885285,201366016470,203140793204,203395834803,205183998799,205440960400,207242578810,207501470025,209316610169,209577440634,211406170000,211668949375,213511335619,213776073588,215632184534,215898890805,217768794445,218037478750,219921243244,220191915339,222089609015,222362278680,224273970034,224548647073,226474404769,226751099010,228690991880,228969713175,230923810219,231204568444,233172938830,233455743885,235438456949,235723318758,237720444004,238007372515,240018979615,240307984800,242334143594,242625235449,244666015945,244959204490,247014676864,247309972143,249380206739,249677618820,251762686150,252062225125,254162195869,254463871854,256578816860,256882639995,259012630279,259318610728,261463717474,261771865425,263932159985,264242485650,266418039544,266730553159,268921438075,269236149900,271442437694,271759358013,273981120709,274300259830,276537569620,276858937875,279111867119,279435474864,281704096090,282029953705,284314339609,284642457498,286942680944,287273069535,289589203555,289921873300,292253991094,292588952469,294937127405,295274390910,297638696524,297978272683,300358782679,300700682040,303097470290,303441703425,305854843969,306201421474,308630988520,308979921015,311425988939,311777287068,314239930414,314593604845,317072898325,317428959750,319924978244,320283437379,322796255935,323157123520,325686817354,326050104153,328596748649,328962465450,331526136160,331894293775,334475066419,334845675684,337443626150,337816697925,340431902269,340807447438,343439981884,343818011355,346467952295,346848477000,349515900994,349898931889,352583915665,352969463730,355672084184,356060160423,358780494619,359171110060,361909235230,362302400925,365058394469,365454121494,368228060980,368626360435,371418323599,371819206608,374629271354,375032749065,377860993465,378267077050,381113579344,381522279999,384387118595,384798447540,387681701014,388095669493,390997416589,391414035870,394334355500,394753636875,397692608119,398114562904,401072265010,401496904545,404473416929,404900752578,407896154824,408326197975,411340569835,411773331900,414806753294,415242245709,418294796725,418733030950,421804791844,422245779363,425336830559,425780582880,428891004970,429337533625,432467407369,432916723914,436066130240,436518246255,439687266259,440142193348,443330908294,443788658085,446997149405,447457733550,450686082844,451149513019,454397802055,454864089960,458132400674,458601558033,461889972529,462362011090,465670611640,466145543175,469474412219,469952248524,473301468670,473782221565,477151875589,477635556918,481025727764,481512349395,484923120175,485412694000,488844147994,489336685929,492788906585,493284420570,496757491504,497255993503,500749998499,501251500500,504766523510,505271037525,508807162669,509314700734,512872012300,513382586475,516961168919,517474791288,521074729234,521591411905,525212790145,525732545250,529375448744,529898288439,533562802315,534088738780,537774948334,538303993773,542011984469,542544151110,546274008580,546809308675,550561118719,551099564544,554873413130,555415016985,559210990249,559755764458,563573948704,564121905615,567962387315,568513539300,572376405094,572930764549,576816101245,577373680590,581281575164,581842386843,585772926439,586336982920,590290254850,590857568625,594833660369,595404243954,599403243160,599977109095,603999103579,604576264428,608621342174,609201810525,613270059685,613853848150,617945357044,618532478259,622647335375,623237802000,627376095994,627969920713,632131740409,632728935930,636914370320,637514949375,641724087619,642328062964,646560994390,647168378805,651425192909,652035999198,656316785644,656931026635,661235875255,661853563800,666182564594,666803713569,671156956705,671781579010,676159154824,676787263383,681189262379,681820870140,686247382990,686882502925,691333620469,691972265574,696448078820,697090262115,701590862239,702236596768,706762075114,707411373945,711961822025,712614698250,717190207744,717846674479,722447337235,723107407620,727733315654,728397002853,733048248349,733715565550,738392240860,739063201275,743765398919,744440015784,749167828450,749846115025,754599635569,755281605138,760060926584,760746592455,765551807995,766241183500,771072386494,771765484989,776622768965,777319603830,782203062484,782903647123,787813374319,788517722160,793453811930,794161936425,799124482969,799836397594,804825495280,805541213535,810556956899,811276492308,816318976054,817042342165,822111661165,822838871550,827935120844,828666189099,833789463895,834524403640,839674799314,840413624193,845591236289,846333959970,851538884200,852285520375,857517852619,858268415004,863528251310,864282753645,869570190229,870328646278,875643779524,876406203075,881749129535,882515534400,887886350794,888656750809,894055554025,894829963050,900256850144,901035282063,906490350259,907272818980,912756165670,913542685125,919054407869,919844992014,925385188540,926179851355,931748619559,932547375048,938144812994,938947675185,944573881105,945380864050,951035936344,951847054119,957531091355,958346358060,964059458974,964878888733,970621152229,971444759190,977216284340,978044082675,983844968719,984676972624,990507318970,991343542665,997203448889,998043906618,1003933472464,1004778178495,1010697503875,1011546472500,1017495657494,1018348903029,1024328047885,1025185584670,1031194789804,1032056632203,1038095998199,1038962160600,1045031788210,1045902285025,1052002275169,1052877120834,1059007574600,1059886783575,1066047802219,1066931388988,1073123073934,1074011053005,1080233505845,1081125891750,1087379214244,1088276021539,1094560315615,1095461558880,1101776926634,1102682620473};
int main()
{
    int t;
    int n;
    cin>>t;
    while(t--)
    {
        cin>>n;//3-2=1 5-3=2
        cout<<ans[(n-1)/2-1]<<endl;
    }
    return 0;
}
WA

 

最后找规律过的,发现对称位置总是在1~mid递增后mid~n递减。。。很惨很菜

#include<bits/stdc++.h>
 
using namespace std;
#define ll long long
#define N 10005
 
int t;
int n;
int main()
{
    cin>>t;
    while(t--)
    {
        ll sum = 0, j = 0;
        cin>>n;
        ll mid = (n+1)/2;
        ll m = mid;
        for(ll i=1; i<=mid; i++)
        {
            sum += m*(2 * j + 1);
            j++;
            m += n;
        }
        j-=2;
        for(ll i=mid+1; i<=n; i++)
        {
            sum += m*(2 * j + 1);
            j--;
            m += n;
        }
        cout<<sum<<endl;
    }
}
找规律

【总结】:这种空间复杂度很大,涉及矩阵的SB题目似乎找规律是很司空见惯的事情,上次EOJ的蛇形矩阵也是找规律,被我模拟一下也挂了,18蓝桥杯那个螺旋矩阵也tm是找规律,上次的HDU幻方也是找规律。。。真实的哭了,以后看到矩阵我先找规律在考虑模拟满意了吗

posted @ 2018-04-05 23:15  Roni_i  阅读(321)  评论(0编辑  收藏  举报