codeforces-743【B找规律】C思维】

52 篇文章 0 订阅
12 篇文章 0 订阅

题目链接:点击打开链接

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 na, and b (1 ≤ n ≤ 1051 ≤ 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 ,问花费的最小代价。

思路:水题啊,但是感觉太简单了,不敢写

#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;
}

题目链接: 点击打开链接

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 ≤ 501 ≤ 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 ,处理 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;
} 

题目链接: 点击打开链接

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 xy 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 xy and z (1 ≤ x, y, z ≤ 109x ≠ yx ≠ zy ≠ 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

大意:找出使等式合法的解。

思路:题中说了找出任意一组解。可以缩小枚举范围,暴力枚举 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值