HDU 5950 Recursive sequence【矩阵快速幂||分块】

题意: f [ n ] = f [ n − 1 ] + 2 ∗ f [ n − 2 ] + i 4 f[n] = f[n - 1] + 2*f[n - 2]+i^4 f[n]=f[n1]+2f[n2]+i4,给你 f [ 1 ] , f [ 2 ] f[1],f[2] f[1],f[2],让你求 f [ n ] ? f[n]? f[n]?
分析:
i 4 i^4 i4拆开得到关于常数的一个递推式,如下图:
在这里插入图片描述
PS:之后遇到后面带常数或者i的矩阵都可以这么解。。。
我的写法:
比较暴力,分块离线处理 i 4 i^4 i4,需要保存(i - 1)和(i - 2)的两个常数,才能推后面的常数部分;
把常数部分处理出来,之后的套矩阵快速幂就行了;

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const LL mod = 2147493647;
const int INF = 0x3f3f3f3f;
const LL MAXN = (1ll << 31) + 5;
LL p[][2] = {0,0,2042983467,150355877,1878320810,832116640,1493365967,1989070424,117051223,2115493600,77549503,1381500758,214651683,1342761211,1355502732,859515750,8214045,663750486,1836579336,243717578,365130337,351413218,533757555,719465264,292674742,1905947826,1267770143,855159998,988262649,1122861928,497484121,180422055,2025045862,1939099229,1616658753,2009682983,1706036924,2108832640,1567580409,1701910858,1015907637,1107061318,396681295,1476860450,903694883,1137503249,1746735788,661997658,1457050928,1186108562,1812993782,1522183613,1801541308,1114642595,822702165,6312927,879890645,1071683880,129058332,1410157189,664951167,1476080242,411558992,1744607050,954006492,1195530955,1710626664,689887097,2077191613,815933065,1665798529,219427204,1630529557,402442002,202840223,1089128236,1348772029,360190986,2099069073,1525527580,2065758661,2092477760,718896219,1204275388,1348016126,1120469171,85070443,295255648,499525135,1092735164,1478304541,579256339,697169433,1761159480,1742248178,2057915141,57273990,1703990626,1963300118,985901072,2022621698,67419740,955316384,462760925,1957811261,1968701076,960954355,360670992,1210998481,1522267989,1040599360,1282239428,1436851307,550554091,1733827999,998471011,184293582,1906974392,940584961,1243576307,474297418,714092639,808841267,15351686,1870715527,865184330,226475569,181372037,787978440,607175623,57609950,624219545,1630088251,1933668942,1337390792,1221020015,941548946,361740105,200381932,1290203772,743816749,867370781,1347800971,756054515,808365525,284085649,1544571044,1502711209,2081496110,595051999,662157685,1542424404,45910203,2098708457,755059897,1758512963,1075602798,1758651497,677324198,1580873095,392618475,656995551,456877228,1292162628,805742993,669376902,1200069720,1836888951,1490951461,1353292849,1417252355,1767548018,1821565286,1165934662,752701216,409487962,1781862043,438900969,1458151558,807976635,2102225667,1648498140,1892545918,1022685122,1356949487,1463852708,73722185,1231063881,1832178590,335302450,487082227,165730821,1936235282,200009191,1407393200,1970478625,1772451581,595168261,1985767677,309323950,1737447595,149552545,664561500,966226895,426726880,984677787,1303151189,1884241114,1950306782,1871126804,1575820908,26198806,1157918447,891354211,1221717783,1432140410,1911870893,1920503507,1641363958,1084895894,1706625970,1045506171,1157901244,1349991464,388864037,1857719369,381954201,1230439258,2015117423,1988243700,1697369077,1780696165,1877535836,1519352366,864041084,49786612,964388690,1840584768,1066449269,2145916777,1257607808,538819206,1791890038,992941637,1340973340,1942087538,1695352034,1387557547,291013657,1278944851,1255782692,1689405233,282233439,626393872,1008065448,1022492445,1252470784,388092833,1556190125,1449028376,1482120214,452801483,2002361579,532653660,738001268,39637333,1932055861,1751033637,976816108,1252484515,209222800,1348795284,975341801,487871993,1852303522,2089562253,867655565,1804411349,1937787969,390512732,351516534,450598431,719553180,1528600478,925122265,223288249,818342338,1134551029,916339553,1378662958,2176028,818595278,1535083628,1999522017,2002589014,1885525616,963578174,1208257756,744359041,1711570947,665904843,1048486169,1074153967,841792600,534697584,1073422866,897614881,1624148917,88775363,741148513,4429484,89718251,1455079129,1083495470,1466055466,1633107602,1803262476,312697350,520671608,929848468,1286951435,1113934015,109978731,125036861,659550501,1581956336,202066877,364029551,64588727,1391752969,1836004060,1291646980,2009558421,1303746808,244531293,182525198,1374578747,361071363,1379060894,1368682358,801914097,898683740,2086308189,215212239,2028828264,1946541222,682834290,2121876155,795344076,1930082419,178870970,1053335294,833483252,878783216,1756297618,855002980,32924580,674890540,1261751535,1796784324,421579647,1392143703,54518167,793920220,1559549051,968648708,462458359,1646284676,179682552,930479492,1664701350,361289863,1286941380,672985131,1416505987,1488570425,1369742163,390869695,1922567159,1949178936,6706703,669261683,1365316529,2009604775,1862543014,32995158,157855803,607691488,700680663,1626766075,1764152335,1288015292,155675850,1072601266,1609477690,760230436,502593470,811001857,220235876,146270136,542297501,986562162,677885116,2035683766,1710632481,1223851696,1117193228,1924628559,1984228045,1323986131,863434546,490263449,1095244845,956650901,940101119,311184175,15582505,776010759,392504070,662119646,1485654213,1330765314,2125697842,599805845,278881806,773088377,563318269,50585735,686898633,1911616276,801654754,1611161616,336583374,1320451964,76386141,1399484280,155749612,680569575,1218667302,934470656,183228814,547474033,838372738,682440188,11207143,1235874676,605173761,1144332729,1219529961,1245813321,1959218752,867043645,667444960,1026099702,2133909606,1373938587,923516340,211709709,618693052,762993308,1526589680,1620651600,434747346,553154343,749770191,1896960625,1505821584,1015992223,198982079,707817752,1272848400,257433224,706599155,1120785475,101390473,737665087,1008644978,515727159,429101171,1123577365,1876938400,996591777,1703378273,1196524396,1217581594,1735883613,314064420,1717726205,1736232667,1420245606,459395020,390240550,1859447846,1568080192,2128451016,36273163,1159145777,198528726,1328598704,462082798,628283443,972797906,1644677380,101420055,2046362936,1187705082,1309854408,222148574,966081055,1371407569,2107085848,1254229821,1535502059,567849801,1358118182,1800182069,1357584626,415715601,2070468116,1749090826,1515081953,1575798264,1521123018,918946181,1679631223,391660432,1738016345,692150835,401950949,207497017,728700232,1505320808,993673002,1656862787,1863873017,141404019,834454338,604338643,582696795,1795201040,604944022,168984726,1583805471,768040027,331375259,666030832,1532439659,1716881340,31624260,898994144,683130541,2097305968,874796795,478852338,1161357707,1611793091,1983328976,2018219737,875841862,771773729,631514987,790934536,1289939958,1407965477,1235738706,565028399,1685278478,1640120014,702810959,1479350143,1500750325,555016881,2129623460,1672949854,1759624656,1060683149,851001909,1593189788,1201433553,2673114,1465822249,1540584886,800457457,1496026459,142423676,639527146,821493391,299336465,1506274286,548705859,387636314,277755542,1973008952,44600774,459725820,1034445804,2124834311,446849249,600810634,29482649,727537628,94744920,718787547,360119923,254593456,1100463727,406884670,851781840,513719765,897163466,1858622243,1887984600,1542660580,178042712,1732924417,2025521835,2036693365,1898849234,779507489,65291903,1709588115,967342650,1603956829,1319919977,74158861,1131840260,371612285,1021528855,1547133148,1568525885,1933823088,1689286888,5760485,354565119,1025698965,1294833970,570151932,1525392504,575147182,1962938213,1421993854,187075508,959880682,1370474105,637609609,477425195,1112350349,1398767979,747792197,1063231406,1123371018,455888151,1986080171,964257404,846378147,1422626499,383295726,37303346,803440204,579561355,642151816,1102990400,468684065,1353857477,465818981,77830869,698211264,1999149373,751799880,2066989963,1241308736,1355431022,1652769152,601003639,45204213,1753553960,430080165,1260209510,220951080,1472060522,836170631,1784672429,1634580324,1541917857,1266962954,591389890,2130975782,1751787483,1794193502,101482337,860374404,858544872,1417811411,1244990772,1842148170,1567349637,1982527047,556505444,438154133,658294794,726752774,1042971149,127433667,1060868788,1963255208,1185093386,918202430,1562897301,658668532,751250633,845133565,1952603301,1657676363,590972435,558052520,1458528447,218735256,213778411,256036490,1207706561,1882578094,1018793035,1103370385,615094623,402883786,810970678,940706282,1690853557,800445571,1254546570,565232522,125749955,1651898304,367908783,143515952,1739270678,1354079150,757332325,583846855,1601882599,206600022,897162134,1356196105,1149883383,15339724,779278305,1395829643,1402655851,2086986,174863101,401512925,1264089582,98285670,926904071,1066815134,1985189337,1476114665,1569872899,2093279406,1780390099,287557673,1987814152,800229167,1047169747,1443243841,907115486,681341151,299245916,1521886151,370270072,485634113,1950983540,1919130712,1968664122,1321651520,903330117,1779135502,1295708961,385382992,2064569649,403397914,1354470707,862591241,1500090874,909339979,547397894,1882319415,699300306,1080916047,147460598,433463386,1818792223,109345833,599667353,1742278316,519944388,747737427,1535323877,2100125203,1016765022,1406191332,994696677,273037030,240139456,33352796,1042624066,1980222156,1089824207,1220665807,502087683,56039156,675814069,941738403,940335112,1946944098,214870080,662889377,530074940,435868100,166615811,1083646035,857915096,1437280036,2136321603,387996453,1279373718,120783447,1924477682,1923132648,1391403613,1766555221,1172760461,118294782,787498076,1600968901,1395177508,1364008318,13785909,125070556,275120936,1352669315,347568598,275345382,1019356134,155847455,1739521662,297428371,252936752,445610181,1403836576,1671195882,1721894196,1284115424,724849048,961715795,2142631583,1386076179,1044499936,1092134352,862735541,284259461,1698345866,1566494867,1781550099,1894110844,702327369,1453836621,2138425301,1209633781,356932226,318197697,815203817,955529691,1896589996,1836975382,1277954453,1591403938,530539503,1982637813,825357325,1145947718,143648139,1939920121,1390640618,1300654657,1841414943,1779538745,1183996508,271566962,450931138,342177740,903034719,864214944,630759177,550131771,1471106629,391920976,1002527953,1115004102,337663130,2060830886,1888540056,2121928848,499205485,597417683,189209342,424802139,1289747656,686841339,489856733,1431170866,475657326,212548465,867352392,1111567519,1224198515,1708601700,762318972,510774963,323681839,831633257,1545468825,956569936,75887976,1452485014,193492185,289936676,756494920,1878867983,389261752,1894336555,1493668073,1552202809,145589926,1358935702,1511932902,353829551,1155116604,474633307,206814136,307286085,1457397751,625579398,1107820496,1709871218,586993510,1526036296,762193017,1229616699,62399518,1128666421,1436442542,1327305520,755147038,1749584051,1891516770,703564342,963936987,756640501,234307084,26910334,1428252673,726392241,1346622905,2072040305,1309950765,1361358301,719706502,1809875946,1995781894,794808607,2096265193,1940973925,1656818844,1053120268,477766508,2118015163,1610852515,1099546887,424381516,1712341293,1117996606,2127820999,872265937,553649598,1007571138,1664419044,518039264,847693725,1071022941,1404852838,606405823,1961228786,369284091,960226904,1013244023,1514144798,1564532437,592172295,1420486708,553850684,1941516780,1670165412,83265146,1389152391,1960274991,1780392359,1227586915,157502417,1128101300,691923181,1212760615,1085512201,750647312,110954165,924842352,395758888,1810446595,1207419709,829859476,504764132,1150467581,1427373457,1041053792,1066262540,1478067865,1506443912,313337257,2116223615,475088915,493036682,1122152001,1720131407,1124870903,1285593400,1410545344,1375579510,1042506229,434455602,2110314963,1721086786,1072420145,1980965100,460311286,1198098802,464879982,1986510989,2014404446,297742531,1074517258,91056700,808665447,265271817,474231314,493139937,648808861,1314526886,1269070218,168687181,137796424,1410192568,70772621,726733314,1354820755,923586159,1104330775,794012149,1066967357,2057932291,26084744,1652624217,2004550191,1424208422,1287818089,2077143897,387832289,708929026,817440602,1986046397,1819854304,889546248,219713380,611277670,445625166,1256008850,524379423,1432852623,1706280382,1499115853,2067883624,1865411958,1712195718,1099164180,1123174408,2008620437,448796435,1947193289,1584673939,1244269730,1413243926,648415888,1722633197,1574350770,1110631250,1356408221,524069727,588422496,758644382,353559461,1213564777,1045713416,1829335222,270241049,1036716330,329407149,2137641855,1881799734,624048948,989785683,1895219538,194283320,457452560,1005305032,1412541190,1379260319,379923416,1941408243,236574230,317409762,1927603943,2124651365,678981691,1315566555,371519576,420984171,340228135,1550116503,996722687,1841242434,1173070558,941578282,196570883,484414556,1139015412,1844286400,2032042104,2037471370,553092563,1805159639,1601777860,218515063,67519724,438360465,938895698,1359018830,443827882,1278611864,1980154639,206350176,1402768136,1497356669,1030729361,606030118,38957161,1047692315,611598768,1699821605,1871939694,805729308,1447275634,436204480,97214694,368015835,208149470,1955069950,650611576,1255096845,212591980,1003517102,72338772,1330142936,264214835,525273462,1210888379,1279875128,1171238278,1704503,642523410,352599631,1517547954,1844373791,1456398086,1506359653,1516517585,290046103,1782032307,934019323,1388619639,156993041,89518175,618368287,701710758,707250877,1568921330,398791726,1831814617,271922944,1610961968,1255988973,1500103629,183222901,113582312,486913606,450221954,1135531787,646541661,1098137084,760076301,983435526,1752504106,514879588,439846786,201313952,1423735639,171247036,1874992727,859717609,1497338694,498855773,1803502024,1787967867,127946026,1629719858,1311640321,297493830,1676875468,1119082113,1279613104,1934082751,1856732409,742748752,943779430,1423522107,1162541472,2000096580,1787628207,1411334295,1548836613,677858347,964336196,1856771727,250106840,1669371472,1607822636,249555256,951741300,1328573327,1163169929,1799436878,1956749978,1812498778,744175899,1818252292,1752248110,725167646,1601077890,679637615,1011472202,244525983,1899498848,1445581676,2141871912,1656911083,821832872,612712389,1515151031,216765884,1171635900,301795368,961302831,104981144,635424415,843673683,1530464165,597442164,627160890,1705947898,1051288249,2079680516,1137608068,491699010,718365128,212407819,1950358396,666168846,1218139647,1906836992,232578179,1483837448,1910509044,1660516996,1189005533,1954309837,170621890,1613852809,192661866,315818763,1073351824,391668339,1270356997,235069029,1875568790,382128497,1595622244,1239127493,797336452,1949844367,1389857234,1901843871,1247260153,637019923,472116984,1569934627,242199166,813858702,1937495130,1212445322,843659627,899630334,1926699810,2035770986,797860691,997418580,600562446,333039723,1561944565,1141037929,781364585,1450002878,1875858490,1307686678,1535663118,139183637,1420524881,1757814520,1001519103,1034781462,965270904,1417328594,1742884453,996897885,1416178416,1633814874,1082095538,32630829,2035842490,1023728632,121696934,1953045791,1757313781,268610220,451911105,418532212,1339722324,1829080089,1431134794,349311603,1091927199,1747075457,970361535,143811902,1816657244,51399730,1311824966,36396030,51031697,1054947129,1033473495,1330647846,1565587635,1239477771,883431520,99338600,174238292,735336888,969514103,1063739531,1197910961,1594339279,1599983168,1734942037,484655569,574112103,1737194969,150530773,2030484520,992955449,1232384760,335042260,982872502,1141735609,1271883458,1056727328,1956660666,645735516,2068362990,1508161218,330365772,1440542417,517624345,217363889,1543357273,89516235,603458062,1224357503,1236567580,94411602,1511496297,240934628,521901408,731952151,1072285171,1955758136,1937549926,1182549303,1871228697,1534914400,366514562,1975708952,1254440268,54535653,1016889839,630708218,1937538986,358259438,399314040,295518764,1366078885,1876762272,1735960300,549052641,1237458032,1971959085,468986308,497449008,808530085,1540758398,1910525604,948454364,1244364757,101222100,677538898,526955879,2144301772,1522099373,740856508,2117702738,1542431671,800725626,1865938110,544367054,1330628696,678205554,630834151,86770239,1490624239,329537572,18839422,879483277,808624799,1739275960,694880878,1378975887,1809610308,1538913498,1860232084,1964566780,779285469,1221531832,206877477,124740402,1759143359,235392115,1874617412,1286393919,1697567400,980675919,232145281,1178991764,275507040,391717914,1991385811,888557077,2097043222,225459246,539541586,302747509,1432046345,345559677,1162202973,594613039,68653780,1845846405,1520321998,2098698400,917981075,72885471,1317785277,853561153,1099158103,557464203,1951974199,2093603737,1743043986,1973707146,349414197,996478049,1699440960,1385383487,1605883245,2138307956,2087641047,1100179175,482551393,1867361883,1108682289,1829159332,2084803941,17382032,292058010,1709754769,580771648,819863046,2025286850,1091208490,1657185622,493269074,1912296071,2117189630,209668726,2029314040,1166032919,438246572,275033811,955571409,1756114273,1365121651,93052890,1499126232,81245089,355411163,324637390,525240006,2091408354,318811474,7444724,2065803442,804923094,1402911985,1897387985,1292556158,89718582,256985253,258747953,1142069307,668439360,551014596,1902228305,1542030207,45950969,1149019578,384112938,2049491092,2080824745,1783392285,239900403,401513549,1201664811,1001288217,2139617454,1050639200,1625543088,1562474413,798553416,1571251379,908190884,1848167480,7340948,1773495500,1729358689,924105901,1283164189,452859883,1096990533,479604835,1305387417,1843956914,1107292659,1064546995,1569485981,89292103,1504701756,1518297960,802395984,1646423350,1546330551,1262537853,1695671286,275039071,1515971589,1006416140,1578324867,280198875,1965718353,1048079421,1904846291,1200719777,1010155101,1790690733,697265724,1275708431,1259421332,2099737614,2144731944,684461281,1266556559,188403416,1194305791,1236537786,1350127700,2070504688,456834933,374432572,189547970,680470558,659317335,548456006,689876147,913450379,1332762670,610438184,1359199136,317326232,851102971,1387724938,1014143042,651200384,220724724,837050349,1972580078,1287907269,622961065,1975060203,144751713,1287324345,1118232553,768883811,1616791605,625887262,1105077506,164935673,1315975048,1156016091,91510812,1527913754,248613876,321139153,157527116,1788369613,1942690368,184649239,390583143,283756754,360030186,1136413441,1478566536,1807613267,1027123186,838492542,1453740989,395726783,1492415210,932274135,517037897,494720647,1451075727,2089616069,1877811483,1673632274,85571956,2141993719,1569652397,809871971,437586510,1095681285,628243961,1106808365,1673264488,595399711,1871164360,508240824,1682082081,422988004,747735633,1259265442,1166123013,1836646984,607485761,788721822,437427636,1629174242,816618583,1520248289,1860154180,976370337,1569864308,1721844563,139678687,1307981199,2074845262,2016409891,1786152216,924963184,229086939,143577554,1753207975,1451164278,1228393115,1389837227,3218747,515551815,783807637,371385243,1580389417,1838172499,998547173,109984329,1075135269,1529811952,147269484,14704920,1537437554,1277796718,1867728256,1123137688,1336838431,709587766,2035453753,332022101,1951140908,1940165066,6015616,1363782531,1982063647,1515384694,1829179896,1721062711,1129220936,1977475861,430030172,765694553,1315604796,1825873301,627998914,1341560215,1343334527,1657124601,1351771085,707179585,233768146,1151168563,349309100,1153128012,1282388673,363361440,1066892592,722894260,2090494627,119445378,554093414,1627795907,657562589,1081631687,480904063,1351266140,1212250909,2023572374,1540187099,1572630209,1995041556,34923344,1709307900,315694659,1030355052,1117819895,263136476,1642035,1494871647,1119464270,441601866,1339060296,1645083794,2105272367,1393606265,2122867999,1005121105,1824190861,730089385,309589773,74820358,61307085,786767276,1823788233,910133796,898164150,259800684,636312332,1639524183,651283720,147751659,77118733,434721902,1731697302,1405735077,1733130936,1223860620,1524482513,2031933997,1094978256,689967950,952754966,1310621551,1447950119,1020418514,337629842,122058676,1174996768,1692884531,719758325,1438073337,2124536679,1178495120,1249229262,806876553,1436299984,2068459616,738486588,196365482,1423899996,774299562,1286118077,209245394,414174969,1418936934,63697358,836662879,2125642389,233704371,741820479,687848858,1618414336,408048839,719703845,1514225517,548636535,1796030440,561543418,1312200943,308623745,81555072,262273330,1887141879,1380107479,517325398,674632561,970038845,317180957,1153262517,372480305,976592399,300585691,1060117252,1752485103,1883148580,473899594,476782838,1619966035,1725824115,837017761,2007281896,1639375095,321521993,1371493929,281474765,263967138,307220535,642576242,634067502,1266825758,579595033,123316437,1982906490,116792368,1259280615,335251951,1995304534,1495520528,70095255,1387280200,313283066,450363373,1889674863,196844002,1585917579,1754490163,1634871563,976424540,2082425201,1412039764,959401830,1482766137,845499891,554298695,1686304232,1383368709,1391841816,956282651,1136089770,1559295,759819470,597016706,427251460,54922475,1281317804,215697547,408639625,1460213934,339059001,1736707355,656983304,387145138,1359133710,720098189,1557808196,1905116051,449580834,119776892,1756895414,2140493609,107298359,798481564,665170901,99776773,651968755,1508318892,567273737,578281706,1748442036,71803566,393660383,266587287,389550552,904816782,29053452,874160026,2110903290,172094528,688329339,1105918821,649028094,660862774,303890249,2074855542,1651574715,405995755,840891821,1510239591,356255696,24917955,1492110626,162609529,843310567,1885156658,1308036563,742315010,1027627831,517458364,1498042161,1535874514,1366396900,1204818255,1806062218,1927824337,870350546,1322483474,1845486005,598156369,2088615045,1480206294,1908727134,1618111523,1825360565,2091000361,399723338,1986844898,573029844,25479971,27576907,951979028,1826001865,621363090,1760649670,1700717486,430363535,1615553131,608143960,776624556,72354675,260745306,2000808965,1879332902,75404065,282571205,1345331534,658659249,1495752805,93073390,1253664658,1256554634,1551469171,925898030,2043488976,1697755295,911142392,263738022,1478060899,1192610487,1119589106,1242781181,690147108,1207875668,296958791,241435128,834410800,58912017,1650575624,1857328987,829010377,1251464357,93583269,102853861,549059691,1756730530,1744956567,1402404838,1723745469,300901117,1853695330,56289510,1814017291,2022758447,556503300,1882346357,493445065,611556758,1513446823,561246249,1731175912,179129149,1964827578,467060500,1470741805,1396871359,1512542134,860624341,1114153102,925915024,980428464,1799137472,13656827,262383103,777333974,1072836192,82549165,1279629387,571569109,1078140024,214025889,985933712,1691259661,180723244,482261888,1938840472,35606384,264409840,60251909,155955673,551364748,1455109102,1700137266,852710099,1463511037,2142852908,1039812177,2085333179,216075729,2018860152,1267027208,239644033,154738294,1377399480,985646510,1500697655,1253714855,2126937569,532931671,292464814,2092484577,249977220,1931673431,519051545,1535849356,994812047,618815414,420203349,657346924,1498767790,84290635,2024867370,171438733,272493009,774691830,267873453,141808541,1964215926,1031810552,1565427816,624957757,850660429,581861178,1314412497,1766170922,975932120,10873712,1518484193,396827741,546302891,1766667642,1496032210,1515230041,1289456311,959925303,2075915834,908152749,1481058054,1482243791,297680541,321200076,2096208954,391623662,1804246976,31778770,551301488,1166105662,781792739,1678734628,1307197070,550226963,160811703,1419610898,1595910885,810770124,954147798,819120552,728566712,435255326,624628061,1436918739,86721642,557070749,1458721135,99983313,1494985330,1700711076,1203334688,241120729,997700033,2072605804,1018952924,2035317301,1321995664,1012292882,1232292909,347532795,48381871,1103526633,1733824403,560195622,7338928,1050042125,43834682,1820370600,1960963505,366289534,525131399,432709147,315182757,1539330557,2038636206,1678028524,2144329385,833128300,1818125475,1792869513,695106624,1868780311,111279306,1546581727,126029275,678591234,825659387,1090798389,1166244396,2121080778,767723090,2059894641,171220930,812413797,618218900,1604360991,122990691,979388615,330785242,367950143,2078539751,1503713447,1322333878,425962850,488894704,1592634456,382766654,1694655996,321296599,663385267,1496871789,1952605057,636974819,2006126724,1571691638,507844332,2022809782,1877348528,1141662707,280704150,1963156226,1718452277,1493449406,2053478376,1148677791,1445977807,1362914612,11859184,1574839705,2091069505,1539644917,1691455788,1430004266,2018148214,469468196,2002880402,899961273,2091491154,1521442863,2060714046,868950315,929782853,519927537,1427595054,351554276,507370392,1314189858,1336019295,715802256,1715507365,1993400922,740759160,1604776104,500554239,17257789,137627637,1034152467,944024334,1067575097,2086712363,584791410,2025330946,424357136,1881937183,287296237,275868871,1931890589,1860817515,1879595163,589165043,434747531,2046390112,2136433611,732369481,2107701328,4665567,2025520453,1068573183,397651971,710807450,838421735,1408570164,1157317667,244212433,2107709341,2107686549,1350697361,1181763803,1667626789,1662048635,2102305075,41414494,1393330533,564635378,1409677223,624619128,1196566075,329086146,1386277261,584725904,1226676302,1449236452,89403874,852716736,478333369,614581720,970075493,914546170,352733152,2127653485,83554980,1149608165,1915439424,1682239510,1920075966,132290417,968462734,129971661,880935938,534033473,436733855,335855392,1950196594,1074377560,1347441622,2021907303,1187688268,456844375,1825913979,1906410881,7108318,155210097,448404934,409490410,1589687148,1535824725,196911680,972770625,77158128,1423501094,393361288,370231039,338287801,1569664231,2039869057,1536216290,365327733,1362672466,1159775952,247511017,1530854559,1161548348,564816660,67701384,16207628,185309464,1688205846,308334886,465557464,198675244,1370357286,337826850,749569311,809317815,1730274195,2144291915,1870065740,2097238039,1039470705,673361576,1680644196,645975195,510510305,1700248358,858184304,1485621591,143448557,637669366,66324655,1682307901,458110836,1032857294,349562883,1863871554,843688596,529284722,1424907086,655665929,983569628,158805573,421544281,749871853,1755912343,1847816385,1465378881,157157144,1796074292,1542113351,69707884,1790406282,554466323,1765081574,1408266243,618457268,1980448665,1805538059,1689603649,102196365,1640242938,2018466791,752505822,878008681,1605807291,1390364306,628098189,29479172,259331639,770660282,486148814,307441325,1761290148,332886039,528088826,736068838,1312255468,163214063,1053208439,1187748601,1768960518,1280176871,145207054,2051168194,899860294,1620727097,346839404,400659001,1711234078,1659986381,1060666385,567621636,28323695,1048458353,1078439297,2019345249,1072880000,1702210549,1682242640,1266305824,133302478,1355558316,263025639,574165699,2061126980,341635579,586591907,578335481,1315453666,336136373,27866242,522431404,2038206060,1557960013,569269191,927581634,1294576561,1462506821,1198884832,1273723715,811512925,1038701914,1486636128,709474336,1901573666,1099186947,323391366,1972823150,1830042421,1457029784,326988098,875737833,52085352,515077420,985147995,507411203,223889464,1435726264,977930157,846712311,299534199,924639915,835591390,1930413348,910176336,1694131668,378661824,880834528,368709128,324989141,584876962,2082189127,2011359043,1949667757,1540011646,197289533,615357493,1677608790,898163513,710544275,2100674269,376276118,575841326,1103729980,2099136938,464280663,724105676,275209247,206006930,54634213,1821857398,579013832,544342812,525956911,211155019,1042973191,401684725,975795305,577671390,854691274,1105313306,643697369,1298157214,121384639,607101409,1260865121,480155267,1211842659,9016489,773716568,200245793,52969136,1048534692,1199675768,944821675,781256407,708887493,2142653400,1735040595,960234135,749094037,525668623,966739389,1850272607,1433615208,1672587331,1885470651,31213119,1645514606,207001284,235470463,1355189082,1969804850,1251376243,942690911,625349879,669325250,1641339079,1846191025,598490273,1882118898,2025444661,920833385,660519855,135630152,1663494588,505872505,988652518,1012110353,1102462977,1899222616,329916611,40173267,761986640,1587826250,1346853989,997098648,1031580211,1548978826,1155258127,1330413989,1031217059,91781380,924496991,2075794976,1267181020,1717936440,1976674671,669014681,2140926665,1852153498,1017366089,1773852155,1982466369,1480111620,84099396,1831138787,508361381,263180339,1365772346,1263776192,1756821130,982779283,1058916023,797941333,287632528,2034427747,125717749,1253673233,90776217,238866261,1326289632,710513900,327733831,2123733770,1717111868,816071284,1506132969,928463136,348164920,993262457,336714496,1727175449,1263857906,586673827,468533912,1427293863,1417673682,1681544482,946978864,450430786,1148981340,1327062652,2135298540,1954510164,479787129,1980050608,832049703,2041475160,1005086283,268835012,11242096,1371588122,1204045546,1650335032,1629122673,1286072236,1570248360,1863053210,1742562040,1731754210,460980233,1971910015,1271280738,2033822388,2085225908,1428174571,707685287,2066706598,1665479883,655853434,994658446,525671804,1308376538,582462022,439608426,2081289454,691202714,1319922435,666239118,91992482,1141893363,697051680,1532032896,1156864589,1057268935,1425465450,427602646,981422653,1321941373,1349443711,693840351,1272895709,443986427,1390370268,1740872459,787981474,272886821,303972250,1447500735,277659686,856703402,1529734338,1270056129,1669231241,953647061,960664162,1109372393,333653468,1894192106,1229997365,2114272445,1161891977,1078894312,617731135,1674584609,65062017,76040224,1199239691,1128398999,1918592227,1855089013,101124929,1158730693,1801162002,1181964619,1165373154,1499403080,266802358,130801952,1553457602,2060670315,8471237,1411097502,373235675,855568966,590758485,2128106589,1782494718,207009568,432993987,2044046927,429642109,1251586920,384114697,2137858096,1308054613,698405853,1143622157,1594610227,523249956,1755778259,1090369292,2053052180,1396702473,1241487800,803615566,1615266327,480490293,266969924,57281939,1720140348,1029806983,1117424095,1233750446,1086004507,583946990,1741227500,206087900,1097509175,2019228426,675831443,2132000404,886141855,150531297,315748861,735811281,991549629,1124328977,601250685,1278850367,2057457510,521868739,322093489,1743584820,1559828412,2024458623,88194874,449130980,447912282,430333851,575771003,736461071,1777191269,109935911,783009123,273834379,57710315,1928832106,1940817721,1537775248,68539500,414712288,1846819992,208952377,92013577,1854837976,995341017,740208776,1574855414,1466327026,2015343690,541802398,1121087375,1997439930,1775634729,698222776,1188196359,1736740769,1763084934,602084132,1502413665,95846652,2068178889,2006999743,721982783,528045650,779769889,1703422170,1116282675,2040165763,631247991,1434769632,480663112,819892255,1517706449,1705262465,1227054062,1954575519,311127327,1927227884,2088600626,1224002305,1690784189,997941558,1420839835,84441193,866472811,1314313189,936084514,1813186974,1637523707,1834573449,416771744,1524398496,719465115,999180499,1409782253,1813129580,1852135653,1969266327,523383916,1343013688,1565028243,2073613833,533590630,209475096,2051206104,846496130,689917152,721315122,1993006973,166355,955346015,1645919171,84912724,602377808,656355100,1284342926,1183721750,1291388894,1355408461,294167494,479322785,631031652,823625148,968932492,1381003899,256602822,1480054988,1989774326,725741762,827250611,148958470,99320693,715965236,1727575054,347969349,159552224,1117861250,75802225,1714468480,904532299,831745488,1100266884,1431106289,1480900806,235581672,1898497704,398858873,257036088,950799103,875251377,638159742,453365494,2070458234,101576748,1417053499,932714065,1067390865,100834675,1708197442,269473311,1932537697,1339115445,1022202749,19228827,239267094,309196993,165525504,1748212330,824709274,1780137499,2117221774,1148516920,1725199537,2056653419,22640141,261234326,1442603581,1642121653,371932391,1209139833,1442142885,611327664,860587870,11010440,223511966,1043243729,1697470927,1663424471,2009541131,124873866,402485356,1648472936,1236555669,450181988,1666962469,404140506,757669566,1042287815,1545546567,1636362875,2024419288,88527291,1516290362,2099796356,1828252149,1441791358,8396055,1101295577,570013468,955732028,1580019479,757468579,627563118,1137942403,1127263552,1000049692,661508572,211445047,662040667,1387537725,1385974930,2096153847,1357579003,1245155274,1553536241,1202203480,1052498870,779384567,184287193,1410381333,2146765491,617274034,2086937194,67927350,1068225166,1823806354,713597290,421131545,252838416,1067300408,702988776,971888912,1890245332,1958862439,1904459870,799178543,1386774635,1969907521,1130446062,443080409,2018439120,222315160,1294300375,1569727434,1375877922,2014013524,1134019002,1459629137,1815940270,627291040,1778737440,1190301176,1057020811,1897772118,51847116,1361012322,808502734,605583537,1383150275,1139088346,568378250,901646469,531621912,1956852771,722142802,824834579,12079853,1853892148,1964793852,140375945,2064831,956944607,1228152454,1814997984,1292130338,2078475014,1467988156,325887397,1231205338,1072776611,966196868,1756690104,1706189432,820191936,1971799879,1652108362,1358539093,1053190907,1090568260,1353018857,337086439,233957203,1630170214,1931835877,1053727461,1831085893,383642570,1620714443,892334638,1400096269,445302095,1329551363,1097265398,1171232575,1731461306,442610600,514106501,380404133,969751545,1298587193,254814587,1977930033,821625065,1293625808,788715160,26793270,1858812480,1823885692,500189673,1305496402,671667494,162819633,918169918,1924471513,1084315296,2001197450,1731273948,1448432946,1596978733,2019591560,587662857,1354477678,892153648,943340652,1435044364,678425566,2012251254,421091846,2130263484,1223576675,1302176320,2011094595,1001864920,1201818056,1461464491,393179596,454000061,929454482,1461007290,1023873274,1474276657,514015405,506640620,1231667195,184195997,1228892201,788419254,1714464203,96576948,1062626201,726686544,1175557518,1387514151,393037575,2025344895,153603309,2113842226,93497776,1589413359,1518558898,1352505827,946909532,1090455854,1889687618,1364012181,335073411,345314294,1407216447,1365822592,163113168,279997401,224429965,1254008590,1363569002,527841807,64473167,421870263,1205822404,490842691,543909432,1144520430,1156334004,27673944,1189805900,1875587160,343231140,1415318836,1217139055,1097012232,1115668465,1140348407,494828454,1398612240,336887686,402458356,1914081662,1329806581,771319990,2118750129,1672679159,1738111033,473962080,439951226,1306303817,1914379298,937059831,1338769807,1500775792,102234668,119866724,79436972,677770259,1406973730,662722844,1940852051,89675360,67864851,1232512666,612510882,1973419000,1535725534,980753207,2143137644,1572769124,75042031,1182832284,301743962,329454515,851223645,2025626998,217776933,1436832782,300470471,1205139977,543936083,631406108,2072703244,927538260,1015770194,1400973064,203006733,1069133429,1656599450,214090471,1110400993,268988980,667742885,1074004214,1165855465,509145227,1441474544,894646054,394155540,144020960,767320446,545178238,940561662,467030825,1965293344,1083584821,1685485619,1049832248,1269399279,1832116486,837861124,1813081805,559124908,1438318156,204692686,969830436,1098734285,674925581,53380549,414975248,1431210585,691854617,2058180191,1185424105,1183547166,699461653,371735884,1707163185,1855869977,539116207,1798681415,81576231,656607825,690782615,40879700,2126117575,141357482,139494528,1495282836,715368753,1254895267,763487915,1817793402,1184983,231622474,641319047,454917838,1253808443,1080171221,1735796059,979174040,392899988,2055493296,224226072,339560192,561650734,1479640502,1491734987,1668245178,2031006063,1172642784,24021581,1983494185,301292001,508267338,1624019709,115601108,1360929814,1161524039,377191269,29828361,613263882,1469713188,249049105,334111508,1934397598,188198958,1134017214,736678029,1156071,1948714093,707732793,1455664120,187103950,1216560734,1775609430,736291899,1505145673,1863152092,93471562,557049380,114041722,556614841,1196052081,1631800039,339881937,214761158,486160050,95510245,407147363,794655240,815586195,1191123774,1908386253,1052016134,691376166,2124589272,1264557326,789347076,1227075040,1672361967,1001247029,1933433545,1312262663,1257942588,1134877735,2045227894,1594287014,30382211,1549318922,1220565355,357836369,1655882116,1260807111,620003915,1409087281,21178689,600450003,391351649,1108697798,544390092,740583589,869874587,638586632,1758701661,731246219,946232146,1353414681,1047784479,828223538,427579708,1683018165,1335623530,1444310647,87586589,64943616,1919132150,1607380502,730066484,1356220811,554229658,472120265,1917462875,1984937048,266381907,644520206,29852247,202246623,1937202827,687795018,1194204129,543939923,342630729,1889199829,458114766,637309964,716534321,2111332316,1704816052,1603410291,900342862,1983807322,2119107353,1504719377,1860805077,814357517,1189506005,1172887494,18040076,550163245,40932420,426036215,1155072828,726905023,2070283319,72745328,803377872,1314190731,1457336936,1119538163,1985292794,1880842961,822770539,308533216,1994334181,907193139,1262774086,2026441347,1180736488,1450942942,795811177,67625004,837468838,304725971,595159811,329079098,1345336538,626085005,183955836,332996981,2020492328,898501534,1127175779,1899924054,690819833,52986981,94870114,1660219235,488780263,1305631779,832248710,1837804800,1354001037,634311649,1850561622,406193775,1820267037,278984534,1952756489,153169386,2112423979,691065612,1312177736,406250993,482272163,121504325,648720632,1083781960,1310742213,1981420201,917647405,1271957671,1524482874,1950547188,1148906027,1465997613,1520686119,186208193,1390142822,2065268806,9840362,527969213,1116010728,2062333428,2131293943,1087202245,1882231587,1765847000,1276621381,696872573,897727896,702415389,663261144,967996802,1580019816,111730459,1144837775,885422385,599290579,36257894,1833018479,897147788,352014112,434647299,176839678,816158936,469640635,1586903312,1363023823,968063907,113866049,57605331,1652444260,889973029,1816873071,345020260,940882487,1542798678,873475301,298604938,1511450423,1169460840,679958463,1984188266,1847537251,1015947035,553770136,951619107,938028982,1443692198,843241398,2001976701,1894311874,1463847866,1996125200,1259721759,1864498384,1460251669,1415560760,1376971209,967040190,805029646,1576240077,1041803107,634780836,1681999541,491265347,1715670990,2057328909,170349345,1796923458,1586290412,529312052,1833259703,832564217,1005995044,622076458,806179195,799479381,1573293909,1461839458,1085485686,1650884182,1771839783,892588205,368595222,2132033471,130975228,1797171740,1684256704,1565874596,858298057,1887495452,376554551,1738470139,2039757034,169623536,1540965020,968731263,809560236,841114776,1373965484,1905002855,971665911,616586850,57024598,785651859,94637405,1572311830,1111051062,1180420331,1871128893,1505289292,1705967120,1052053378,445117470,1242596144,97266788,1186934673,554746740,1977200864,1259387579,1429374376,1108670146,338067571,1065237547,1253067927,114273263,394094258,142929823,1704641354,663123370,2002581819,181203279,161986248,53626592,529484803,1302025286,111920808,1062926012,803233050,609847882,1806221236,1650441495,816798633,1980387598,364244919,1915443098,1347855384,1600199697,1949445494,1378241815,521160384,38363366,667882998,728361728,1599885559,1460097241,617885035,1737790339,2074650109,884467206,1142930609,509791680,1636083198,1230542463,859051206,1405011400,2109616717,944468453,826394047,206768272,1703485709,1638356437,499558427,1015115345,879433262,1667421045,2061819989,1129578094,35604220,1740161236,170556174,142621447,513530061,1199361480,289172349,2098777256,326409251,1803272182,479538891,1629234867,788186736,322424640,218051220,1367968831,543430071,832705681,1359348954,1550971989,535719354,778441689,1737420435,1735029241,1002713823,914584216,17518422,175652214,232021192,292771908,700739472,1670768408,1658601355,1262006622,502946057,1613661455,1298089727,2049900546,1434678215,367199697,1462798871,765549045,1626950066,1936726357,1665017899,159167642,555536480,1456548085,1507406975,1244744230,1318733030,1624954762,1340632218,1493351993,581500893,1729764158,1789391488,2139464105,1040602601,331962552,529721038,1723430028,657048284,502059344,614211091,1427758956,1181168911,1551920194,378153059,1068280664,134937988,60854251,1934194942,1101892867,1269947217,685226416,579161149,1758350498,2093516817,394948549,511895146,1350607967,1226848277,1174548874,1580512620,851727387,1116359867,932716509,428933362,959649769,15735381,550754612,298800265,1318030007,1165062342,870443121,710217510,2005325369,736553455,244048388,1151744422,924393948,1941186332,19157788,1312596453,18179639,1690149682,1521420368,1490607421,1702748759,640865727,1221939712,1516989828,1259767005,866906000,672785794,1299915743,740628675,1453845082,1759788213,2067571507,515102660,923995409,1469040258,196153157,879797469,453337322,187399864,1663374691,1541873647,691450256,650192845,616431711,461369004,1593656845,626496987,1249961608,1039937469,1766045101,1745176272,1098213658,222907848,439509694,23930361,10565399,1484465365,1965178564,34400089,564316210,1911626070,719829972,1581565983,750927170,1667972817,33364456,2141134445,998287071,720641566,1347790141,1351068193,268811418,1285663982,517933349,1790173012,1911603225,2111141341,216528,304422911,610090623,1094688873,1202585056,1304841923,387815872,404115922,1840905679,1176089780,605555887,1691086968,1398744084,456260139,993475867,316000503,314590886,2019409805,163681388,1617005793,307490447,799016332,827057369,19508885,1250938536,1201820454,1671079994,1270906876,544984280,358638638,368833817,1537315768,702039755,674772079,1499880576,687915082,1555670862,1657822678,205891149,1320552040,459299338,1758747083,905209165,343071615,1218786620,908952361,892105878,405442426,449293636,1797444596,1481051951,26775653,41723340,1565779144,2134086201,385871057,1416369929,301065004,266875165,1736001848,499982226,416232703,746717105,1603018414,1301920314,863915849,25317820,75331979,724199088,252297378,960457728,352422051,395971386,205681962,209693859,357633644,636590668,834404815,645113937,605072728,1156946387,274630218,1842958842,614589477,331911426,1374159945,570277580,1908675296,1194136152,385568299,383602060,1866513841,1289644103,453218662,2095588195,286595280,1508073216,1313011843,354461774,1982591244,536957494,840681692,1773065219,1338243676,478896703,220836868,907042769,339551427,2113550271,737274838,1752885889,1748688403,1982941817,2021104628,1359216890,1776853681,1359589500,1119641200,849539565,1154899566,470344722,1272842795,1500177405,750768062,468733305,629271577,1499637710,794760446,1885494639,169567640,1058091368,2070445085,567916375,1631873399,405715284,1441325749,208107521,1826803091,1307593264,175921216,1075740241,818211222,1950316439,130170148,972834195,1313846750,18278925,196251336,1016358911,689834984,365071738,163743910,475592323,2006901719,538539065,480035982,587925351,77456925,1569831288,676412495,1411037599,392395713,729640329,1478893210,1749586035,957566540,298502647,661701364,1599610542,2084645768,1378624592,252035277,1525621603,1762247614,1078583246,619710000,1577351832,119831137,1441140397,1923843031,2035633759,1204304416,111489013,856517186,1194712866,341104355,1566804851,638732797,321696454,1944314938,1196635439,30090707,1003362456,1785610325,1380633796,1876638077,766914581,1490842966,1826849014,1230281601,1531213449,1765746949,1055588483,1188603753,484029792,1520713752,1091450336,1893370851,291465287,835795643,508368367,254065734,2102536969,349438043,1797051345,2125618491,1987221278,1051928920,214413567,573580082,1154142476,931070014,697347425,1611266938,1590027025,1244596972,342652618,828854930,120270818,1431173580,1851454733,1738400889,1476552102,1248977363,626427269,1752791612,992197725,209323353,818988349,1538514505,1160230775,531351355,1911232429,60480324,1723016062,130103323,1872034226,278285628,327316789,342845529,1872224821,1893910041,1642119193,706626401,134694629,263616649,149977810,1809392799,1931613245,527646499,1963923532,1742474336,1751929851,1674330470,1545661937,1304138694,150964789,836717915,1036298808,1324955616,548631192,152987424,354731542,1145278318,1889410514,2087487118,724691620,1780337328,677552914,290863370,1427457578,443296151,1498125056,1109696912,2018747928,936657795,1292431162,43695854,340473783,402142169,1512653454,780900620,526237671,398019449,211607828,2129475377,1897371922,432379646,584219460,312357881,1404744713,1744751211,1350810840,1634527565,1783347068,1520259488,217874207,2090357040,831906918,1952175528,1265700942,1830335335,1018892758,786799949,333136582,1779081699,571047604,459530794,1142222889,1474681379,2037022431,1796756502,554490145,762157317,641864038,1250292586,219779684,453436724,639403725,1891672416,647408047,179617347,279321592,1139146980,282596621,598295821,516790537,300529596,1707199181,778785636,1930405837,660287362,1666391143,439756929,607490144,1618437971,231425076,483709873,2098002145,1349271916,2117075112,1225797939,692233910,1100610734,1657723317,1945220761,2146026739,1346253706,882176855,952250272,192393317,1351447980,54633646,670468261,1738448357,749968317,828841858,974967994,656849960,2053277284,528952058,2031976761,644993885,1342760864,1451405843,1530510797,1098348803,2015087884,1848262697,683008672,521748489,1248525585,1072235836,1037266484,890730443,1066024016,227924526,1339421022,1377502216,705525077,1944887698,1253678504,1495411592,310408311,578586356,1031765977,437849388,61780955,1949900149,143155644,781179049,40348931,1593932883,1195017834,1292187059,899585024,1138804406,634259961,1954590976,1102183147,631065491,577439288,1858941196,1424298817,1101697847,916526146,594096567,801523910,553050923,393620897,396658315,1089473373,1332521685,1047087750,10782147,1387871118,928220051,1480379997,553271546,77901345,338658831};

void init() {
    LL ans1 = 0, ans2 = 0, ans, res;
    printf("{");
    for(LL i = 3; i < MAXN; ++i) {
        res = (i * i) % mod * i % mod * i % mod;
        if(i & 1) ans1 = ans1 * 2 % mod + ans2 + res;
        else ans2 = ans2 * 2 % mod + ans1 + res;
        ans1 %= mod, ans2 %= mod;
        if(i % 1000000 == 0) {
            printf("%lld,%lld,", ans1, ans2);
        }
    }
    printf("}");
}

struct mat {
	LL m[2][2];
}ans;

mat mul(mat a,mat b)
{
	mat c;
	for(int i=0;i<2;i++)
		for(int j=0;j<2;j++) {
			c.m[i][j]=0;
			for(int k=0;k<2;k++) {
				c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%mod;
			}
		}
	return c;
}

mat pow(mat a, LL n) {
	memset(ans.m,0,sizeof(ans.m));
	for(int i=0;i<2;i++)
		ans.m[i][i]=1ll;
	while(n) {
		if(n&1ll)
			ans=mul(ans,a);
		a=mul(a,a);
		n>>=1;
	}
	return ans;
}

int main() {
    //freopen("out.txt", "w", stdout);
    int T; //init();
    scanf("%d", &T);
    mat base={1,2,1,0};
    while(T--) {
        LL a, b, n;
        scanf("%lld %lld %lld", &n, &a, &b);
        if(n <= 2) {
            if(n == 1) printf("%lld\n", a);
            else printf("%lld\n", b);
            continue;
        }
        LL m = n / 1000000;
        LL ans1 = p[m][0], ans2 = p[m][1], ans, res;
        LL cnt = max(3ll, m * 1000000 + 1);
        for(LL i = cnt; i <= n; ++i) {
            res = (i * i) % mod * i % mod * i % mod;
            if(i & 1) ans1 = ans1 * 2 % mod + ans2 + res;
            else ans2 = ans2 * 2 % mod + ans1 + res;
            ans1 %= mod, ans2 %= mod;
            ans = ((i & 1) ? ans1 : ans2);
        }
        mat B = {b, 0, a, 0};
        mat A = mul(pow(base, n - 2), B);
        printf("%lld\n", (A.m[0][0] + ans) % mod);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值