组内限时训练1 暴力篇

A - Vanya and Scales

 

Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2 (exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.

Input

The first line contains two integers w, m (2 ≤ w ≤ 1091 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.

Output

Print word 'YES' if the item can be weighted and 'NO' if it cannot.

Examples

Input
3 7
Output
YES
Input
100 99
Output
YES
Input
100 50
Output
NO

Note

Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.

 

Note to the second sample test. One pan of the scales can have an item of mass 99and the weight of mass 1, and the second pan can have the weight of mass 100.

 

Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.

这个是一个进制转化问题,我也不是很会做,具体就看博客

这个是一个偏思维的题目。

#include <iostream>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn = 1e3+10;

int main()
{
	int w, m;
	cin >> w >> m;
	while(m)
	{
		if ((m - 1) % w == 0) m--;
		else if ((m + 1) % w == 0) m++;
		else if(m%w)
		{
			printf("NO\n");
			return 0;
		}
		m /= w;
	}
	printf("YES\n");
	return 0;
}

  

B - "Or" Game

You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make  as large as possible, where  denotes the bitwise OR.

Find the maximum possible value of  after performing at most koperations optimally.

Input

The first line contains three integers nk and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8).

The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

Output

Output the maximum value of a bitwise OR of sequence elements after performing operations.

Examples

Input
3 1 2
1 1 1
Output
3
Input
4 2 3
1 2 4 8
Output
79

Note

For the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is .

For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result.

这个是一个不是很难的暴力,但是容易超时,比较难发现可以用pre和aft去存前缀或和后缀或。

然后就是看代码:

#include <iostream>
#include <algorithm>
#include <queue>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
ll a[maxn];
ll pre[maxn], aft[maxn];

int main()
{
	int n, k, x;
	cin >> n >> k >> x;
	ll s = x;
	for (int i = 2; i <= k; i++) s *= x;
	for(int i=1;i<=n;i++)
	{
		cin >> a[i];
		pre[i] = pre[i - 1] | a[i];
	}
	for(int i=n;i>=1;i--)
	{
		aft[i] = aft[i + 1] | a[i];
	}
	ll ans = 0;
	for(int i=1;i<=n;i++)
	{
		ll exa = pre[i - 1] | (a[i] * s) | aft[i + 1];
		ans = max(ans, exa);
	}
	printf("%lld\n", ans);
	return 0;
}

  

 

猜你喜欢

转载自www.cnblogs.com/EchoZQN/p/10663065.html