Codeforces Round #316 (Div. 2) B. Simple Game (模拟)

One day Misha and Andrew were playing a very simple game. First, each player chooses an integer in the range from1ton. Let’s assume that Misha chose numberm, and Andrew chose numbera.

Then, by using a random generator they choose a random integercin the range between1andn(any integer from1tonis chosen with the same probability), after which the winner is the player, whose number was closer toc. The boys agreed that ifmandaare located on the same distance fromc, Misha wins.

Andrew wants to win very much, so he asks you to help him. You know the number selected by Misha, and numbern. You need to determine which value ofaAndrew must choose, so that the probability of his victory is the highest possible.

More formally, you need to find such integera(1≤a≤n), that the probability thatis maximal, wherecis the equiprobably chosen integer from1ton(inclusive).

Input

The first line contains two integersnandm(1≤m≤n≤109) — the range of numbers in the game, and the number selected by Misha respectively.

Output

Print a single number — such valuea, that probability that Andrew wins is the highest. If there are multiple such values, print the minimum of them.

Sample test(s)

input

3 1

output

2

input

4 3

output

2

Note

In the first sample test: Andrew wins ifcis equal to2or3. The probability that Andrew wins is2/3. If Andrew choosesa=3, the probability of winning will be1/3. Ifa=1, the probability of winning is0.

In the second sample test: Andrew wins ifcis equal to1and2. The probability that Andrew wins is1/2. For other choices ofathe probability of winning is less.

WA点在要特别考虑n=1时的情况。

#include<iostream>#include<cstdio>#include<cstring>using namespace std;int main(){int n,m,mid,ans;cin>>n>>m;if(n%2==0) {mid=n/2;if(m<=mid) ans=m+1;else ans=m-1;}else {mid=n/2+1;if(m<mid) ans=m+1;else ans=m-1;}if(ans==0) ans=1;printf("%d\n",ans);return 0;}

,与那些新人和旧人们共同经历吧!

Codeforces Round #316 (Div. 2) B. Simple Game (模拟)

相关文章:

你感兴趣的文章:

标签云: