Conservation is a very important thinking,because it can show us the essence of problems.
For example,the conservation of energy and momentum can help us solve some physical problems easily instead of thinking about the process of collision.
So the coding is.
I will talk about this from three points.
Let’s have a look at a problem.
We have a series of numbers.We can operate it as follow:Choose three adjacent numbers,add the middle to the left and the right,then turn the middle negative.
Now give you the beginning series and the ending series,please tell me if it can be operated to achieve the transform.
We will quickly find that the sum of every three adjacent numbers will not be changed however we operate it.
But it doesn’t work ,for our solution.We can get a counterexample easily:(1,2,3)and(2,1,3).
So, we must think more.
Although the sum of all three numbers doesn’t work,we can observe the sum of part of numbers.
We suppose that the three numbers are a1,a2,a3 and the part sums are S1=a1,S2=a1+a2,S3=a1+a2+a3.
Then we will find something magic.
Before transform,the sums are (S1,S2,S3).
After transform, the sums are (S2,S1,S3).
This is an amazing finding.We can solve the problem with it.
Sometimes, the problem doesn’t have ang conserved value.
But we can build a conserved value by adding weighting.
Let’s think about this problem.
We have lots of cells arranged in a row.There are some balls in some cells.We can operate it in two ways:One is reducing one ball in one of the nonempty cells and putting one ball into two cells next to it in the left sperately;the other is its contray operation.
Now I give you the begining state.You should operate it until it cannot be operated.And then give me your ending state.
When we read it,we may feel it hard to solve.
But we can give the cell some weights to change it.
Two operations tell us that two balls taken from two adjacent cells sperately are equal to the one in the cell on thier right.
So what can you imagine?
Yes, it’s Fibonacci.
We can give every cells a number of Fibonacci.
Then we will find that the sum of the numbers wil not change however we operate it.
And then ,let’s think about when it will stop.
To stop it,two things are needed:
So, these mean what we need to do is only decomposing the sum.
This is the magic of weighting conversation.
The problem is:
There are n steps.There some of your soldiers on the some of them.Every time you can regroup your soldiers into two groups.And your enemy will kill one group,and the soldiers in the other will go to the next step.If one of your soldiers arrive at the highest step,then you win.
Now I tell you where your soldiers are.Please tell me if you can win.If yes,tell me how many soldiers will get to the top.
The method is similar.
Let’s think about this. If you have two soldiers on the No.(n-1) step,can you put them in the same group?Of course, the answer is no.They must be killed together if we do like that.So we must divide them into two groups.So there must be one of them getting to the top.
The fact tell us that every two soldiers in one step are equal to the one on the higher step.
I am sure you have known how to solve it.
PS:为了锻炼自己的英文水平,我会写写英语博客。但防止因我的英语太过顽劣导致大家看不懂,我会在下面翻译一下。所以,不想看英文的可以忽略。想看的,可以帮忙找找错误。谢谢大家。(写英文不容易,给个赞再走呗)
守恒是一种非常重要的思想。它可以向我们揭示问题的本质。
例如,动量守恒和能量守恒就可以帮助我们轻易地解决很多物理问题,而避开了其中复杂的过程。
编程也是一样。
我将会从三个方面谈论这个事情。
先看个题:
我们有一个数列。我们可以按照如下操作它:选择三个连续的数字,将中间一个数字加到旁边两个数字上,然后将中间的数字变成其相反数。
现在我给你开始序列和结束序列。请你告诉我是否可以通过以上操作实现二者的转换。
我们很快会发现在我们操作的过程中三个数的总和是不变的。
但是,这对我们的解题并不起作用,我们很容易就能举出反例:(1,2,3)和(2,1,3).
因此,我们必须进一步思考。
尽管三个数的总和不起作用,但我们可以考虑部分和。
我们假设三个数是a1,a2,a3,三个部分和是S1,S2,S3.
其中S1=a1,S2=a1+a2,S3=a1+a2+a3
现在神奇的事发生了。
变化前,是(S1,S2,S3)
变化后,是(S2,S1,S3)
这是一个神奇的发现。这样我们就可以解决问题了/
有时,问题本身并没有守恒的量。
但是我们可以通过添加权值来创造守恒的量。
让我们来看看这道题:
我们有一列格子。一些格子中会放入一些球,我们可以按照两种方法来操作它:一种是从一个非空的格子中取出一个球,然后在它左边两个格子中各放入一个球;另一个操作则是它的逆操作。
现在,我给你初始的状态。你应当操作它直到不能操作为止,并告诉我最终的状态。
乍一看,有点难。
但是我们可以给格子赋权值来改变它。
这两个操作告诉我们一个事实,两个相邻的格子中的球等同于其右边一个格子里的一个球。
所以,你想到了什么?
没错,斐波那契数列!
我们可以给每一个格子一个斐波那契数。
然后我们就会发现数字的总和是守恒的。
接下来我们再来思考思考什么时候会停止。
有两个条件:
所以,这告诉我们,我们唯一要做的,就是分解我们得到的总和。
这就是权值守恒的神奇。
问题是:
有n个台阶。其中有些台阶上有一些你的士兵。每一次,你需要将你的士兵重新分组成两组。你的敌人会从中选择一组杀掉,另外一组则会上升一个台阶。如果你有一个士兵到达了最高的台阶,你就赢了。
现在我给你你的士兵的初始位置。清告诉我你是否能赢,如果能,请告诉我能到达最高层的人数。
方法是类似的。
如果你有两个士兵在第n-1层,你会把他们分在同一组吗?
显然不会。如果我们那样做,他们就会被敌人一起杀掉。因此,我们必须把他们分到两组,这样就会有其中一个士兵能到达第n个台阶。
这也就告诉我们,一个台阶上的两个士兵相当于更高一级台阶上的一个士兵。
我相信你知道怎么去解决它了。