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  给你长度魏n的01串  符号相同表示机场属于同一家公司  相同公司通航免费 不同公司通航费用为abs(i-j) 输出最小的花费

题解:细细分析一下  相同公司的花费为零 不同公司任意点的花费为1   水

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 const int N=100005;
14 using namespace  std;
15 int n,a,b;
16 char c[100005];
17 int main()
18 {
19     scanf("%d %d %d",&n,&a,&b);
20     getchar();
21     for(int i=1;i<=n;i++)
22         scanf("%c",&c[i]);
23     if(c[a]==c[b])
24     {
25         printf("0\n");
26         return 0;
27     }
28     else
29     {
30         printf("1\n");
31     }
32     return 0;
33 }

 

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.

 题意:按照题目要求生成序列 问第k个数是什么

 题解:将k转换为二进制 可以发现结果为末尾零的个数加一

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 const int N=100005;
14 using namespace  std;
15 ll  n,k;
16 int main()
17 {
18     scanf("%I64d %I64d",&k,&n);
19     ll ans=0;
20     while(((n>>ans)&1)==0) ans++;
21     printf("%d\n",ans+1);
22     return 0;
23 }

 

C. Vladik and fractions
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vladik and Chloe decided to determine who of them is better at math. Vladik claimed that for any positive integer n he can represent fraction as a sum of three distinct positive fractions in form .

Help Vladik with that, i.e for a given n find three distinct positive integers x, y and z such that . Because Chloe can't check Vladik's answer if the numbers are large, he asks you to print numbers not exceeding 109.

If there is no such answer, print -1.

Input

The single line contains single integer n (1 ≤ n ≤ 104).

Output

If the answer exists, print 3 distinct numbers x, y and z (1 ≤ x, y, z ≤ 109, x ≠ y, x ≠ z, y ≠ z). Otherwise print -1.

If there are multiple answers, print any of them.

Examples
Input
3
Output
2 7 42
Input
7
Output
7 8 56
题意:公式题目
题解:我是莫名其妙暴力出来的
看了别人的题解 是直接分解式子的 2/n=1/n+1/(n+1)+1/n*(n+1)
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 const int N=100005;
14 using namespace  std;
15 int n;
16 ll x1,x2,x3;
17 int main()
18 {
19     scanf("%d",&n);
20     int i=n;
21     while(i)
22     {
23         if(2*i>n)
24         {
25             x1=i;
26             break;
27         }
28         i++;
29     }
30     ll zi1,mu1;
31     zi1=2*x1-n;
32     mu1=n*x1;
33     i=x1+1;
34     while(i)
35     {
36         if(zi1*i>mu1)
37         {
38             x2=i;
39             break;
40         }
41         i++;
42     }
43     ll zi2,mu2;
44     zi2=zi1*x2-mu1;
45     mu2=mu1*x2;
46     if(mu2%zi2==0)
47     {
48         x3=mu2/zi2;
49         if(x1!=x3&&x2!=x3)
50         {
51             cout<<x1<<" "<<x2<<" "<<x3<<endl;
52             return 0;
53         }
54     }
55     cout<<"-1"<<endl;
56     return 0;
57 }

 

D. Chloe and pleasant prizes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Christmas is coming, so sponsors decided to decorate the Christmas tree with their prizes.

They took n prizes for the contestants and wrote on each of them a unique id (integer from 1 to n). A gift i is characterized by integer ai — pleasantness of the gift. The pleasantness of the gift can be positive, negative or zero. Sponsors placed the gift 1 on the top of the tree. All the other gifts hung on a rope tied to some other gift so that each gift hung on the first gift, possibly with a sequence of ropes and another gifts. Formally, the gifts formed a rooted tree with n vertices.

The prize-giving procedure goes in the following way: the participants come to the tree one after another, choose any of the remaining gifts and cut the rope this prize hang on. Note that all the ropes which were used to hang other prizes on the chosen one are not cut. So the contestant gets the chosen gift as well as the all the gifts that hang on it, possibly with a sequence of ropes and another gifts.

Our friends, Chloe and Vladik, shared the first place on the olympiad and they will choose prizes at the same time! To keep themselves from fighting, they decided to choose two different gifts so that the sets of the gifts that hang on them with a sequence of ropes and another gifts don't intersect. In other words, there shouldn't be any gift that hang both on the gift chosen by Chloe and on the gift chosen by Vladik. From all of the possible variants they will choose such pair of prizes that the sum of pleasantness of all the gifts that they will take after cutting the ropes is as large as possible.

Print the maximum sum of pleasantness that Vladik and Chloe can get. If it is impossible for them to choose the gifts without fighting, print Impossible.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of gifts.

The next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the pleasantness of the gifts.

The next (n - 1) lines contain two numbers each. The i-th of these lines contains integers ui and vi (1 ≤ ui, vi ≤ n, ui ≠ vi) — the description of the tree's edges. It means that gifts with numbers ui and vi are connected to each other with a rope. The gifts' ids in the description of the ropes can be given in arbirtary order: vi hangs on ui or ui hangs on vi.

It is guaranteed that all the gifts hang on the first gift, possibly with a sequence of ropes and another gifts.

Output

If it is possible for Chloe and Vladik to choose prizes without fighting, print single integer — the maximum possible sum of pleasantness they can get together.

Otherwise print Impossible.

Examples
Input
8
0 5 -1 4 3 2 6 5
1 2
2 4
2 5
1 3
3 6
6 7
6 8
Output
25
Input
4
1 -5 1 1
1 2
1 4
2 3
Output
2
Input
1
-1
Output
Impossible
题意:给你一颗树 树有点权 现在找到两个不关联的子树 输出最大的子树点权和
题解:dfs序将子树转化为区间 在n个区间中选取两个不相交的区间 输出最大的权值和。(具体看代码)
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <cstring>
  5 #include <algorithm>
  6 #include <stack>
  7 #include <queue>
  8 #include <cmath>
  9 #include <map>
 10 #define ll  __int64
 11 #define mod 1000000007
 12 #define dazhi 2147483647
 13 using namespace  std;
 14 int n;
 15 ll a[200005];
 16 int u,v;
 17 int pre[200005];
 18 ll d[1000006];
 19 int in[200005];
 20 int out[200005];
 21 ll dp[2000005];
 22 int nedge=0;
 23 struct node
 24 {
 25     int from;
 26     int to;
 27 }N[1000006];
 28 struct xx
 29 {
 30      int l;
 31      int r;
 32      ll w;
 33 }M[200005];
 34 void add(int from ,int to)
 35 {
 36     nedge++;
 37     N[nedge].to=to;
 38     N[nedge].from=pre[from];
 39     pre[from]=nedge;
 40 }
 41 int dfn=0;
 42 ll sum=0;
 43 map<int,int> mp;
 44 map<int,int>mpp;
 45 void getdfs(int root)
 46 {
 47     sum+=a[root];
 48     in[root]=++dfn;
 49     d[dfn]=sum;
 50     mp[dfn]=root;
 51     mpp[root]=1;
 52     for(int i=pre[root];i;i=N[i].from)
 53     {
 54         if(mpp[N[i].to])
 55             continue;
 56         getdfs(N[i].to);
 57     }
 58     out[root]=dfn;
 59 }
 60 bool cmp(struct xx aa,struct xx bb)
 61 {
 62     return aa.r<bb.r;
 63 }
 64 int main()
 65 {
 66     memset(pre,0,sizeof(pre));
 67     mp.clear();
 68     scanf("%d",&n);
 69     for(int i=1;i<=n;i++)
 70         scanf("%I64d",&a[i]);
 71     for(int i=1;i<=n-1;i++)
 72     {
 73         scanf("%d %d",&u,&v);
 74         add(u,v);
 75         add(v,u);
 76     }
 77     getdfs(1);
 78     for(int i=1;i<=n;i++)
 79     {
 80         M[i].l=in[i];
 81         M[i].r=out[i];
 82         M[i].w=d[out[i]]-d[in[i]]+a[mp[in[i]]];
 83     }
 84     sort(M+1,M+1+n,cmp);
 85     ll ans=-1e18-1;
 86     ll maxn=-1e18-1;
 87     int flag=0;
 88     for(int i=0;i<=n;i++)
 89         dp[i]=-1e18-1;
 90     for(int i=1;i<=n;i++)
 91     {
 92         maxn=max(M[i].w,maxn);
 93         dp[M[i].r]=maxn;
 94     }
 95     for(int i=2;i<=n;i++)
 96     {
 97         if(dp[i]<=dp[i-1])
 98             dp[i]=dp[i-1];
 99     }
100     for(int i=1;i<=n;i++){
101         ans=max(ans,M[i].w+dp[M[i].l-1]);
102         if(dp[M[i].l-1]!=(-1e18-1))
103             flag=1;
104     }
105     if(flag==0)
106     printf("Impossible\n");
107     else
108     printf("%I64d\n",ans);
109     return 0;
110 }
111 /*
112 10
113 -1 2 -2 -3 -1 -1 0 -4 -5 -4
114 4 6
115 6 9
116 1 2
117 6 2
118 7 8
119 7 9
120 5 10
121 6 3
122 10 1
123 */