CodeChef 2016年6月问题

DEVARRAY

(1)问题描述:

Devu has an array A consisting of N positive integers. He would like to perform following operation on array.
Pick some two elements a, b in the array (a could be same as b, but their corresponding indices in the array should not be same).
Remove both the elements a and b and instead add a number x such that x lies between min(a, b)and max(a, b), both inclusive, (i.e. min(a, b) ≤ x ≤ max(a, b)).
Now, as you know after applying the above operation N - 1 times, Devu will end up with a single number in the array. He is wondering whether it is possible to do the operations in such a way that he ends up a number t.
He asks your help in answering Q such queries, each of them will contain an integer t and you have to tell whether it is possible to end up t.
Input

There is only one test case per test file.
First line of the input contains two space separated integers N, Q denoting number of elements in A and number of queries for which Devu asks your help, respectively
Second line contains N space separated integers denoting the content of array A.
Each of the next Q lines, will contain a single integer t corresponding to the query.
Output

Output Q lines, each containing "Yes" or "No" (both without quotes) corresponding to the answer of corresponding query.
Constraints

1 ≤ N, Q ≤ 105
0 ≤ t ≤ 109
Subtasks

Subtask #1 : 30 points
1 ≤ Ai ≤ 2
Subtask #2 : 70 points
1 ≤ Ai ≤ 109
Example

Input 1:
1 2
1
1
2

Output:
Yes
No

Input 2:
2 4
1 3
1
2
3
4

Output:
Yes
Yes
Yes
No
Explanation

In the first example, Devu can't apply any operation. So the final element in the array will be 1 itself.
In the second example,
Devu can replace 1 and 3 with any of the numbers among 1, 2, 3. Hence final element of the array could be 1, 2 or 3.

(2)要点:

(3)代码:

#include <stdio.h>
#include <vector>
using std::vector;

int main()
{
	unsigned int n = 0,q = 0,v = 0;scanf("%d%d",&n,&q);
	unsigned int minv = 1000000001,maxv = 0;
	for(unsigned int i = 0;i < n;++i)
	{
		scanf("%d",&v);
		if(v < minv) minv = v;
		if(v > maxv) maxv = v;
	}
	for(unsigned int i = 0;i < q;++i)
	{
		scanf("%d",&v);
		printf("%s\n",(v >= minv && v <= maxv)?"Yes":"No");
	}
	return 0;
}


CHCOINSG

(1)问题描述:

Chef is playing a game with his friend Misha. They have a pile containg N coins. Players take alternate turns, removing some coins from the pile. On each turn, a player can remove either one coin or coins equal to some prime power (i.e. px coins, where p - prime number and x - positive integer). Game ends when the pile becomes empty. The player who can not make a move in his turn loses.
Chef plays first. Your task is to find out who will win the game, provided that both of the player play optimally.
Input

The first line of the input contains an integer T denoting the number of test cases. The description of Ttest cases follows.
The only line of each test case contains one integer N.
Output

For each test case, output a single line containing one word - the name of the winner of the game. Print "Chef" (without quotes) if Chef wins the game, print "Misha" (without quotes) otherwise.
Constraints

1 ≤ T ≤ 1000
1 ≤ N ≤ 109
Subtasks

Subtask #1 (20 points):
1 ≤ N ≤ 10
Subtask #2 (30 points):
1 ≤ N ≤ 104
Subtask #3 (50 points): No additional constraints.
Example

Input:
2
1
8

Output:
Chef
Chef
Explanation

Example case 1. Chef will remove the only coin from the pile and will win the game.
Example case 2. Chef will remove all 8 coins from the pile and win the game. Chef can remove 8 coins because 8 is a prime number, as 8 = 23.

(2)要点:归纳法容易证明只有6n是必败局面

(3)代码:

#include <stdio.h>
#include <vector>
using std::vector;

int main()
{
	static const unsigned int maxn = 10000;
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned int n = 0;scanf("%d",&n);
		printf("%s\n",(0 == (n%6))?"Misha":"Chef");
	}
	return 0;
}



BINOP

(1)问题描述:

Today is Chef's birthday. His mom decided to surprise him with a truly fantastic gift: his favourite binary stringB. But, unfortunately, all the stocks of binary string B have been sold out, and only a binary string A (A ≠ B)is available in the market.
She purchases the string A and tries to convert it to string B by applying any of following three operations zero or more times.
AND Operation:
She will choose a pair of indices i and j such that i != j and perform following sequence of operations.
result = Ai & Aj
Ai = result & Ai
Aj = result & Aj
 
OR Operation:
She will choose a pair of indices i and j such that i != j and perform following sequence of operations.
result = Ai | Aj
Ai = result | Ai
Aj = result | Aj
 
XOR Operation:
She will choose a pair of indices i and j such that i != j and perform following sequence of operations.
result = Ai ^ Aj
Ai = result ^ Ai
Aj = result ^ Aj
 
Chef's mom is eagerly waiting to surprise him with his favourite gift and therefore, she wants to convert string A to string B as fast as possible. Can you please help her by telling her the minimum number of operations she will require? If it is impossible to do so, then let Chef's mom know about it.
Input

First line of input contains a single integer T denoting the number of test cases. T test cases follow.
First line of each test case, will contain binary string A.
Second line of each test case, will contain binary string B.
Output

For each test case, Print "Lucky Chef" (without quotes) in first line and minimum number of operations required to convert string A to sting B in second line if conversion is possible. Print "Unlucky Chef" (without quotes) in a new line otherwise.
Constraints

1 ≤ T ≤ 105
1 ≤ |A| ≤ 106
1 ≤ |B| ≤ 106
A != B
|A| = |B|
sum of |A| over all test cases does not exceed 106
sum of |B| over all test cases does not exceed 106
Subtasks

Subtask #1 (40 points) : Sum of |A| & |B| over all test cases does not exceed 103
Subtask #2 (60 points) : Sum of |A| & |B| over all test cases does not exceed 106 
Example

Input
2
101
010
1111
1010
Output
Lucky Chef
2
Unlucky Chef

Explanation

Example case 1.
Applying XOR operation with indices i = 1 and j = 2. Resulting string will be 011.
Then, Applying AND operation with indices i = 1 and j = 3. Resulting string will be 010.
 
Example case 2.
It is impossible to convert string A to string B.

(2)要点:And操作实际就是将一个1变成0;Or操作实际就是将一个0变成1;XOR操作就是就是将一个0和1对换

(3)代码:

#include <stdio.h>
#include <vector>
#include <string.h>
using std::vector;

// 
int main()
{
	static const unsigned int buff_size = 1000010;
	char* abuff = new char[buff_size];
	char* bbuff = new char[buff_size];
	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		scanf("%s%s",abuff,bbuff);
		unsigned int count01 = 0,count10 = 0,count0 = 0,count1 = 0;
		for(size_t i = 0;i < strlen(abuff);++i)
		{
			count0 += ('0' == abuff[i]);
			count1 += ('1' == abuff[i]);
			if('0' == abuff[i] && '1' == bbuff[i]) ++ count01;
			else if('1' == abuff[i] && '0' == bbuff[i]) ++ count10;
		}
		if(count01 == count10) printf("Lucky Chef\n%u\n",count01);
		else if(count01 > count10)
		{
			if(0 == count1) printf("Unlucky Chef\n");
			else printf("Lucky Chef\n%u\n",count01);
		}
		else
		{
			if(0 == count0) printf("Unlucky Chef\n");
			else printf("Lucky Chef\n%u\n",count10);
		}
	}
	delete[] abuff;delete[] bbuff;
	return 0;
}



CHEFARK

(1)问题描述:

Chef has an array A consisting of N integers. He also has an intger K.
Chef wants you to find out number of different arrays he can obtain from array A by applying the following operation exactly K times.
Pick some element in the array and multiply it by -1
As answer could be quite large, print it modulo 109 + 7.
Input

The first line of the input contains an integer T denoting the number of test cases. The description of Ttest cases follows.
The first line of each test case contains two space separated integers N, K as defined above.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output

For each test case, output a single line containing an integer corresponding to the number of different arrays Chef can get modulo 109 + 7.
Constraints

1 ≤ T ≤ 10
1 ≤ N, K ≤ 105
-106 ≤ Ai ≤ 106
Subtasks

Subtask #1 (10 points) : N, K ≤ 10
Subtask #2 (30 points) : N, K ≤ 100
Subtask #3 (60 points) : N, K ≤ 105
Example

Input:
3
1 3
100
3 1
1 2 1
3 2
1 2 1

Output:
1
3
4
Explanation

Example case 1.
Chef has only one element and must apply the operation 3 times to it. After applying the operations, he will end up with -100. That is the only array he will get.
Example case 2.
Chef can apply operation to one of three elements. So, he can obtain three different arrays.
Example case 3.
Note that other than applying operation to positions (1, 2), (1, 3), (2, 3), Chef can also apply the operation twice on some element and get the original.
In summary, Chef can get following four arrays.
[1, 2, 1]
[-1, -2, 1]
[-1, 2, -1]
[1, -2, -1]

(2)要点:如果0的个数是c0 (c0 > 0),则结果是C(n-c0,0) + C(n-c0,1) + C(n-c0,2) + ... + C(n-c0,n-c0) = 2^(n-c0);如果c0 = 0,则是C(n,k) + C(n,k-2) + C(n,k-4) + ... 

(3)代码:

#include <stdio.h>
#include <vector>
#include <string.h>
#include <assert.h>
using std::vector;

// 计算a^x (mod module)
static unsigned int quick_modexp(unsigned int a,unsigned int module,unsigned long long x)
{
}

// 计算C(n,m)%module,其中module为素数,且n < module,m < module
// factorial[x] = x! mod (module),其中0 <= x < module
static unsigned int fermat_mod(unsigned int n,unsigned int m,unsigned int module,const unsigned int* factorial)
{
}

int main()
{
	static const unsigned int module = 1000000007;
	static const unsigned int maxn = 100000;
	vector<unsigned long long> exp2(maxn+1,1);
	for(unsigned int i = 1;i <= maxn;++i) exp2[i] = (exp2[i-1]*2)%module;

	unsigned int factorial[maxn+1] = { 1 };
	for(unsigned int i = 1;i <= maxn;++i) factorial[i] = ((unsigned long long)(factorial[i-1])*i)%module;

	unsigned int nCases = 0;scanf("%d",&nCases);
	for(unsigned int iCases = 1;iCases <= nCases;++iCases)
	{
		unsigned int n = 0,k = 0;scanf("%d%d",&n,&k);
		unsigned int count0 = 0;
		for(unsigned int i = 0;i < n;++i)
		{
			int v = 0;scanf("%d",&v);
			count0 += (0 == v);
		}

		unsigned long long ans = 0;
		if(0 == count0)
		{
			for(unsigned int i = k%2;i <= k;i += 2)
			{
				ans += fermat_mod(n,i,module,factorial);
			}
		}
		else ans = exp2[n-count0];
		printf("%u\n",(unsigned int)(ans%module));

	}
	return 0;
}



CHEARMY

(1)问题描述:

(2)要点:

(3)代码:



(1)问题描述:

(2)要点:

(3)代码:



(1)问题描述:

(2)要点:

(3)代码:



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值