组内限时训练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
");
			return 0;
		}
		m /= w;
	}
	printf("YES
");
	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
", ans);
	return 0;
}

  

 

 
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Molly Hooper has n different kinds of chemicals arranged in a line. Each of the chemicals has an affection value, The i-th of them has affection value ai.

Molly wants Sherlock to fall in love with her. She intends to do this by mixing a contiguous segment of chemicals together to make a love potion with total affection value as a non-negative integer power of k. Total affection value of a continuous segment of chemicals is the sum of affection values of each chemical in that segment.

Help her to do so in finding the total number of such segments.

Input

The first line of input contains two integers, n and k, the number of chemicals and the number, such that the total affection value is a non-negative power of this number k. (1 ≤ n ≤ 105, 1 ≤ |k| ≤ 10).

Next line contains n integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — affection values of chemicals.

Output

Output a single integer — the number of valid segments.

Examples
input
Copy
4 2
2 2 2 2
output
Copy
8
input
Copy
4 -3
3 -6 -3 12
output
Copy
3
Note

Do keep in mind that k0 = 1.

In the first sample, Molly can get following different affection values:

  • 2: segments [1, 1], [2, 2], [3, 3], [4, 4];
  • 4: segments [1, 2], [2, 3], [3, 4];
  • 6: segments [1, 3], [2, 4];
  • 8: segments [1, 4].

Out of these, 2, 4 and 8 are powers of k = 2. Therefore, the answer is 8.

In the second sample, Molly can choose segments [1, 2], [3, 3], [3, 4].

这个题目是思维题,我开始也想直接暴力,就是直接枚举k的次方,直到它到达10的14次方,
这个不是很大,如果是2都只要几十次,所以肯定不会超时,不过不知道怎么操作。
看了题解,觉得map真是一个好东西,它可以存很大的数,耗费的空间也不是很大。
我们就可以把数值存到map里面。
具体就是,先处理这些数,求出前缀和,然后就是求出k的幂。
最后枚举。

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

int main()
{
	int n, k;
	cin >> n >> k;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld", &a[i]);
		a[i] += a[i - 1];//求前缀数组
	}
	int cnt = 2;
	kc[1] = 1;//这个是幂为0的时候
	if (k == -1)//当k=-1的时候就两种可能,这个要特殊枚举
	{
		kc[2] = -1;
		cnt++;
	}
	else if(k!=1)
	{
		ll temp = k;
		while(temp<1e15&&temp>-1e15)
		{
			kc[cnt++] = temp;
			temp *= k;
		}
	}
	ll ans = 0;
	for (int i = 1; i < cnt; i++) mp[kc[i]] = 1;
	for(int i=1;i<=n;i++)
	{
		if (mp[a[i]]) ans+=mp[a[i]];
		for(int j=1;j<cnt;j++)
		{
			mp[a[i] + kc[j]]++;
		}
	}
	printf("%lld
", ans);
	return 0;
}

  

原文地址:https://www.cnblogs.com/EchoZQN/p/10663065.html