题目链接:点击打开链接
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.
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.
Print single integer — the minimum cost Vladik has to pay to get to the olympiad.
4 1 4 1010
1
5 5 2 10110
0
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 ,问花费的最小代价。
思路:水题啊,但是感觉太简单了,不敢写
#include<cstdio>
#include<cstring>
using namespace std;
int n,a,b;
char str[100010];
int main()
{
while(~scanf("%d%d%d",&n,&a,&b))
{
scanf("%s",str+1);
if(str[a]==str[b])
puts("0");
else
puts("1");
}
return 0;
}
题目链接: 点击打开链接
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!
The only line contains two integers n and k (1 ≤ n ≤ 50, 1 ≤ k ≤ 2n - 1).
Print single integer — the integer at the k-th position in the obtained sequence.
3 2
2
4 8
4
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 ,处理 n-1 次。每一次处理要把上次的处理的结果添加到这一次的末尾,再在整个序列的中间插入一个之前没有出现过的正整数,问处理 n-1 次后,第 k 个位置的数字是什么。
思路:
先看处理过程:
初态: 1 此时 n=1
第 1 次: 1 2 1 此时 n=2
第 2 次: 1 2 1 3 1 2 1 此时 n=3
第 3 次: 1 2 1 3 1 2 1 4 1 2 1 3 1 2 此时 n=4
…………
以上可以看出:
序列的奇数位置均为 1;
序列的偶数位置,可以观察:出现同一个数字的位置成等差数列
第一次出现 2 的位置是 2 ^ ( 2-1 ) ,公差为 2 ^ 2 ;
第一次出现 3 的位置是 2 ^ ( 3-1 ) ,公差为 2 ^ 3 ;
第一次出现 4 的位置是 2 ^ ( 4-1 ) ,公差为 2 ^ 4 ;
…………
第一次出现 m 的位置是 2 ^ ( m-1 ) ,公差为 2 ^ m ;通项公式为 ( 2t - 1 ) * 2 ^ ( m -1 ) ,表示第 t 次出现 m 的位置是 ( 2t - 1 ) * 2 ^ ( m -1 ) ;这里的 t = ( 1,2,3 …… ) ,显然 t 为奇数。
处理 n-1 次出现的整数有 n 个,然后枚举这 n 个整数,判断是否合法
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#define LL long long
using namespace std;
int n;
LL k;
int main()
{
/* 程序跑的太太太慢
map<LL,int> M;
LL len=(1LL<<50)-1;
for(int i=1;i<=50;i++)
{
LL a1=1LL<<i-1;
LL d=1LL<<i;
while(a1<=len)
{puts("&&&");
M[a1]=i;
a1+=d;
}
}
printf("%d\n",M[k]);
*/
while(~scanf("%d%I64d",&n,&k))
{
if(k&1)
puts("1");
else
{
for(int i=1;i<=n;i++)
{
LL p=1LL<<i-1;
if(k%p==0&&((k/p)&1))
{
printf("%d\n",i);
break;
}
}
}
}
return 0;
}
题目链接: 点击打开链接
思路:题中说了找出任意一组解。可以缩小枚举范围,暴力枚举 x 、y,然后代入等式判断 z 是否是整数。但要注意姿势
还有就是巧妙地找出一组特解,代码给出:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
int main()
{
while(~scanf("%d",&n))
{
if(n==1)
{
puts("-1");
continue;
}
printf("%d %d %d\n",n,n+1,n*(n+1));
}
return 0;
}