<h1>A题</h1> <h2>小a的计算器(水题)</h2> <p>链接:<a href="https://ac.nowcoder.com/acm/contest/317/A" target="_blank">https://ac.nowcoder.com/acm/contest/317/A</a><br>来源:牛客网<br><br>小a的数学基础实在太差了,以至于他只会用计算器算数。他的计算器比较特殊,只有<span class="MathJax_Preview"><span id="MathJax-Element-1-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span id="MJXc-Node-1" class="mjx-math"><span id="MJXc-Node-2" class="mjx-mrow"><span id="MJXc-Node-3" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">+<span id="MJXc-Node-4" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-5" class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R">−<span id="MJXc-Node-6" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-7" class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R">×<span id="MJXc-Node-8" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-9" class="mjx-texatom MJXc-space1"><span id="MJXc-Node-10" class="mjx-mrow"><span id="MJXc-Node-11" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">/<span class="MJX_Assistive_MathML">+,−,×,/(即加减乘除)四种运算。<br> 经过一番周折,小a终于算出了他想要的数,但是他却忘记了最初的数是什么。不过幸运的是他记下了整个操作序列,他想请你帮他算出最初的数<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <h2><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">思路</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></h2> <p><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">就是用个数组,将操作序列记录下来,然后逆序进行求解,这里对乘的运算的逆是除,但是,要排除除以0的情况,进行下特判</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <h2><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">代码</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></h2> <div class="cnblogs_code"> <pre>#include &lt;bits/stdc++.h&gt; <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main() { </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> n,m; cin</span>&gt;&gt;n&gt;&gt;<span style="color: #000000;">m; </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> op[<span style="color: #800080;">105</span>],x[<span style="color: #800080;">105</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">1</span>;i&lt;=n;i++) cin&gt;&gt;op[i]&gt;&gt;<span style="color: #000000;">x[i]; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=n;i&gt;=<span style="color: #800080;">1</span>;i--<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(op[i]==<span style="color: #800080;">1</span>) m-=<span style="color: #000000;">x[i]; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(op[i]==<span style="color: #800080;">2</span>)m+=<span style="color: #000000;">x[i]; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(op[i]==<span style="color: #800080;">3</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(x[i]==<span style="color: #800080;">0</span>) m=<span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> m=m/<span style="color: #000000;">x[i]; } </span><span style="color: #0000ff;">else</span> m=m*<span style="color: #000000;">x[i]; } cout</span>&lt;&lt;m&lt;&lt;<span style="color: #000000;">endl; </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></pre> </div> <p>&nbsp;</p> <h1>B题</h1> <h2>小a与204</h2> <p>链接:<a href="https://ac.nowcoder.com/acm/contest/317/B" target="_blank">https://ac.nowcoder.com/acm/contest/317/B</a><br>来源:牛客网<br><br>小a非常喜欢<span class="MathJax_Preview"><span id="MathJax-Element-1-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>204</mn></math>"><span id="MJXc-Node-1" class="mjx-math"><span id="MJXc-Node-2" class="mjx-mrow"><span id="MJXc-Node-3" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">204<span class="MJX_Assistive_MathML">204这个数字,因为<span class="MathJax_Preview"><span id="MathJax-Element-2-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msup><mi></mi><mo>&amp;#x2032;</mo></msup><msup><mi>a</mi><mo>&amp;#x2032;</mo></msup><msup><mo>+</mo><mo>&amp;#x2032;</mo></msup><msup><mi>k</mi><mo>&amp;#x2032;</mo></msup><mo>=</mo><mn>204</mn></math>"><span id="MJXc-Node-4" class="mjx-math"><span id="MJXc-Node-5" class="mjx-mrow"><span id="MJXc-Node-6" class="mjx-msup"><span class="mjx-base"><span id="MJXc-Node-7" class="mjx-mi"><span class="mjx-sup"><span id="MJXc-Node-8" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">′<span id="MJXc-Node-9" class="mjx-msup"><span class="mjx-base"><span id="MJXc-Node-10" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">a<span class="mjx-sup"><span id="MJXc-Node-11" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">′<span id="MJXc-Node-12" class="mjx-msup MJXc-space2"><span class="mjx-base"><span id="MJXc-Node-13" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">+<span class="mjx-sup"><span id="MJXc-Node-14" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">′<span id="MJXc-Node-15" class="mjx-msup MJXc-space2"><span class="mjx-base"><span id="MJXc-Node-16" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span class="mjx-sup"><span id="MJXc-Node-17" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">′<span id="MJXc-Node-18" class="mjx-mo MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">=<span id="MJXc-Node-19" class="mjx-mn MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">204<span class="MJX_Assistive_MathML">′a′+′k′=204。<br> 现在他有一个长度为<span class="MathJax_Preview"><span id="MathJax-Element-3-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi></math>"><span id="MJXc-Node-20" class="mjx-math"><span id="MJXc-Node-21" class="mjx-mrow"><span id="MJXc-Node-22" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span class="MJX_Assistive_MathML">n的序列,其中只含有<span class="MathJax_Preview"><span id="MathJax-Element-4-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>2</mn><mo>,</mo><mn>0</mn><mo>,</mo><mn>4</mn></math>"><span id="MJXc-Node-23" class="mjx-math"><span id="MJXc-Node-24" class="mjx-mrow"><span id="MJXc-Node-25" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">2<span id="MJXc-Node-26" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-27" class="mjx-mn MJXc-space1"><span class="mjx-char MJXc-TeX-main-R">0<span id="MJXc-Node-28" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-29" class="mjx-mn MJXc-space1"><span class="mjx-char MJXc-TeX-main-R">4<span class="MJX_Assistive_MathML">2,0,4这三种数字<br> 设<span class="MathJax_Preview"><span id="MathJax-Element-5-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>a</mi><mi>i</mi></msub></math>"><span id="MJXc-Node-30" class="mjx-math"><span id="MJXc-Node-31" class="mjx-mrow"><span id="MJXc-Node-32" class="mjx-msubsup"><span class="mjx-base"><span id="MJXc-Node-33" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">a<span class="mjx-sub"><span id="MJXc-Node-34" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span class="MJX_Assistive_MathML">ai为序列中第<span class="MathJax_Preview"><span id="MathJax-Element-6-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>i</mi></math>"><span id="MJXc-Node-35" class="mjx-math"><span id="MJXc-Node-36" class="mjx-mrow"><span id="MJXc-Node-37" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span class="MJX_Assistive_MathML">i个数,你需要重新排列这个数列,使得<span class="MathJax_Preview"><span id="MathJax-Element-7-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><munderover><mo>&amp;#x2211;</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><mo stretchy=&quot;false&quot;>(</mo><msub><mi>a</mi><mi>i</mi></msub><mo>&amp;#x2212;</mo><msub><mi>a</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>i</mi><mo>&amp;#x2212;</mo><mn>1</mn></mrow></msub><msup><mo stretchy=&quot;false&quot;>)</mo><mn>2</mn></msup></math>"><span id="MJXc-Node-38" class="mjx-math"><span id="MJXc-Node-39" class="mjx-mrow"><span id="MJXc-Node-40" class="mjx-munderover"><span class="mjx-base"><span id="MJXc-Node-41" class="mjx-mo"><span class="mjx-char MJXc-TeX-size1-R">∑<span class="mjx-stack"><span class="mjx-sup"><span id="MJXc-Node-47" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span class="mjx-sub"><span id="MJXc-Node-42" class="mjx-texatom"><span id="MJXc-Node-43" class="mjx-mrow"><span id="MJXc-Node-44" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span id="MJXc-Node-45" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">=<span id="MJXc-Node-46" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span id="MJXc-Node-48" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">(<span id="MJXc-Node-49" class="mjx-msubsup"><span class="mjx-base"><span id="MJXc-Node-50" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">a<span class="mjx-sub"><span id="MJXc-Node-51" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span id="MJXc-Node-52" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">−<span id="MJXc-Node-53" class="mjx-msubsup MJXc-space2"><span class="mjx-base"><span id="MJXc-Node-54" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">a<span class="mjx-sub"><span id="MJXc-Node-55" class="mjx-texatom"><span id="MJXc-Node-56" class="mjx-mrow"><span id="MJXc-Node-57" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span id="MJXc-Node-58" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">−<span id="MJXc-Node-59" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span id="MJXc-Node-60" class="mjx-msubsup"><span class="mjx-base"><span id="MJXc-Node-61" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">)<span class="mjx-sup"><span id="MJXc-Node-62" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">2<span class="MJX_Assistive_MathML">∑i=1n(ai−ai−1)2最大(公式的含义是:每个数与前一个数差的平方的和)<br> 注意:我们默认<span class="MathJax_Preview"><span id="MathJax-Element-8-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>a</mi><mn>0</mn></msub><mo>=</mo><mn>0</mn></math>"><span id="MJXc-Node-63" class="mjx-math"><span id="MJXc-Node-64" class="mjx-mrow"><span id="MJXc-Node-65" class="mjx-msubsup"><span class="mjx-base"><span id="MJXc-Node-66" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">a<span class="mjx-sub"><span id="MJXc-Node-67" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">0<span id="MJXc-Node-68" class="mjx-mo MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">=<span id="MJXc-Node-69" class="mjx-mn MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">0<span class="MJX_Assistive_MathML">a0=0</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <h2><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">思路</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></h2> <p><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">贪心,我们需要对一个序列进行排序,是的两两之差的平方,要尽可能的大。</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <p><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">这里我们只需要先将2 0 4这三个数的次数统计一下,然后从1开始,对于前一个数进行判断然后放值进行就行了</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <p><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>+</mo><mo>,</mo><mo>&amp;#x2212;</mo><mo>,</mo><mo>&amp;#x00D7;</mo><mo>,</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo MJXc-space1"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="mjx-texatom MJXc-space1"><span class="mjx-mrow"><span class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R"><span class="MJX_Assistive_MathML">如果前一个数是0,那优先放4,如果4没了,就放2,如果2也没了,就放0,其他数也同理。</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <h2>代码</h2> <div class="cnblogs_code"> <pre>#include &lt;bits/stdc++.h&gt; <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main() { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n,m; </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> ans=<span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">int</span> a=<span style="color: #800080;">0</span>,b=<span style="color: #800080;">0</span>,c=<span style="color: #800080;">0</span><span style="color: #000000;">; cin</span>&gt;&gt;<span style="color: #000000;">n; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i&lt;n;i++<span style="color: #000000;">){ cin</span>&gt;&gt;<span style="color: #000000;">m; </span><span style="color: #0000ff;">if</span>(m==<span style="color: #800080;">0</span>) a++<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(m==<span style="color: #800080;">2</span>) b++<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(m==<span style="color: #800080;">4</span>) c++<span style="color: #000000;">; } </span><span style="color: #0000ff;">int</span> t=<span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i&lt;n;i++<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(t==<span style="color: #800080;">0</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(c&gt;<span style="color: #800080;">0</span>) c--,ans+=<span style="color: #800080;">16</span>,t=<span style="color: #800080;">4</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(b&gt;<span style="color: #800080;">0</span>) b--,ans+=<span style="color: #800080;">4</span>,t=<span style="color: #800080;">2</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> a--<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(t==<span style="color: #800080;">2</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(c&gt;<span style="color: #800080;">0</span>) c--,ans+=<span style="color: #800080;">4</span>,t=<span style="color: #800080;">4</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(a&gt;<span style="color: #800080;">0</span>) a--,ans+=<span style="color: #800080;">4</span>,t=<span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> b--<span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(t==<span style="color: #800080;">4</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(a&gt;<span style="color: #800080;">0</span>) a--,ans+=<span style="color: #800080;">16</span>,t=<span style="color: #800080;">0</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(b&gt;<span style="color: #800080;">0</span>) b--,ans+=<span style="color: #800080;">4</span>,t=<span style="color: #800080;">2</span><span style="color: #000000;">; </span><span style="color: #0000ff;">else</span> c--<span style="color: #000000;">; } } cout</span>&lt;&lt;ans&lt;&lt;<span style="color: #000000;">endl; </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></pre> </div> <h1>C题</h1> <h2>小a与星际探索</h2> <p>链接:<a href="https://ac.nowcoder.com/acm/contest/317/C" target="_blank">https://ac.nowcoder.com/acm/contest/317/C</a><br>来源:牛客网<br><br>小a正在玩一款星际探索游戏,小a需要驾驶着飞船从<span class="MathJax_Preview"><span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>1</mn></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">1号星球出发前往<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span class="MJX_Assistive_MathML">n号星球。其中每个星球有一个能量指数<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>p</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">p<span class="MJX_Assistive_MathML">p。星球<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>i</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span class="MJX_Assistive_MathML">i能到达星球<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>j</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">j<span class="MJX_Assistive_MathML">j当且仅当<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>p</mi><mi>i</mi></msub><mo>&amp;gt;</mo><msub><mi>p</mi><mi>j</mi></msub></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-msubsup"><span class="mjx-base"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">p<span class="mjx-sub"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">i<span class="mjx-mo MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">&gt;<span class="mjx-msubsup MJXc-space3"><span class="mjx-base"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">p<span class="mjx-sub"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">j<span class="MJX_Assistive_MathML">pi&gt;pj。<br> 同时小a的飞船还有一个耐久度<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>t</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">t<span class="MJX_Assistive_MathML">t,初始时为<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>1</mn></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">1号点的能量指数,若小a前往星球<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>j</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">j<span class="MJX_Assistive_MathML">j,那么飞船的耐久度会变为<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>t</mi><mo>&amp;#x2295;</mo><msub><mi>p</mi><mi>j</mi></msub></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">t<span class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">⊕<span class="mjx-msubsup MJXc-space2"><span class="mjx-base"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">p<span class="mjx-sub"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">j<span class="MJX_Assistive_MathML">t⊕pj(即<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>t</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">t<span class="MJX_Assistive_MathML">t异或<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>p</mi><mi>j</mi></msub></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-msubsup"><span class="mjx-base"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">p<span class="mjx-sub"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">j<span class="MJX_Assistive_MathML">pj,关于其定义请自行百度)<br> 小a想知道到达<span class="MathJax_Preview"><span class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi></math>"><span class="mjx-math"><span class="mjx-mrow"><span class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span class="MJX_Assistive_MathML">n号星球时耐久度最大为多少<br></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <div> 注意:对于每个位置来说,从它出发可以到达的位置仅与两者的 <span class="MathJax_Preview"><span id="MathJax-Element-14-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>p</mi></math>"><span id="MJXc-Node-52" class="mjx-math"><span id="MJXc-Node-53" class="mjx-mrow"><span id="MJXc-Node-54" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">p<span class="MJX_Assistive_MathML">p有关,与下标无关</span></span></span></span></span></span></span> </div> <h2><span class="MathJax_Preview">思路</span></h2> <p><span class="MathJax_Preview">背包问题的dp</span></p> <p><span class="MathJax_Preview">首先我们队这个能量指数进行排序,使得对于每个星球,在他之后的所有星球都可以从这个星球到达。</span></p> <p><span class="MathJax_Preview">然后就开始dp呗,我自己的代码好像不是正确的。所以再贴一份官方的代码</span></p> <h2><span class="MathJax_Preview">代码</span></h2> <div class="cnblogs_code"> <pre>#include &lt;bits/stdc++.h&gt; <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n; </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> node{ </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> a,b; }num[</span><span style="color: #800080;">3005</span><span style="color: #000000;">]; </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> cmp(node x,node y){ </span><span style="color: #0000ff;">if</span>(x.b==y.b) <span style="color: #0000ff;">return</span> x.a&lt;<span style="color: #000000;">y.b; </span><span style="color: #0000ff;">return</span> x.b&gt;<span style="color: #000000;">y.b; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main() { std::ios::sync_with_stdio(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">); cin</span>&gt;&gt;<span style="color: #000000;">n; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i&lt;n;i++<span style="color: #000000;">){ cin</span>&gt;&gt;<span style="color: #000000;">num[i].b; num[i].a</span>=<span style="color: #000000;">i; } sort(num,num</span>+<span style="color: #000000;">n,cmp); </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> s,t; </span><span style="color: #008000;">//</span><span style="color: #008000;">for(int i=0;i&lt;n;i++) cout&lt;&lt;num[i].a&lt;&lt;' '&lt;&lt;num[i].b&lt;&lt;endl;</span> <span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=<span style="color: #800080;">0</span>;i&lt;n;i++<span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(num[i].a==<span style="color: #800080;">0</span>) s=<span style="color: #000000;">i; </span><span style="color: #0000ff;">if</span>(num[i].a==n-<span style="color: #800080;">1</span>) t=<span style="color: #000000;">i; } </span><span style="color: #008000;">//</span><span style="color: #008000;">cout&lt;&lt;s&lt;&lt;' '&lt;&lt;t&lt;&lt;endl;</span> <span style="color: #0000ff;">if</span>(s&gt;t) cout&lt;&lt;-<span style="color: #800080;">1</span>&lt;&lt;<span style="color: #000000;">endl; </span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">int</span> dp[<span style="color: #800080;">3005</span><span style="color: #000000;">]; memset(dp,</span><span style="color: #800080;">0</span>,<span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(dp)); dp[s]</span>=<span style="color: #000000;">num[s].b; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i=s+<span style="color: #800080;">1</span>;i&lt;=t;i++<span style="color: #000000;">){ </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> j=s;j&lt;i;j++<span style="color: #000000;">){ dp[i]</span>=max(dp[i],dp[j]^<span style="color: #000000;">num[i].b); } } </span><span style="color: #0000ff;">if</span>(!dp[t]) cout&lt;&lt;-<span style="color: #800080;">1</span>&lt;&lt;<span style="color: #000000;">endl; </span><span style="color: #0000ff;">else</span> cout&lt;&lt;dp[t]&lt;&lt;<span style="color: #000000;">endl; } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></pre> </div> <div class="cnblogs_code"> <pre>#include&lt;bits/stdc++.h&gt; <span style="color: #0000ff;">#define</span> LL long long <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std; </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> MAXN = <span style="color: #800080;">10003</span>, INF = 1e9 + <span style="color: #800080;">10</span><span style="color: #000000;">; </span><span style="color: #0000ff;">void</span> chmin(<span style="color: #0000ff;">int</span> &amp;a, <span style="color: #0000ff;">int</span> b) {a = (a &lt; b ?<span style="color: #000000;"> a : b);} </span><span style="color: #0000ff;">void</span> chmax(<span style="color: #0000ff;">int</span> &amp;a, <span style="color: #0000ff;">int</span> b) {a = (a &gt; b ?<span style="color: #000000;"> a : b);} </span><span style="color: #0000ff;">int</span> sqr(<span style="color: #0000ff;">int</span> x) {<span style="color: #0000ff;">return</span> x *<span style="color: #000000;"> x;} inline </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> read() { </span><span style="color: #0000ff;">char</span> c = getchar(); <span style="color: #0000ff;">int</span> x = <span style="color: #800080;">0</span>, f = <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(c &lt; <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span> || c &gt; <span style="color: #800000;">'</span><span style="color: #800000;">9</span><span style="color: #800000;">'</span>) {<span style="color: #0000ff;">if</span>(c == <span style="color: #800000;">'</span><span style="color: #800000;">-</span><span style="color: #800000;">'</span>) f = -<span style="color: #800080;">1</span>; c =<span style="color: #000000;"> getchar();} </span><span style="color: #0000ff;">while</span>(c &gt;= <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span> &amp;&amp; c &lt;= <span style="color: #800000;">'</span><span style="color: #800000;">9</span><span style="color: #800000;">'</span>) x = x * <span style="color: #800080;">10</span> + c - <span style="color: #800000;">'</span><span style="color: #800000;">0</span><span style="color: #800000;">'</span>, c =<span style="color: #000000;"> getchar(); </span><span style="color: #0000ff;">return</span> x *<span style="color: #000000;"> f; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> N, mx; </span><span style="color: #0000ff;">bool</span><span style="color: #000000;"> f[MAXN][MAXN]; </span><span style="color: #0000ff;">struct</span><span style="color: #000000;"> Node { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> id, val; </span><span style="color: #0000ff;">bool</span> <span style="color: #0000ff;">operator</span> &lt; (<span style="color: #0000ff;">const</span> Node &amp;rhs) <span style="color: #0000ff;">const</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span> val &gt;<span style="color: #000000;"> rhs.val; } }a[MAXN]; signed main() { </span><span style="color: #008000;">//</span><span style="color: #008000;">freopen("a2.in", "r", stdin); </span><span style="color: #008000;">//</span><span style="color: #008000;">cout &lt;&lt; (457 ^ 23);</span> N =<span style="color: #000000;"> read(); </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">1</span>; i &lt;= N; i++) a[i].id = i, a[i].val = read(); mx = <span style="color: #800080;">6001</span><span style="color: #000000;">; sort(a </span>+ <span style="color: #800080;">1</span>, a + N + <span style="color: #800080;">1</span><span style="color: #000000;">); </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">1</span>, flag = <span style="color: #800080;">1</span>; i &lt;= N; i++<span style="color: #000000;">) { </span><span style="color: #0000ff;">if</span>(a[i].id == <span style="color: #800080;">1</span>) {flag = <span style="color: #800080;">0</span>, f[i][a[i].val] = <span style="color: #800080;">1</span>; <span style="color: #0000ff;">continue</span><span style="color: #000000;">;} </span><span style="color: #0000ff;">if</span>(flag) <span style="color: #0000ff;">continue</span><span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(a[i].id ==<span style="color: #000000;"> N) { </span><span style="color: #0000ff;">int</span> k = i - <span style="color: #800080;">1</span><span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(k &amp;&amp; a[i].val == a[k].val) k--<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span>(!k) <span style="color: #0000ff;">break</span><span style="color: #000000;">; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> j = mx; j &gt;= <span style="color: #800080;">0</span>; j--<span style="color: #000000;">) { f[i][j] </span>|= f[k][j ^<span style="color: #000000;"> a[i].val]; </span><span style="color: #0000ff;">if</span>(f[i][j]) {printf(<span style="color: #800000;">"</span><span style="color: #800000;">%d</span><span style="color: #800000;">"</span>, j); <span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">;} } </span><span style="color: #0000ff;">break</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span>(a[i].val == a[i - <span style="color: #800080;">1</span><span style="color: #000000;">].val) { memcpy(f[i], f[i </span>- <span style="color: #800080;">1</span>], <span style="color: #0000ff;">sizeof</span><span style="color: #000000;">(f[i])); } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> j = <span style="color: #800080;">0</span>; j &lt;= mx; j++<span style="color: #000000;">) f[i][j] </span>|= (f[i - <span style="color: #800080;">1</span>][j ^ a[i].val] | f[i - <span style="color: #800080;">1</span><span style="color: #000000;">][j]); } } puts(</span><span style="color: #800000;">"</span><span style="color: #800000;">-1</span><span style="color: #800000;">"</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></pre> </div> <h1>D题</h1> <h2>小a与黄金街道</h2> <p>链接:<a href="https://ac.nowcoder.com/acm/contest/317/D" target="_blank">https://ac.nowcoder.com/acm/contest/3***><br>来源:牛客网<br><br>小a和小b来到了一条布满了黄金的街道上。它们想要带几块黄金回去,然而这里的城管担心他们拿走的太多,于是要求小a和小b通过做一个游戏来决定最后得到的黄金的数量。<br> 游戏规则是这样的:<br> 假设道路长度为<span class="MathJax_Preview"><span id="MathJax-Element-1-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi></math>"><span id="MJXc-Node-1" class="mjx-math"><span id="MJXc-Node-2" class="mjx-mrow"><span id="MJXc-Node-3" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span class="MJX_Assistive_MathML">n米(左端点为<span class="MathJax_Preview"><span id="MathJax-Element-2-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>0</mn></math>"><span id="MJXc-Node-4" class="mjx-math"><span id="MJXc-Node-5" class="mjx-mrow"><span id="MJXc-Node-6" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">0<span class="MJX_Assistive_MathML">0,右端点为<span class="MathJax_Preview"><span id="MathJax-Element-3-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi></math>"><span id="MJXc-Node-7" class="mjx-math"><span id="MJXc-Node-8" class="mjx-mrow"><span id="MJXc-Node-9" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span class="MJX_Assistive_MathML">n),同时给出一个数<span class="MathJax_Preview"><span id="MathJax-Element-4-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>k</mi></math>"><span id="MJXc-Node-10" class="mjx-math"><span id="MJXc-Node-11" class="mjx-mrow"><span id="MJXc-Node-12" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span class="MJX_Assistive_MathML">k(下面会提到<span class="MathJax_Preview"><span id="MathJax-Element-5-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>k</mi></math>"><span id="MJXc-Node-13" class="mjx-math"><span id="MJXc-Node-14" class="mjx-mrow"><span id="MJXc-Node-15" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span class="MJX_Assistive_MathML">k的用法)<br> 设小a初始时的黄金数量为<span class="MathJax_Preview"><span id="MathJax-Element-6-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>A</mi></math>"><span id="MJXc-Node-16" class="mjx-math"><span id="MJXc-Node-17" class="mjx-mrow"><span id="MJXc-Node-18" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">A<span class="MJX_Assistive_MathML">A,小b初始时的黄金数量为<span class="MathJax_Preview"><span id="MathJax-Element-7-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>B</mi></math>"><span id="MJXc-Node-19" class="mjx-math"><span id="MJXc-Node-20" class="mjx-mrow"><span id="MJXc-Node-21" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">B<span class="MJX_Assistive_MathML">B<br> 小a从<span class="MathJax_Preview"><span id="MathJax-Element-8-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>1</mn></math>"><span id="MJXc-Node-22" class="mjx-math"><span id="MJXc-Node-23" class="mjx-mrow"><span id="MJXc-Node-24" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">1出发走向<span class="MathJax_Preview"><span id="MathJax-Element-9-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi><mo>&amp;#x2212;</mo><mn>1</mn></math>"><span id="MJXc-Node-25" class="mjx-math"><span id="MJXc-Node-26" class="mjx-mrow"><span id="MJXc-Node-27" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span id="MJXc-Node-28" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">−<span id="MJXc-Node-29" class="mjx-mn MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">n−1,小b从<span class="MathJax_Preview"><span id="MathJax-Element-10-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi><mo>&amp;#x2212;</mo><mn>1</mn></math>"><span id="MJXc-Node-30" class="mjx-math"><span id="MJXc-Node-31" class="mjx-mrow"><span id="MJXc-Node-32" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span id="MJXc-Node-33" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">−<span id="MJXc-Node-34" class="mjx-mn MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">n−1出发走向<span class="MathJax_Preview"><span id="MathJax-Element-11-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>1</mn></math>"><span id="MJXc-Node-35" class="mjx-math"><span id="MJXc-Node-36" class="mjx-mrow"><span id="MJXc-Node-37" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">1,两人的速度均为<span class="MathJax_Preview"><span id="MathJax-Element-12-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mn>1</mn><mi>m</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo>/</mo></mrow><mi>s</mi></math>"><span id="MJXc-Node-38" class="mjx-math"><span id="MJXc-Node-39" class="mjx-mrow"><span id="MJXc-Node-40" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">1<span id="MJXc-Node-41" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">m<span id="MJXc-Node-42" class="mjx-texatom"><span id="MJXc-Node-43" class="mjx-mrow"><span id="MJXc-Node-44" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">/<span id="MJXc-Node-45" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">s<span class="MJX_Assistive_MathML">1m/s<br> 假设某一时刻(必须为整数)小a的位置为<span class="MathJax_Preview"><span id="MathJax-Element-13-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>x</mi></math>"><span id="MJXc-Node-46" class="mjx-math"><span id="MJXc-Node-47" class="mjx-mrow"><span id="MJXc-Node-48" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">x<span class="MJX_Assistive_MathML">x,小b的位置为<span class="MathJax_Preview"><span id="MathJax-Element-14-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>y</mi></math>"><span id="MJXc-Node-49" class="mjx-math"><span id="MJXc-Node-50" class="mjx-mrow"><span id="MJXc-Node-51" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">y<span class="MJX_Assistive_MathML">y,若<span class="MathJax_Preview"><span id="MathJax-Element-15-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>g</mi><mi>c</mi><mi>d</mi><mo stretchy=&quot;false&quot;>(</mo><mi>n</mi><mo>,</mo><mi>x</mi><mo stretchy=&quot;false&quot;>)</mo><mo>=</mo><mn>1</mn></math>"><span id="MJXc-Node-52" class="mjx-math"><span id="MJXc-Node-53" class="mjx-mrow"><span id="MJXc-Node-54" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">g<span id="MJXc-Node-55" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">c<span id="MJXc-Node-56" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">d<span id="MJXc-Node-57" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">(<span id="MJXc-Node-58" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span id="MJXc-Node-59" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-60" class="mjx-mi MJXc-space1"><span class="mjx-char MJXc-TeX-math-I">x<span id="MJXc-Node-61" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">)<span id="MJXc-Node-62" class="mjx-mo MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">=<span id="MJXc-Node-63" class="mjx-mn MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">gcd(n,x)=1且<span class="MathJax_Preview"><span id="MathJax-Element-16-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>g</mi><mi>c</mi><mi>d</mi><mo stretchy=&quot;false&quot;>(</mo><mi>n</mi><mo>,</mo><mi>y</mi><mo stretchy=&quot;false&quot;>)</mo><mo>=</mo><mn>1</mn></math>"><span id="MJXc-Node-64" class="mjx-math"><span id="MJXc-Node-65" class="mjx-mrow"><span id="MJXc-Node-66" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">g<span id="MJXc-Node-67" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">c<span id="MJXc-Node-68" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">d<span id="MJXc-Node-69" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">(<span id="MJXc-Node-70" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span id="MJXc-Node-71" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">,<span id="MJXc-Node-72" class="mjx-mi MJXc-space1"><span class="mjx-char MJXc-TeX-math-I">y<span id="MJXc-Node-73" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">)<span id="MJXc-Node-74" class="mjx-mo MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">=<span id="MJXc-Node-75" class="mjx-mn MJXc-space3"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">gcd(n,y)=1,那么小a的黄金数量<span class="MathJax_Preview"><span id="MathJax-Element-17-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>A</mi></math>"><span id="MJXc-Node-76" class="mjx-math"><span id="MJXc-Node-77" class="mjx-mrow"><span id="MJXc-Node-78" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">A<span class="MJX_Assistive_MathML">A会变为<span class="MathJax_Preview"><span id="MathJax-Element-18-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>A</mi><mo>&amp;#x2217;</mo><msup><mi>k</mi><mi>x</mi></msup><mo stretchy=&quot;false&quot;>(</mo><mi>k</mi><mi>g</mi><mo stretchy=&quot;false&quot;>)</mo></math>"><span id="MJXc-Node-79" class="mjx-math"><span id="MJXc-Node-80" class="mjx-mrow"><span id="MJXc-Node-81" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">A<span id="MJXc-Node-82" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">∗<span id="MJXc-Node-83" class="mjx-msubsup MJXc-space2"><span class="mjx-base"><span id="MJXc-Node-84" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span class="mjx-sup"><span id="MJXc-Node-85" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">x<span id="MJXc-Node-86" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">(<span id="MJXc-Node-87" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span id="MJXc-Node-88" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">g<span id="MJXc-Node-89" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">)<span class="MJX_Assistive_MathML">A∗kx(kg),小b的黄金数量<span class="MathJax_Preview"><span id="MathJax-Element-19-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>B</mi></math>"><span id="MJXc-Node-90" class="mjx-math"><span id="MJXc-Node-91" class="mjx-mrow"><span id="MJXc-Node-92" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">B<span class="MJX_Assistive_MathML">B会变为<span class="MathJax_Preview"><span id="MathJax-Element-20-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>B</mi><mo>&amp;#x2217;</mo><msup><mi>k</mi><mi>y</mi></msup><mo stretchy=&quot;false&quot;>(</mo><mi>k</mi><mi>g</mi><mo stretchy=&quot;false&quot;>)</mo></math>"><span id="MJXc-Node-93" class="mjx-math"><span id="MJXc-Node-94" class="mjx-mrow"><span id="MJXc-Node-95" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">B<span id="MJXc-Node-96" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">∗<span id="MJXc-Node-97" class="mjx-msubsup MJXc-space2"><span class="mjx-base"><span id="MJXc-Node-98" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span class="mjx-sup"><span id="MJXc-Node-99" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">y<span id="MJXc-Node-100" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">(<span id="MJXc-Node-101" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">k<span id="MJXc-Node-102" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">g<span id="MJXc-Node-103" class="mjx-mo"><span class="mjx-char MJXc-TeX-main-R">)<span class="MJX_Assistive_MathML">B∗ky(kg)<br> 当小a到达<span class="MathJax_Preview"><span id="MathJax-Element-21-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi><mo>&amp;#x2212;</mo><mn>1</mn></math>"><span id="MJXc-Node-104" class="mjx-math"><span id="MJXc-Node-105" class="mjx-mrow"><span id="MJXc-Node-106" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">n<span id="MJXc-Node-107" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">−<span id="MJXc-Node-108" class="mjx-mn MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">1<span class="MJX_Assistive_MathML">n−1时游戏结束<br> 小a想知道在游戏结束时<span class="MathJax_Preview"><span id="MathJax-Element-22-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>A</mi><mo>+</mo><mi>B</mi></math>"><span id="MJXc-Node-109" class="mjx-math"><span id="MJXc-Node-110" class="mjx-mrow"><span id="MJXc-Node-111" class="mjx-mi"><span class="mjx-char MJXc-TeX-math-I">A<span id="MJXc-Node-112" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">+<span id="MJXc-Node-113" class="mjx-mi MJXc-space2"><span class="mjx-char MJXc-TeX-math-I">B<span class="MJX_Assistive_MathML">A+B的值<br> 答案对<span class="MathJax_Preview"><span id="MathJax-Element-23-Frame" class="mjx-chtml MathJax_CHTML" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msup><mn>10</mn><mn>9</mn></msup><mo>+</mo><mn>7</mn></math>"><span id="MJXc-Node-114" class="mjx-math"><span id="MJXc-Node-115" class="mjx-mrow"><span id="MJXc-Node-116" class="mjx-msubsup"><span class="mjx-base"><span id="MJXc-Node-117" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">10<span class="mjx-sup"><span id="MJXc-Node-118" class="mjx-mn"><span class="mjx-char MJXc-TeX-main-R">9<span id="MJXc-Node-119" class="mjx-mo MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">+<span id="MJXc-Node-120" class="mjx-mn MJXc-space2"><span class="mjx-char MJXc-TeX-main-R">7<span class="MJX_Assistive_MathML">109+7取模</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></p> <h2><span class="MathJax_Preview">思路</span></h2> <p><span class="MathJax_Preview">首先我们需要理解一个知识点,对于gcd(n,x)=1 时,x&lt;n,gcd(n,n-x)=1,这个是满足的</span></p> <p><span class="MathJax_Preview">那这个问题就转换成我们要求在n以内有多少个与n互质的个数,也就是欧拉函数。</span></p> <p><span class="MathJax_Preview">当然只求个数是不行的,我们需要知道这些数的和,就为<span id="MathJax-Element-37-Frame" class="MathJax" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>&amp;#x03D5;</mi><mo stretchy=&quot;false&quot;>(</mo><mi>n</mi><mo stretchy=&quot;false&quot;>)</mo></math>"><span id="MathJax-Span-674" class="math"><span id="MathJax-Span-675" class="mrow"><span id="MathJax-Span-676" class="mi">ϕ<span id="MathJax-Span-677" class="mo">(<span id="MathJax-Span-678" class="mi">n<span id="MathJax-Span-679" class="mo">)*n/2,这个可以推一下</span></span></span></span></span></span></span><br></span></p> <h2>代码</h2> <div class="cnblogs_code"> <pre>#include &lt;bits/stdc++.h&gt; <span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span><span style="color: #000000;"> std; </span><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> mod=1e9+<span style="color: #800080;">7</span><span style="color: #000000;">; </span><span style="color: #0000ff;">int</span> phi(<span style="color: #0000ff;">int</span><span style="color: #000000;"> n) { </span><span style="color: #0000ff;">int</span> ans =<span style="color: #000000;"> n; </span><span style="color: #0000ff;">for</span>(<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">2</span>;i*i &lt;= n;i++)<span style="color: #0000ff;">if</span>(n % i == <span style="color: #800080;">0</span><span style="color: #000000;">) { ans </span>-= ans/<span style="color: #000000;">i; </span><span style="color: #0000ff;">while</span>(n % i == <span style="color: #800080;">0</span><span style="color: #000000;">) n </span>/=<span style="color: #000000;"> i; } </span><span style="color: #0000ff;">if</span>(n != <span style="color: #800080;">1</span><span style="color: #000000;">) ans </span>-= ans/<span style="color: #000000;">n; </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ans; } </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> power_mod(<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> a,<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> b){ </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> ans=<span style="color: #800080;">1</span><span style="color: #000000;">; a</span>=a%<span style="color: #000000;">mod; </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(b){ </span><span style="color: #0000ff;">if</span>(b&amp;<span style="color: #800080;">1</span>) ans=ans*a%<span style="color: #000000;">mod; a</span>=a*a%<span style="color: #000000;">mod; b</span>&gt;&gt;=<span style="color: #800080;">1</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> ans; } </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main() { </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> n; </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span><span style="color: #000000;"> k,a,b; ios::sync_with_stdio(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">); cin</span>&gt;&gt;n&gt;&gt;k&gt;&gt;a&gt;&gt;<span style="color: #000000;">b; </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> x=(<span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span>)phi(n)*n/<span style="color: #800080;">2</span><span style="color: #000000;">; a</span>=a%<span style="color: #000000;">mod; b</span>=b%<span style="color: #000000;">mod; </span><span style="color: #0000ff;">long</span> <span style="color: #0000ff;">long</span> ans=(a+b)*power_mod(k,x)%<span style="color: #000000;">mod; cout</span>&lt;&lt;ans&lt;&lt;<span style="color: #000000;">endl; </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">0</span><span style="color: #000000;">; }</span></pre> </div> <p>&nbsp;</p>