P1146 coin flip, p1146 coin flip

Source: Internet
Author: User

P1146 coin flip, p1146 coin flip

There is a row of coins on the desktop, N in total, each coin is facing up. Now I want to flip all the coins to the back up, the rule is that every time I can flip any N-1 coins (front up is turned to the back up, and vice versa ). Find a shortest sequence of operations (turning each N-1 coin into one operation ).

Input/Output Format

Input Format:


The input contains only one row and a natural number N (N is an even number not greater than 100 ).


Output Format:


The first line of the output file contains an integer "S", indicating the minimum number of operations required. Each row in the next S line represents the status of the coin on the table after each operation (a row contains N integers (0 or 1), indicating the status of each coin: 0-positive up, and 1-the opposite side is up, no extra space is allowed ).

If there are multiple operating solutions, you only need to output the smallest lexicographically.


Input and Output sample input sample #1:
Output sample #1:

Mathematical Method: the I-th flip is to flip all coins except the I-th coin.

Below is a proof of text:

Proof 1

Define A certain n-1 operation as A class.

Define the B operation to flip all coins.

Define a C operation to flip a coin.

The problem with the subject is whether A can reach A certain state after several A operations, while A operation is equivalent to doing B once C. Note that B and C operations are interchangeable, therefore, it can be understood that the C operation is performed for several times first, and then the B operation is performed for the same number of times.

The number of C operations is equivalent to one coin flip, so the I flip is to flip all the coins except the I coin.

Proof 2

Of course, it can also be explained as follows: make a very simple change -- break down five coins each time into two steps:

1. Flip a coin once;

2. Flip all coins once.

If p is an even number, the second step above is actually offset, so it is equivalent to taking the first step every time. So p = 6.

If p is an odd number, it is equivalent to only taking the first step at a time and turning all the coins over once. This is equivalent to doing only the first step of odd numbers, and finally keeping all the coins in front and up, this is obviously not feasible.

In summary, p = 6

Proof 3

All you need to do is to flip each coin an odd number of times.

A total of six coins, five at a time.

In this case, only one coin is flipped once, three, or five times.

However, you can only flip five times at a time, but not more or less. Therefore, you must increase the total number of times to an integer multiple of 5.

Therefore, each coin is flipped over five times. 5x6 = 30 times in total

5 at a time

30/5 = 6 times

A: At least six times


 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int a[10001]; 7 int main() 8 { 9     ios::sync_with_stdio(false);10     int n;11     cin>>n;12     cout<<n<<endl;13     for(int i=1;i<=n;i++)14     {15         for(int j=1;j<=n;j++)16         {17             if(j!=i)18             {19                 a[j]==1?a[j]=0:a[j]=1;20             }21         }22         for(int i=1;i<=n;i++)23             cout<<a[i];24         cout<<endl;25     }26     return 0;27 }


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.