2018 Ninth Blue Bridge Cup Group B Finals Solutions

One, change coins

  1. Content: The denominations of Planet X's banknotes are only: 100 yuan, 5 yuan, 2 yuan, 1 yuan, a total of 4 kinds. When Xiao Ming traveled to X-star, he only had 2 X-star coins of 100 yuan in his hand, which was too inconvenient. He happened to pass by X-star bank to change change. Xiao Ming has a bit of obsessive-compulsive disorder. He insists that the number of 2-yuan coins in the 200-yuan change is exactly 10 times the number of 1-yuan coins, and the rest are of course 5 yuan denominations. The staff of the bank is a little embarrassed. Can you help to calculate: on the premise of meeting Xiao Ming's requirements, how many banknotes should be exchanged for him at least? (5 yuan, 2 yuan, and 1 yuan denominations must be available, not 0)
  2. Idea: Violent solution
  3. Answer: 74
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int i,j,k;
	for(i=9;i>=1;i--)
	{
		k=200-21*i;
		if(k%5==0)
		{
			cout<<11*i+k/5;
		}  
	}
	return 0;
 } 

Second, the laser pattern

  1. Content: In order to increase the atmosphere of the grand festival of Planet X, 30 mechatronics were lined up to shoot beams of light into space.
    During the installation and debugging, I found out that for some reason, the two adjacent lasers cannot be turned on at the same time! The king would like to know how many laser effects can be produced in the current situation with this bug? Obviously, if there are only 3 machines, there can be a total of 5 styles, that is: all closed (sorry, at this time silence is better than sound, this is also considered one), a total of 3 kinds of two machines, only 30 machines of one kind It's not easy, the king has to ask you for help.
    It is required to submit an integer representing the number of patterns that can be formed by 30 lasers.
  2. Ideas: deep search
  3. Answer: 2178309
#include<bits/stdc++.h>
using namespace std;
long long t=0;
int s[33]; //可以另用一个数组保存内容,0,1,记得每次用完由1归0
int ac(int n,int m)
{
	if(n==31)
	{
		t++;
		return 0;
	}
	if(m==1)  ac(n+1,0);//不能用n++
	if(m==0)
	{
		ac(n+1,1);
		ac(n+1,0);  //不是return ac(); 
	}
}
int main()
{
	ac(1,0);
	cout<<t;
	return 0;
 } 

Three, Gray code

  1. Content: Gray code is an n-bit binary representation of numbers. Unlike ordinary binary representation, it requires that two adjacent numbers differ by only 1 digit. The first and last two numbers are also required to be only 1 digit apart. There are many algorithms to generate Gray codes. The following is the more common one: start from encoding all 0s. When the odd-numbered number is generated, only the last digit of the current number is changed (0 becomes 1, 1 becomes 0). When generating an even number, first find the rightmost 1 and change the number to the left. The 4-bit Gray code sequence generated using this rule is as follows:
  2. 0000
    0001
    0011
    0010
    0110
    0111
    0101
    0100
    1100
    1101
    1111
    1110
    1010
    1011
    1001
    1000
  3. Code to fill in the blanks:
#include <stdio.h>
void show(int a,int n)
{
	int i;
	int msk = 1;
	for(i=0; i<n-1; i++) msk = msk << 1;
	for(i=0; i<n; i++){
		printf((a & msk)? "1" : "0");
		msk = msk >> 1;
	}
	printf("\n");
} 


void f(int n)
{
	int i;
	int num = 1;
	for(i=0; i<n; i++) num = num<<1;
	
	int a = 0;
	for(i=0; i<num; i++){
		show(a,n);
		
		if(i%2==0){
			a = a ^ 1;
		}
		else{
			a = _________________________ ; //填空
		}
	}
}


int main()
{
	f(4);
	return 0;
}

4. Supplementary knowledge points

	1. &   1,1->1    1,0->0   0,0->0  ----------》全真即真,有假即假;&1则不变,&0则1变0
	2. |    1,1->1    1,0->1   0,0->0
	3. ^    1,1->0    1,0->1   0,0->0----------》相同为1,不同为0;^0不变^1互换01
	4. ~    1变成0,0变成1
	5.<< a 左移a位,缺位补0,高位溢出移除
	6. >>  右移指定位数,缺位补0
	7. 1真,负0正假
void show(int a,int n)
{
	int i;
	int msk = 1;
	for(i=0; i<n-1; i++) msk = msk << 1;  //msk为1000
	for(i=0; i<n; i++){
		printf((a & msk)? "1" : "0");  //根据&的性质,为从高位逐步输出,值不变
		msk = msk >> 1;  //msk分别为 1000,0100,0010,0001
	}
	printf("\n");
} 
void f(int n)
{
	int i;
	int num = 1;
	for(i=0; i<n; i++) num = num<<1;
	
	int a = 0;
	for(i=0; i<num; i++){
		show(a,n);  //由调用关系得出每次循环,每次输出
		
		if(i%2==0){
			a = a ^ 1; //为0,下一次奇,决定下一次,1即0001,0不变,1互换01
			//即是要求里面的为奇最后一位变换
		}
		else{
			a = _________________________ ; //故此,为偶数,找最近1,前一位变号
			//(a&(-a))功能输出最左边连续的0加一个1,例12:1010  12&(-12):10
			//在左移,根据^性质,1对应到要变号的位处,即可改变了。
		}
	}
}

5. Answer (not unique): a (i (i+1)) ((i (i+1))/2)
------------- (the above analysis corresponds to the answer) a^(( a&(-a))<<1)
------------------------- a ((a ((a&(a-1)))) <<1)

Fourth, adjust the watch

  1. Content: Xiao Ming bought a high-end and elegant electronic watch, he is about to adjust the time.
    --------In the M78 nebula, the unit of measurement of time is different from that on the earth. An hour in the M78 nebula has n minutes.
    -------- As we all know, the watch has only one button to add one to the current number. When adjusting the minutes, if the current displayed number is 0, then pressing the button will change to 1, and pressing it again will change to 2. If the current number is n - 1, it will become 0 after pressing it once.
    ---------As an obsessive-compulsive disorder patient, Xiao Ming must adjust the time of his watch. If the time on the watch is 1 more than the current time, it takes n - 1 presses plus one button to set the correct time back.
    --------Xiao Ming thought, if the watch could add another button, it would be great to add k to the current number. He wants to know, given the +k button, the maximum number of times it would have to be pressed to go from any minute to any other minute according to the optimal strategy.
    --------Note that when pressing the +k button, if the number after adding k exceeds n-1, the modulo n will be taken. For example, when n=10, k=6, assuming the current time is 0, press the +k button twice, it will be adjusted to 2.
  2. Input: two integers n, k in one line, the meaning is the same as the title.
  3. Output: One integer per line. Indicates: according to the optimal strategy, how many times to press the button from one time to another.
  4. Input: 5 3 Output: 2
  5. "Example Explanation" Press 0 times if the time is correct. Otherwise the relationship between the number of times to press and the series of operations is as follows:
    1: +1
    2: +1, +1
    3: +3
    4: +3, +1
    "Data Range" for 30% of the data 0 < k < n <= 5
    ------------------ for 60% of the data 0 < k < n <= 100
    ---------------- --For 100% of the data 0 < k < n <= 100000
    "resource contract" peak memory consumption (including virtual machines) < 256M CPU consumption < 1000ms

Idea: Wide search BFS - use queue
-------- Deep search DFS - recursion, only press 1 or k times each time, it may take up too much space;

Reverse thinking: simplification starts at 0; +1 times are 1 and k-1, and then fill in gradually, so as to ensure that the fill is the smallest each time, plus judgment, when the place filled by +1/+k is filled If you fill it in, you will not fill it in. It is equivalent to a wide search in nature. I don’t know if it will time out.

#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main()
{
	int n,k;
	cin>>n>>k;
	memset(a,-1,sizeof(a));  //填-1可以哟,还有0,1不可以,不知道为什么
	a[0]=0;//初始化的条件
	int t=0;//限定条件
	int f=1;//限定条件
	while(1) //循环
	{
		f=1;
		for(int i=0;i<n;i++)
		{
			if(a[i]==t)
			{
					if(a[(i+1)%n]!=-1)  a[(i+1)%n]=t+1;//判断哦
					if(a[(i+k)%n]!=-1)   a[(i+k)%n]=t+1;
			}
		}
		t++;
		for(int i=0;i<n;i++) //当全部都有数就不用在解释了
		{
			if(a[i]==-1){
				f=0;
				break;
			}
		}
		if(f) break;
	}
	cout<<t; //这时的t已经为最大
	return 0;
 } 

Wide search: https://blog.csdn.net/nka_kun/article/details/80487535

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000007
using namespace std;
typedef long long ll;
const int maxn = 1e5+5;
const double esp = 1e-12;
const int inf = 0x3f3f3f3f;
map<int,int>::iterator it;
 
struct node
{
	int cost,time;
	node(int cost,int time):cost(cost),time(time){}
};
 
int n,k;
int vis[maxn],num[maxn];
 
void bfs()
{
	queue<node> q;
	q.push(node(0,0));//假设从0开始走表,从哪都一样 
	num[0] = 0;
	vis[0] = 1;
	
	while(!q.empty())
	{
		node now = q.front();
		q.pop();
		
		int to = (now.time+1)%n;
		if(!vis[to])
		{
			vis[to] = 1;
			num[to] = now.cost+1;
			q.push(node(now.cost+1,to));
		}
		to = (now.time+k)%n;
		if(!vis[to])
		{
			vis[to] = 1;
			num[to] = now.cost+1;
			q.push(node(now.cost+1,to));
		}
	}
}
 
int main()
{
	mem(num,inf);
	cin>>n>>k;
	bfs();
	
	int ans = 0;
	for(int i = 0;i< n;i++)
		ans = max(ans,num[i]);
	
	printf("%d\n",ans);
		
	return 0;
}

Fifth, build blocks

Xiao Ming is very interested in building blocks. His blocks are all cubes of the same size.
When building blocks, Xiaoming chose m blocks as the foundation, lined them up on the table, leaving no gaps in between, and called it the 0th floor.
Then, Xiao Ming can place the 1st floor, the 2nd floor, ..., up to the nth floor. There are three rules to follow when placing blocks:

Rule 1: Each building block must be placed directly above a certain building block and aligned with the building block on the next layer;
Rule 2: The building blocks on the same layer must be placed in a row, with no gaps in between;
Rule 3: Xiao Ming Blocks cannot be placed in places you don't like.

Among them, the positions that Xiao Ming did not like were marked on the drawings. There are n lines in the drawing, and each line from bottom to top corresponds to the first to nth layers of the building blocks. Each line has m characters, the characters may be '.' or 'X', where 'X' indicates that this position is not liked by Xiao Ming.
Now, Xiao Ming wants to know how many ways there are to place building blocks. He found you who participated in the Blue Bridge Cup to help him calculate the answer.
Since this answer can be large, you only need to answer this answer modulo 1000000007 (one billion zero seven).
Note: Putting nothing on the foundation is also considered one of the solutions.

【Input format】
The first line of the input data has two positive integers n and m, which represent the size of the drawing.
Then n lines, each with m characters, are used to describe the drawing. Each character can only be '.' or 'X'.

[Output format]
Output an integer, which represents the result of the answer modulo 1000000007.

【Sample input 1】
2 3
…X
.X.

[Sample output 1]
4

【Example 1】
Successful placement (where O means placing blocks):
(1)
…X
.X.
(2)
…X
OX.
(3)
OX
OX.
(4)
…X
.XO

【Sample input 2】
3 3
…X
.X.

[Sample output 2]
16

[Data scale convention]
For 10% of the data, n=1, m<=30;
for 40% of the data, n<=10, m<=30;
for 100% of the data, n<=100, m<= 100.

Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 1000ms

slightly

Six, matrix summation

After many written tests and interviews, Xiao Ming successfully entered the Macrohard company.
Xiaoming's task today is to fill up such a table:
the table has n rows and n columns, and the numbers of rows and columns are counted from 1.
where the value of the jth element in row i is the square of gcd(i, j), where
gcd is the greatest common divisor, and the following are the first four columns of the first four rows of this table:
1 1 1 1
1 4 1 4
1 1 9 1
1 4 1 16

Xiao Ming suddenly had a strange idea. He wanted to know the sum of all the elements in this table.
Since the watch is too large, he hopes to use the power of the computer.

The meaning of "input format"
is one positive integer n per line.

"Output format" is
one number per line, representing the sum of all elements. Since the answer is relatively large, please output the result modulo (10^9 + 7) (ie: one billion zero seven).

"Sample Input"
4

"Sample Output"
48

"Data Range"
For 30% of the data, n <= 1000
10% of the data exists, n = 10^5
For 60% of the data, n <= 10^6
For 100% of the data, n <= 10^7

Resource convention:
peak memory consumption (including virtual machine) < 256M
CPU consumption < 2000ms

Ideas: Mobius inversion, number theory
https://www.cnblogs.com/chenyang920/p/4811995.html
https://www.cnblogs.com/peng-ym/p/8647856.html
not
http: //www.cnblogs.com/peng-ym/p/8652288.html

Summary: The first two questions, basic questions, are not proficient; the third question, the fourth question, extensive search practice, five or six can only search violently. Come on, this level of competition is a gift.
Write the code and check it again, whether there is a–>aa, corresponding errors, etc.
Use the numbers given in the question and the debugging you have compiled, especially the first few questions, you must not be wrong! !
Write big questions as much as possible, not violence, and violence should be optimized as much as possible to pass more data;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324907834&siteId=291194637