智力体验(?)

1980 字
10 分钟
智力体验(?)

欢迎来到今天的智力体验。这意味着您将重新定义您的智力。

CF原题大赏

学习取模

题面

原题:

传送门:Problem - 1562A - Codeforces

You are given two integers $l$ and $r$, $l≤r$. Find the largest possible value of amodbamodb over all pairs $(a,b)$ of integers for which $r≥a≥b≥l$.

As a reminder, $a mod b$ is a remainder we get when dividing $a$ by $b$. For example, $26mod8=2$.

题目大意:

给您一个范围lr,使得ablr的范围中且a mod b最大,输出该a mod b的值。

解题

  • 对于任意的一个数a,要使a mod b尽可能大,容易得到b的值应为a÷2+1
  • 而现在b的范围已知,即b可以根据a的取值自由选择,那么应让a的取值尽量大,a mod b才会最大。

由上可知,我们需要对lr的情况进行讨论。

  • (r/2+1)≥l时,可以使得a取到最大值r,而b也可以取到a÷2+1的理想值。
  • (r/2+1)<l时,a可以是最大值r,但b能取到的最优解为l。此时答案为r mod l

示例代码

#include
   

using namespace std; int t; int l,r; int main(){ cin>>t; while(t—){ cin>>l>>r; if((r/2+1)>=l){ cout<<((r-1)/2)< <endl; }else{="" cout<<r%l<<endl;="" }="" return="" 0;="" <="" re="">

<p>
<!-- /wp:enlighter/codeblock --></p>
<p>
<!-- wp:heading {"level":1} --></p>
<h1>学习异或</h1>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:paragraph --></p>
<p>
<s>
好奇的宝宝
</s>Bob又在搞事情了。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:heading --></p>
<h2>题面</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:heading {"level":3} --></p>
<h3>原题</h3>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:paragraph --></p>
<p>传送门:<a href="https://codeforces.com/contest/1567/problem/B">Problem - B - Codeforces</a></p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:quote --></p>
<blockquote class="wp-block-quote">
<p>Alice gave Bob two integers&nbsp;aa&nbsp;and&nbsp;bb&nbsp;(a&gt;0a&gt;0&nbsp;and&nbsp;b≥0b≥0). Being a curious boy, Bob wrote down an array of&nbsp;non-negative&nbsp;integers with&nbsp;MEXMEX&nbsp;value of all elements equal to&nbsp;aa&nbsp;and&nbsp;XORXOR&nbsp;value of all elements equal to&nbsp;bb.</p>
<p>What is the&nbsp;shortest&nbsp;possible length of the array Bob wrote?</p>
<p>Recall that the&nbsp;MEXMEX&nbsp;(<a href="https://vjudge.net/problem/description/163581?1631434219000">Minimum EXcluded</a>) of an array is the minimum non-negative integer that does&nbsp;not&nbsp;belong to the array and the&nbsp;XORXOR&nbsp;of an array is the&nbsp;<a href="https://en.wikipedia.org/wiki/Bitwise_operation#XOR">bitwise XOR</a>&nbsp;of all the elements of the array.</p>
</blockquote>
<p>
<!-- /wp:quote --></p>
<p>
<!-- wp:heading {"level":3} --></p>
<h3>大意</h3>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:paragraph --></p>
<p>给两个数<code>a</code>,<code>b</code>,表示在一个集合${0,1, ... , a-1, ... , x}$中,有<code>x-任意元素=b</code>的存在。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p><code>a</code>为<code>EXcluded</code>数,意思是在这个集合中,<strong>最小</strong>的<strong>不存在</strong>的<strong>非负数</strong>为<code>a</code>。并且在这个集合中,一定有两个数之差为<code>b</code>,输出这个集合最少有几个数。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:heading --></p>
<h2>补充</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:list {"ordered":true} --></p>
<ol>
<li>如果<code>a^b=c</code>,则<code>a^c=b</code>。</li>
<li><code>a^(a+1)^(a+2)^(a+3)=0 (a为4的倍数)</code> ,证明过程可参考二进制,末两位分别为<code>00</code>,<code>01</code>,<code>10</code>,<code>11</code>。</li>
</ol>
<p>
<!-- /wp:list --></p>
<p>
<!-- wp:heading --></p>
<h2>解题</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:paragraph --></p>
<p>这题需要对多种情况进行分类讨论,根据<code>补充2</code>发现,其规律为4个一循环。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:heading --></p>
<h2>示例代码</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:enlighter/codeblock --></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include
<bits tdc++.h="">

using namespace std; int t; int a,b; int main(){ cin>>t; while(t—){ int ans=0; cin>>a>>b; switch (a%4) { case 1: ans=a-1; break; case 2: ans=1; break; case 3: ans=a; break; default: ans=0; break; } if(ans==b)cout< <a<<endl; else="" if((b^ans)!=“a)cout<<a+1<<endl;” cout<<a+2<<endl;="" }="" return="" 0;="" }<="" re="">

<p>
<!-- /wp:enlighter/codeblock --></p>
<p>
<!-- wp:heading {"level":1} --></p>
<h1>学习加法</h1>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:heading --></p>
<h2>题面</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:heading {"level":3} --></p>
<h3>原题</h3>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:paragraph --></p>
<p>传送门:<a href="https://codeforces.com/problemset/problem/1567/C">Problem - 1567C - Codeforces</a></p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:separator --></p>
<hr class="wp-block-separator" />
<!-- /wp:separator -->
<p></p>
<p>
<!-- wp:group --></p>
<div class="wp-block-group">
<!-- wp:paragraph -->
<p></p>
<p>Alice has just learned addition. However, she hasn't learned the concept of &quot;carrying&quot; fully&nbsp;— instead of carrying to the&nbsp;next&nbsp;column, she carries to the column&nbsp;two columns to the left.</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph {"align":"center"} --></p>
<p class="has-text-align-center">For example, the&nbsp;regular&nbsp;way to evaluate the sum&nbsp;2039+29762039+2976&nbsp;would be as shown:<img src="https://vj.ppsucxtt.cn/fdf82ba5355116a4525b30b0470264b9?v=1631085496" width="208" height="89" /></p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>However, Alice evaluates it as shown:<img src="https://vj.ppsucxtt.cn/73d845a6f53bdbc0556975e371489630?v=1631085496" width="207" height="89" /></p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>In particular, this is what she does:</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:list --></p>
<ul>
<li>add&nbsp;99&nbsp;and&nbsp;66&nbsp;to make&nbsp;1515, and carry the&nbsp;11&nbsp;to the column&nbsp;two columns to the left, i.&nbsp;e. to the column &quot;00&nbsp;99&quot;;</li>
<li>add&nbsp;33&nbsp;and&nbsp;77&nbsp;to make&nbsp;1010&nbsp;and carry the&nbsp;11&nbsp;to the column&nbsp;two columns to the left, i.&nbsp;e. to the column &quot;22&nbsp;22&quot;;</li>
<li>add&nbsp;11,&nbsp;00, and&nbsp;99&nbsp;to make&nbsp;1010&nbsp;and carry the&nbsp;11&nbsp;to the column&nbsp;two columns to the left, i.&nbsp;e. to the column above the plus sign;</li>
<li>add&nbsp;11,&nbsp;22&nbsp;and&nbsp;22&nbsp;to make&nbsp;55;</li>
<li>add&nbsp;11&nbsp;to make&nbsp;11.</li>
</ul>
<p>
<!-- /wp:list --></p>
<p>
<!-- wp:paragraph --></p>
<p>Thus, she ends up with the incorrect result of&nbsp;1500515005.</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>Alice comes up to Bob and says that she has added two numbers to get a result of&nbsp;nn. However, Bob knows that Alice adds in her own way. Help Bob find the number of&nbsp;ordered pairs of positive integers&nbsp;such that when Alice adds them, she will get a result of&nbsp;nn. Note that pairs&nbsp;(a,b)(a,b)&nbsp;and&nbsp;(b,a)(b,a)&nbsp;are considered different if&nbsp;a≠ba≠b.</p>
<p>
<!-- /wp:paragraph --></p>
</div>
<p>
<!-- /wp:group --></p>
<p>
<!-- wp:heading {"level":3} --></p>
<h3>大意</h3>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:quote --></p>
<blockquote class="wp-block-quote">
<p>爱丽丝刚刚学会了加法。然而,她还没有完全学会 &quot;携带 &quot;的概念--她不是携带到下一列,而是携带到左边两列的列。</p>
<p>例如,评估2039+2976的常规方法是如图所示。</p>
<p>然而,爱丽丝是按照图中的方式进行评估的。</p>
<p>具体来说,她是这样做的。</p>
<p>将9和6相加为15,并将1带到左边两列,即 &quot;0 9 &quot;列。<br />加3和7得10,并把1带到左边两列,即 &quot;2 2 &quot;列。<br />加1、0和9组成10,并将1带到左边两列,即加号上面的那一列。<br />加1,2和2组成5。<br />加1为1。<br />因此,她最后得到的结果是不正确的15005。<br />爱丽丝走过来对鲍勃说,她把两个数字相加得到的结果是n,但是,鲍勃知道爱丽丝是用自己的方式加的。请帮助鲍勃找出有秩序的正整数对,使爱丽丝将它们相加后得到的结果是n。请注意,如果a≠b,则成对的(a,b)和(b,a)被视为不同。</p>
<p>输入<br />输入由多个测试案例组成。第一行包含一个整数t(1≤t≤1000)--测试案例的数量。测试用例的描述如下。</p>
<p>每个测试用例的唯一一行包含一个整数n(2≤n≤109)--Alice给Bob看的数字。</p>
<p>输出<br />对于每个测试用例,输出一个整数 - 有序的正整数对的数量,以便当Alice将它们相加时,她将得到一个n的结果。</p>
<p><cite>—— 通过 www.DeepL.com/Translator 翻译</cite></p>
</blockquote>
<p>
<!-- /wp:quote --></p>
<p>
<!-- wp:heading --></p>
<h2>解题</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:paragraph --></p>
<p>本题可思考Alice的加法方式和正常进位的加法方式。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>Alice将个位的进位进到了百位,百位的进到了万位……反过来,十位的进到千位,如是往复。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>不难发现,将互不相关的奇数位和偶数位分别提出来组成新的正常进位的数字,利用排列组合的原理即可求出最终答案。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>对于自然数<code>n</code>,使得<code>a+b=n (a,b为正整数)</code>的方案数为<code>n-1</code>种。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:paragraph --></p>
<p>通过比较发现,当<strong>A的奇偶都为0</strong>,且<strong>B的奇偶都为0</strong>时,应该将答案减去这两个舍去的情况。</p>
<p>
<!-- /wp:paragraph --></p>
<p>
<!-- wp:heading --></p>
<h2>示例代码</h2>
<p>
<!-- /wp:heading --></p>
<p>
<!-- wp:enlighter/codeblock --></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">#include
<bits tdc++.h="">

using namespace std; int t; string n; int num1,num2; int main(){ cin>>t; while (t—){ cin>>n; num1=num2=0; for(int i=0;i <n.length();i++){ if(i%2=“=0)” num1=“num110+n[i]-‘0’;” else="" num2=“num210+n[i]-‘0’;” }="" cout<<”=""> ”< <num1<<” “<<num2<<endl;="" cout<<(num1+1)*(num2+1)-2<<endl;="" }="" return="" 0;="" }<="" re="">

<p>
<!-- /wp:enlighter/codeblock --></p>
</num1<<">
</n.length();i++){>
</bits></pre>
</a<<endl;>
</bits></pre>
</endl;>

支持与分享

如果这篇文章对你有帮助,欢迎分享给更多人或赞助支持!

赞助
智力体验(?)
https://www.0x3f.foo/posts/智力体验/
作者
Dignite
发布于
2021-09-12
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
Dignite
When nothing goes right, go left.
公告
欢迎来到我的博客!这是一则示例公告。
分类
标签
站点统计
文章
146
分类
5
标签
271
总字数
314,753
运行时长
0
最后活动
0 天前

目录