A. Vladik and flights
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output:standard output
Vladik is a competitive programmer. This year he is going to win the International Olympiad in Informatics. But it is not as easy as it sounds: the question Vladik face now is to find the cheapest way to get to the olympiad.
Vladik knows n airports. All the airports are located on a straight line. Each airport has unique id from 1 to n, Vladik’s house is situated next to the airport with id a, and the place of the olympiad is situated next to the airport with id b. It is possible that Vladik’s house and the place of the olympiad are located near the same airport.
To get to the olympiad, Vladik can fly between any pair of airports any number of times, but he has to start his route at the airport a and finish it at the airport b.
Each airport belongs to one of two companies. The cost of flight from the airport i to the airport j is zero if both airports belong to the same company, and |i - j| if they belong to different companies.
Print the minimum cost Vladik has to pay to get to the olympiad.
Input
The first line contains three integers n, a, and b (1 ≤ n ≤ 105, 1 ≤ a, b ≤ n) — the number of airports, the id of the airport from which Vladik starts his route and the id of the airport which he has to reach.
The second line contains a string with length n, which consists only of characters 0 and 1. If the i-th character in this string is 0, then i-th airport belongs to first company, otherwise it belongs to the second.
Output
Print single integer — the minimum cost Vladik has to pay to get to the olympiad.
Examples
Input
4 1 4
1010
Output
1
Input
5 5 2
10110
Output
0
Note
In the first example Vladik can fly to the airport 2 at first and pay |1 - 2| = 1 (because the airports belong to different companies), and then fly from the airport 2 to the airport 4 for free (because the airports belong to the same company). So the cost of the whole flight is equal to 1. It’s impossible to get to the olympiad for free, so the answer is equal to 1.
In the second example Vladik can fly directly from the airport 5 to the airport 2, because they belong to the same company.题意:有n个飞机场,起始点是a,目的地是b,如果两个飞机场属于同一家公司费用为0,否则费用为两者坐标的绝对值。问a到b的最小花费。
题解:如果a,b属于同一家花费为0,否则花费为1,因为一定存在两个机场相临且不属于同一家,在那变一下就好。
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
char aa[100006];
int main()
{
int n,a,b;
cin>>n>>a>>b;
scanf("%s",aa+1);
if(aa[a]==aa[b])
cout<<0<<endl;
else
cout<<1<<endl;
}
B. Chloe and the sequence
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output
Chloe, the same as Vladik, is a competitive programmer. She didn’t have any problems to get to the olympiad like Vladik, but she was confused by the task proposed on the olympiad.
Let’s consider the following algorithm of generating a sequence of integers. Initially we have a sequence consisting of a single element equal to 1. Then we perform (n - 1) steps. On each step we take the sequence we’ve got on the previous step, append it to the end of itself and insert in the middle the minimum positive integer we haven’t used before. For example, we get the sequence [1, 2, 1] after the first step, the sequence [1, 2, 1, 3, 1, 2, 1] after the second step.
The task is to find the value of the element with index k (the elements are numbered from 1) in the obtained sequence, i. e. after (n - 1) steps.
Please help Chloe to solve the problem!
Input
The only line contains two integers n and k (1 ≤ n ≤ 50, 1 ≤ k ≤ 2n - 1).
Output
Print single integer — the integer at the k-th position in the obtained sequence.
Examples
Input
3 2
Output
2
Input
4 8
Output
4
Note
In the first sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1]. The number on the second position is 2.
In the second sample the obtained sequence is [1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1]. The number on the eighth position is 4.
题意:n-1操作,每次往数列后加未出现过的最小数字后再将原数列复制一次粘到新序列后,问第n-1次操作后 第k位是几。
题解:看这个序列 [1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2, 1].
1出现坐标对应的二进制为[1,11,101,111,1001,1011,1111]
2.。。。。。。。。。。。[10,110,1010,1110]
3。。。。。。。。。。。[100,1100]
4。。。。。。。。。。。[1000]
规律二进制第一个’1’出现的位置既为该位的数字。
代码:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[10000];
int main()
{
ll n,k;
cin>>n>>k;
int cnt=0;
while(k)
{
a[cnt++]=k%2;
k/=2;
}
int ans=0;
for(int i=0;i<cnt;i++)
{
ans++;
if(a[i]==1)
{
break;
}
}
printf("%d\n",ans);
}