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$.
题目大意:
给您一个范围l和r,使得a和b在l到r的范围中且a mod b最大,输出该a mod b的值。
解题
对于任意的一个数a,要使a mod b尽可能大,容易得到b的值应为a÷2+1。
而现在b的范围已知,即b可以根据a的取值自由选择,那么应让a的取值尽量大,a mod b才会最大。
由上可知,我们需要对l和r的情况进行讨论。
当(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="">
1
<p>
2
<!-- /wp:enlighter/codeblock --></p>
3
4
<p>
5
<!-- wp:heading {"level":1} --></p>
6
7
<h1>学习异或</h1>
8
9
<p>
10
<!-- /wp:heading --></p>
11
12
<p>
13
<!-- wp:paragraph --></p>
14
15
<p>
16
<s>
17
好奇的宝宝
18
</s>Bob又在搞事情了。</p>
19
20
<p>
21
<!-- /wp:paragraph --></p>
22
23
<p>
24
<!-- wp:heading --></p>
25
26
<h2>题面</h2>
27
28
<p>
29
<!-- /wp:heading --></p>
30
31
<p>
32
<!-- wp:heading {"level":3} --></p>
33
34
<h3>原题</h3>
35
36
<p>
37
<!-- /wp:heading --></p>
38
39
<p>
40
<!-- wp:paragraph --></p>
41
42
<p>传送门:<a href="https://codeforces.com/contest/1567/problem/B">Problem - B - Codeforces</a></p>
43
44
<p>
45
<!-- /wp:paragraph --></p>
46
47
<p>
48
<!-- wp:quote --></p>
49
50
<blockquote class="wp-block-quote">
51
<p>Alice gave Bob two integers aa and bb (a>0a>0 and b≥0b≥0). Being a curious boy, Bob wrote down an array of non-negative integers with MEXMEX value of all elements equal to aa and XORXOR value of all elements equal to bb.</p>
52
53
<p>What is the shortest possible length of the array Bob wrote?</p>
54
55
<p>Recall that the MEXMEX (<a href="https://vjudge.net/problem/description/163581?1631434219000">Minimum EXcluded</a>) of an array is the minimum non-negative integer that does not belong to the array and the XORXOR of an array is the <a href="https://en.wikipedia.org/wiki/Bitwise_operation#XOR">bitwise XOR</a> of all the elements of the array.</p>
<p>Alice has just learned addition. However, she hasn't learned the concept of "carrying" fully — instead of carrying to the next column, she carries to the column two columns to the left.</p>
52
53
<p>
54
<!-- /wp:paragraph --></p>
55
56
<p>
57
<!-- wp:paragraph {"align":"center"} --></p>
58
59
<p class="has-text-align-center">For example, the regular way to evaluate the sum 2039+29762039+2976 would be as shown:<img src="https://vj.ppsucxtt.cn/fdf82ba5355116a4525b30b0470264b9?v=1631085496" width="208" height="89" /></p>
60
61
<p>
62
<!-- /wp:paragraph --></p>
63
64
<p>
65
<!-- wp:paragraph --></p>
66
67
<p>However, Alice evaluates it as shown:<img src="https://vj.ppsucxtt.cn/73d845a6f53bdbc0556975e371489630?v=1631085496" width="207" height="89" /></p>
68
69
<p>
70
<!-- /wp:paragraph --></p>
71
72
<p>
73
<!-- wp:paragraph --></p>
74
75
<p>In particular, this is what she does:</p>
76
77
<p>
78
<!-- /wp:paragraph --></p>
79
80
<p>
81
<!-- wp:list --></p>
82
83
<ul>
84
85
<li>add 99 and 66 to make 1515, and carry the 11 to the column two columns to the left, i. e. to the column "00 99";</li>
86
87
<li>add 33 and 77 to make 1010 and carry the 11 to the column two columns to the left, i. e. to the column "22 22";</li>
88
89
<li>add 11, 00, and 99 to make 1010 and carry the 11 to the column two columns to the left, i. e. to the column above the plus sign;</li>
<p>Thus, she ends up with the incorrect result of 1500515005.</p>
104
105
<p>
106
<!-- /wp:paragraph --></p>
107
108
<p>
109
<!-- wp:paragraph --></p>
110
111
<p>Alice comes up to Bob and says that she has added two numbers to get a result of nn. However, Bob knows that Alice adds in her own way. Help Bob find the number of ordered pairs of positive integers such that when Alice adds them, she will get a result of nn. Note that pairs (a,b)(a,b) and (b,a)(b,a) are considered different if a≠ba≠b.</p>