hdu 5667 Sequence (矩阵快速幂)

Sequence

 
 Accepts: 59
 
 Submissions: 650
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">\ \ \ \</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0em"></span><span class="strut bottom" style="display:inline-block; height:0em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span></span></span></span></span>Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到了一个数列:

<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">f_n=\left\{\begin{matrix} 1 ,&n=1 \\ a^b,&n=2 \\ a^bf_{n-1}^cf_{n-2},&otherwise \end{matrix}\right.</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:2.05911em"></span><span class="strut bottom" style="display:inline-block; height:3.61822em; vertical-align:-1.55911em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.10764em">f</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:0.15em; margin-right:0.05em; margin-left:-0.10764em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle cramped" style="display:inline-block; font-size:0.7em"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="mrel" style="margin-left:0.27778em">=</span><span class="minner textstyle uncramped" style="margin-left:0.27778em"><span class="style-wrap reset-textstyle textstyle uncramped" style="position:relative; font-size:1em"><span class="delimsizing mult" style=""><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:0.65001em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="delimsizinginner delim-size4" style="display:inline-block"><span style="font-family:KaTeX_Size4;">⎩</span></span></span><span style="display:block; height:0px; position:relative; top:-1e-05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="delimsizinginner delim-size4" style="display:inline-block"><span style="font-family:KaTeX_Size4;">⎨</span></span></span><span style="display:block; height:0px; position:relative; top:-1.15002em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="delimsizinginner delim-size4" style="display:inline-block"><span style="font-family:KaTeX_Size4;">⎧</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span></span><span class="mord" style=""><span class="mtable" style=""><span class="col-align-c" style=""><span class="vlist" style="display:inline-block; text-align:center"><span style="display:block; height:0px; position:relative; top:-1.21911em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="mord textstyle uncramped" style="display:inline-block"><span class="mord" style="">1</span><span class="mpunct" style="">,</span></span></span><span style="display:block; height:0px; position:relative; top:-0.01em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="mord textstyle uncramped" style="display:inline-block"><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">a</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:-0.363em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle uncramped" style="display:inline-block; font-size:0.7em"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">b</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="mpunct" style="">,</span></span></span><span style="display:block; height:0px; position:relative; top:1.19911em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="mord textstyle uncramped" style="display:inline-block"><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">a</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:-0.363em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle uncramped" style="display:inline-block; font-size:0.7em"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">b</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.10764em">f</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:0.248108em; margin-left:-0.10764em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle cramped" style="display:inline-block; font-size:0.7em"><span class="mord scriptstyle cramped" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span><span class="mbin" style="">−</span><span class="mord" style="">1</span></span></span></span><span style="display:block; height:0px; position:relative; top:-0.363em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle uncramped" style="display:inline-block; font-size:0.7em"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">c</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.10764em">f</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:0.15em; margin-right:0.05em; margin-left:-0.10764em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle cramped" style="display:inline-block; font-size:0.7em"><span class="mord scriptstyle cramped" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span><span class="mbin" style="">−</span><span class="mord" style="">2</span></span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="mpunct" style="">,</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="arraycolsep" style="display:inline-block; width:0.5em"></span><span class="arraycolsep" style="display:inline-block; width:0.5em"></span><span class="col-align-c" style=""><span class="vlist" style="display:inline-block; text-align:center"><span style="display:block; height:0px; position:relative; top:-1.21911em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="mord textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span><span class="mrel" style="margin-left:0.27778em">=</span><span class="mord" style="margin-left:0.27778em">1</span></span></span><span style="display:block; height:0px; position:relative; top:-0.01em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="mord textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span><span class="mrel" style="margin-left:0.27778em">=</span><span class="mord" style="margin-left:0.27778em">2</span></span></span><span style="display:block; height:0px; position:relative; top:1.19911em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="mord textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">o</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">t</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">h</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">e</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.02778em">r</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.02691em">w</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">i</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">s</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">e</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span></span></span><span class="sizing reset-size5 size5 reset-textstyle textstyle uncramped nulldelimiter" style="display:inline-block; width:0.12em; font-size:1em"></span></span></span></span></span></span>

<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">\ \ \ \</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0em"></span><span class="strut bottom" style="display:inline-block; height:0em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span></span></span></span></span>他给了你几个数:<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">n</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.43056em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">a</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.43056em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">a</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">b</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.69444em"></span><span class="strut bottom" style="display:inline-block; height:0.69444em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">b</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">c</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.43056em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">c</span></span></span></span></span>,你需要告诉他<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">f_n</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.69444em"></span><span class="strut bottom" style="display:inline-block; height:0.88888em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.10764em">f</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:0.15em; margin-right:0.05em; margin-left:-0.10764em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle cramped" style="display:inline-block; font-size:0.7em"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span></span></span></span></span>模<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">p</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.625em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">p</span></span></span></span></span>后的数值.
输入描述
<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">\ \ \ \</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0em"></span><span class="strut bottom" style="display:inline-block; height:0em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span></span></span></span></span>第一行一个数T,为测试数据组数.

<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">\ \ \ \</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0em"></span><span class="strut bottom" style="display:inline-block; height:0em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span></span></span></span></span>每组数据一行,一行五个正整数,按顺序为<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">n</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.43056em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">a</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.43056em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">a</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">b</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.69444em"></span><span class="strut bottom" style="display:inline-block; height:0.69444em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">b</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">c</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.43056em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">c</span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">p</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.625em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">p</span></span></span></span></span>.

<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">\ \ \ \ 1\le T \le 10,1\le n\le 10^{18}</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.814108em"></span><span class="strut bottom" style="display:inline-block; height:1.00855em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord" style="">1</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-left:0.27778em; margin-right:0.13889em">T</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord" style="margin-left:0.27778em">1</span><span class="mord" style="">0</span><span class="mpunct" style="">,</span><span class="mord" style="margin-left:0.16667em">1</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-left:0.27778em">n</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord" style="margin-left:0.27778em">1</span><span class="mord" style=""><span class="mord" style="">0</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:-0.363em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle uncramped" style="display:inline-block; font-size:0.7em"><span class="mord scriptstyle uncramped" style=""><span class="mord" style="">1</span><span class="mord" style="">8</span></span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span></span></span></span></span>,<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">1\le a,b,c\le 10^9</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.814108em"></span><span class="strut bottom" style="display:inline-block; height:1.00855em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord" style="">1</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-left:0.27778em">a</span><span class="mpunct" style="">,</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-left:0.16667em">b</span><span class="mpunct" style="">,</span><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-left:0.16667em">c</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord" style="margin-left:0.27778em">1</span><span class="mord" style=""><span class="mord" style="">0</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:-0.363em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle uncramped" style="display:inline-block; font-size:0.7em"><span class="mord" style="">9</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span></span></span></span></span>,p是质数且<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">p\le 10^9+7</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.814108em"></span><span class="strut bottom" style="display:inline-block; height:1.00855em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">p</span><span class="mrel" style="margin-left:0.27778em">≤</span><span class="mord" style="margin-left:0.27778em">1</span><span class="mord" style=""><span class="mord" style="">0</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:-0.363em; margin-right:0.05em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle uncramped" style="display:inline-block; font-size:0.7em"><span class="mord" style="">9</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span><span class="mbin" style="margin-left:0.22222em">+</span><span class="mord" style="margin-left:0.22222em">7</span></span></span></span></span>.
输出描述
<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">\ \ \ \</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0em"></span><span class="strut bottom" style="display:inline-block; height:0em; vertical-align:0em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span><span class="mord mspace" style="display:inline-block"> </span></span></span></span></span>对每组数据输出一行一个数,输出<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">f_n</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.69444em"></span><span class="strut bottom" style="display:inline-block; height:0.88888em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord" style=""><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic; margin-right:0.10764em">f</span><span class="vlist" style="display:inline-block"><span style="display:block; height:0px; position:relative; top:0.15em; margin-right:0.05em; margin-left:-0.10764em"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span><span class="reset-textstyle scriptstyle cramped" style="display:inline-block; font-size:0.7em"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">n</span></span></span><span class="baseline-fix" style="display:inline-table; height:0px; position:relative; table-layout:fixed"><span class="fontsize-ensurer reset-size5 size5" style="display:inline-block; font-size:1em"><span style="font-size:0em">​</span></span>​</span></span></span></span></span></span></span>对<span style=""><span class="katex" style="font-size:1.21em; font-family:KaTeX_Main; line-height:1.2; white-space:nowrap"><span class="katex-mathml" style="position:absolute; padding:0px; border:0px; height:1px; width:1px; overflow:hidden">p</span><span class="katex-html" style="display:inline-block"><span class="strut" style="display:inline-block; height:0.43056em"></span><span class="strut bottom" style="display:inline-block; height:0.625em; vertical-align:-0.19444em"></span><span class="base textstyle uncramped" style="display:inline-block"><span class="mord mathit" style="font-family:KaTeX_Math;font-style:italic">p</span></span></span></span></span>取模后的数值.
输入样例
1
5 3 3 3 233
输出样例
190

观察递推式我们可以发现,所有的f_ifi都是aa的幂次,所以我们可以对fif_ifi取一个以aa为底的loglog,即gi=loga fig_i=log_a\ f_i

那么递推式变成gi=b+c∗gi−1+gi−2g_i=b+c*g_{i-1}+g_{i-2},这个式子可以矩阵乘法

这题有一个小trick,注意a mod p=0a\ mod\ p=0的情况.

分析:根据题解化简找出那个行列式,然后就求gi。需要注意的是gi是指数,根据费马小定理,指数%(p-1)即可。

a ^ b % c = a ^(b % phi(c) + phi(c)) % c; 因为c是质数,所以这里的phi就为c - 1;所以只要用矩阵幂的时候% c -1,答案% c即可



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
ll mod;
struct mat
{
   ll m[3][3];
};
mat mul(mat a,mat b)
{
    mat c;
    memset(c.m,0,sizeof(c.m));
    for(int k=0;k<3;k++)
        for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)
        c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j]%(mod-1))%(mod-1);
    return c;
}
mat qmod(mat a,ll k)
{
    mat c;
    for(int i=0;i<3;i++)
        for(int j=0;j<3;j++)c.m[i][j]=(i==j);
    for(;k;k>>=1){
        if(k&1)c=mul(c,a);
        a=mul(a,a);
    }
    return c;
}
ll po(ll x, ll k)
{
    ll res = 1;
    for(;k;k>>=1){
        if(k&1)res=res*x%mod;
        else x=x*x%mod;
    }
    return res;
}
int main()
{
    //freopen("f.txt","r",stdin);
    int  T;
    ll a,b,c, n;
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&mod);
        if(n==1){
            printf("1\n");continue;
        }
        else if(n==2){
            ll res=po(a,b);
            printf("%lld\n",res);continue;
        }
        mat ans;
        ans.m[0][0]=c;ans.m[0][1]=1;ans.m[0][2]=0;
        ans.m[1][0]=1;ans.m[1][1]=ans.m[1][2]=0;
        ans.m[2][0]=b;ans.m[2][1]=0;ans.m[2][2]=1;
        ans=qmod(ans,n-2);
        ll res=1;
        ll k=ans.m[0][0]*b+ans.m[2][0];
        res=po(a,k);
        printf("%lld\n",res);
    }
    return 0;
}





posted @ 2016-04-18 19:27  HARD_UNDERSTAND  阅读(166)  评论(0编辑  收藏  举报