Description
To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps numbered from
1 to N. The lamps are connected to four buttons:
button 1 -- when this button is pressed, all the lamps change their state: those that are ON are turned OFF and those that are OFF are turned ON.
button 2 -- changes the state of all the odd numbered lamps.
button 3 -- changes the state of all the even numbered lamps.
button 4 -- changes the state of the lamps whose number is of the form 3K+1 (with K >= 0), i.e., 1,4,7,...
There is a counter C which records the total number of button presses.
When the party starts, all the lamps are ON and the counter C is set to zero.
You are given the value of counter C and information on the final state of some of the lamps. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.
1 to N. The lamps are connected to four buttons:
button 1 -- when this button is pressed, all the lamps change their state: those that are ON are turned OFF and those that are OFF are turned ON.
button 2 -- changes the state of all the odd numbered lamps.
button 3 -- changes the state of all the even numbered lamps.
button 4 -- changes the state of the lamps whose number is of the form 3K+1 (with K >= 0), i.e., 1,4,7,...
There is a counter C which records the total number of button presses.
When the party starts, all the lamps are ON and the counter C is set to zero.
You are given the value of counter C and information on the final state of some of the lamps. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.
Input
Your program is to read from standard input. The input contains four lines, describing the number N of lamps available, the number C of button presses, and the state of some of the lamps in the final configuration.
The first line contains the number N and the second line the final value of counter C. The third line lists the lamp numbers you are informed to be ON in the final configuration, separated by one space and terminated by the integer -1. The fourth line lists the lamp numbers you are informed to be OFF in the final configuration, separated by one space and terminated by the integer -1.
The parameters N and C are constrained by:
10 <= N <= 100
1 <= C <= 10000
The number of lamps you are informed to be ON, in the final configuration, is less than or equal to 2.The number of lamps you are informed to be OFF, in the final configuration, is less than or equal to 2.
The first line contains the number N and the second line the final value of counter C. The third line lists the lamp numbers you are informed to be ON in the final configuration, separated by one space and terminated by the integer -1. The fourth line lists the lamp numbers you are informed to be OFF in the final configuration, separated by one space and terminated by the integer -1.
The parameters N and C are constrained by:
10 <= N <= 100
1 <= C <= 10000
The number of lamps you are informed to be ON, in the final configuration, is less than or equal to 2.The number of lamps you are informed to be OFF, in the final configuration, is less than or equal to 2.
Output
Your program is to write to standard output. The output must contain all the possible final configurations (without repetitions) of all the lamps. There is at least one possible final configuration. Each possible configuration
must be written on a different line. Each line has N characters, where the first character represents the state of lamp 1 and the last character represents the state of lamp N. A 0 (zero) stands for a lamp that is OFF, and a 1 (one) stands for a lamp that
is ON. Configurations should be listed in binary ascending order.
Sample Input
10 1 -1 7 -1
Sample Output
0000000000 0101010101 0110110110
题意:有这么一串灯,有开和关两种状态。还有4个按钮,功能:1,将所有灯状态变化一次(开变关,关变开);2,将所有的奇数位置变化状态一次;3,将所有的偶数位置变化状态一次;4,将3K+1(K = 1,2,3,4...)位置变化状态一次。输入第一个数 N 是灯的数量,第二个数 C 是按按钮的次数,后面两行,第一行是必须为开的位置,第二行是必须为关的位置,两行都以-1结束输入。让你输出满足要求中所有可能的情况。
首先理解一点,那就是按钮只有按和不按两种状态,所以按2次和2次以上都可以抵消到0-1次之间。再就是按钮4是以3K+1为规律的,所以灯都是按照6为周期由0和1组成的串。
将所有按按钮情况进行统计,可以得出所有的状态【ans数组】,在进行一番推到之后,可以看出按按钮4次及以上可以出现所有状态。在4次以下的,就需要来判断 C 次能否完成【can数组】。输入一个要求后,挨着看是否满足要求就好了。
#include <cstdio>
///ans 来存放所有出现的情况
char ans[8][6] = {
'0','0','0','0','0','0',
'0','0','1','1','1','0',
'0','1','0','1','0','1',
'0','1','1','0','1','1',
'1','0','0','1','0','0',
'1','0','1','0','1','0',
'1','1','0','0','0','1',
'1','1','1','1','1','1'
};
///can[i][j]判断i状态是否能按j次实现
int can[8][4] = {
0,1,1,1,
0,0,1,1,
0,1,1,1,
0,1,0,1,
0,0,1,1,
0,1,1,1,
0,0,1,1,
1,0,1,1,
};
int lamps_on[3], lamps_off[3];
int main()
{
int n, c;
scanf("%d%d", &n, &c);
for (int i = 0; i < 3; ++i)
{
scanf("%d", &lamps_on[i]);
if (lamps_on[i] == -1)
break;
}
for (int i = 0; i < 3; ++i)
{
scanf("%d", &lamps_off[i]);
if (lamps_off[i] == -1)
break;
}
for (int i = 0; i < 8; ++i)
{
bool flag = true;
///判断要求是否满足
for (int j = 0; lamps_on[j] != -1; ++j)
{
if (ans[i][(lamps_on[j]-1)%6] == '0')
flag = false;
}
for (int j = 0; lamps_off[j] != -1; ++j)
{
if (ans[i][(lamps_off[j]-1)%6] == '1')
flag = false;
}
if (flag)///判断在规定的步数内是否可以完成
{
bool final_flag;
if (c >= 4)
final_flag = true;
else
{
if (can[i][c] == 1)
final_flag = true;
else
final_flag = false;
}
if (final_flag)
{
for (int k = 0; k < n; ++k)
printf("%c", ans[i][k%6]);
printf("\n");
}
}
}
return 0;
}