USACO section 2.2 Party Lamps

Keil开发中CMSIS Driver API缺失问题的排查与解决 本文详细解析Keil开发中CMSIS Driver API缺失问题的根源与解决方案。针对版本不匹配、安装不完整等常见原因,提供在线与离线安装的对比及详细排查步骤,重点涵盖版本兼容性分析与项目配置验证,帮助开发者彻底解决API缺失错误并预防问题复发。 阅读详情
Party Lamps
IOI 98

To brighten up the gala dinner of the IOI'98 we have a set of N (10 <= N <= 100) colored 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 3xK+1 (with K>=0), i.e., 1,4,7,...

A counter C 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 (0 <= C <= 10000) and the final state of some of the lamps after some operations have been executed. Write a program to determine all the possible final configurations of the N lamps that are consistent with the given information, without repetitions.

PROGRAM NAME: lamps

INPUT FORMAT

 

Line 1:N
Line 2:Final value of C
Line 3:Some lamp numbers ON in the final configuration, separated by one space and terminated by the integer -1.
Line 4:Some lamp numbers OFF in the final configuration, separated by one space and terminated by the integer -1.

No lamp will be listed twice in the input.

SAMPLE INPUT (file lamps.in)

10
1
-1
7 -1

In this case, there are 10 lamps and only one button has been pressed. Lamp 7 is OFF in the final configuration.

OUTPUT FORMAT

Lines with all the possible final configurations (without repetitions) of all the lamps. 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. The lines must be ordered from least to largest (as binary numbers).

If there are no possible configurations, output a single line with the single word `IMPOSSIBLE'

SAMPLE OUTPUT (file lamps.out)

0000000000
0101010101
0110110110

In this case, there are three possible final configurations:

  • All lamps are OFF
  • Lamps 1, 4, 7, 10 are OFF and lamps 2, 3, 5, 6, 8, 9 are ON.
  • Lamps 1, 3, 5, 7, 9 are OFF and lamps 2, 4, 6, 8, 10 are ON.

解题思路:灯的开关有四种方式,每种方式只能使用一次,因为使用两次和不使用是一样的。因此总的使用情况有2^4种,依次枚举找出符合条件的,再按从大到小排序输出即可。我是用个类存bool行表状态。因为情况最多只有16种,因此取bool型的前四位转化为十进制来排序,最后输出即可。

另外还有一点就是开关使用次数。只要前面枚举时看使用的开关数小于等于题目要求数并且使用开关数于题目要求数同奇偶即可。(因为使用两次一样的开关和不使用一样)

 

/*
ID:nealgav1
PROG:lamps
LANG:C++
*/
#include<fstream>
#include<algorithm>
#include<cstring>
using namespace std;
const int mm=230;//灯的数量
class node
{public:
  int num;//用来排序的,取vis[]前四位转化为十进制
  bool vis[mm];
  node()
  { num=0;
    memset(vis,1,sizeof(vis));//初始状态为全开
  }
}f[21];
node _ans[21];//存最后的结果
//bool vis[21][mm];
int on[mm],off[mm],ans[mm],lans,lon,loff;//最后状态的开关要求
int _num;
int n,c;
void button(int num,int x)//四种开关操作
{
  if(x==1)
  {
    for(int i=0;i<n;i++)
    f[num].vis[i]^=1;
  }
  else if(x==2)
  {
    for(int i=0;i<n;i+=2)
    f[num].vis[i]=f[num].vis[i]^1;
  }
  else if(x==3)
  {
    for(int i=1;i<n;i+=2)
    f[num].vis[i]=f[num].vis[i]^1;
  }
  else
  { for(int i=0;i<n;i+=3)
    f[num].vis[i]=f[num].vis[i]^1;
  }
}
int lowbit(int x)
{
  return x&(-x);
}
bool look_up(int num,int x)//判断状态可行性
{
  int nu=1,km=0;
  while(x)
  {
    if(x&1)
    {
      button(num,nu);km++;
    }
    x>>=1;nu++;
  }
  if(km>c||(km&1)!=(c&1))//若使用开关数大于要求或使用开关数与要求奇偶性不同
  return 0;              //则不符合条件
  for(int i=0;i<lon-1;i++)
  if(!f[num].vis[on[i]-1])
  return 0;
  for(int i=0;i<loff-1;i++)
  if(f[num].vis[off[i]-1])
  return 0;
  return 1;
}
void charge()
{
  lans=0;
  for(int i=0;i<16;i++)//枚举16种状态
  {
    if(look_up(i,i))
    {
      ans[lans++]=i;
    }
  }
}
bool cmp(node a,node b)
{
  return a.num<b.num;
}
void b_sort()//排序
{  _num=0;
  for(int i=0;i<lans;i++,_num++)
  {
   _ans[_num]=f[ans[i]];
   for(int j=0;j<8;j++)
   { _ans[_num].num<<=1;
     if(_ans[_num].vis[j])
    _ans[_num].num+=1;

   }
  }
  //cout<<"lans "<<lans<<"lans\n";
  sort(_ans,_ans+_num,cmp);
}
int main()
{
  ifstream cin("lamps.in");
  ofstream cout("lamps.out");
  cin>>n>>c;
  for(lon=0;on[lon-1]!=-1;lon++)
  {
    cin>>on[lon];
  }
  for(loff=0;off[loff-1]!=-1;loff++)
  {
    cin>>off[loff];
  }
  charge();
  b_sort();
  if(_num<=0)cout<<"IMPOSSIBLE\n";
  else
  for(int i=0;i<_num;i++)
  {for(int j=0;j<n;j++)
   cout<<_ans[i].vis[j];
   //cout<<_ans[i].num<<"\n";
   cout<<"\n";
  }
}


 

 

 

USER: Neal Gavin Gavin [nealgav1]
TASK: lamps
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.011 secs, 3356 KB]
   Test 2: TEST OK [0.000 secs, 3356 KB]
   Test 3: TEST OK [0.000 secs, 3356 KB]
   Test 4: TEST OK [0.000 secs, 3356 KB]
   Test 5: TEST OK [0.000 secs, 3356 KB]
   Test 6: TEST OK [0.000 secs, 3356 KB]
   Test 7: TEST OK [0.000 secs, 3356 KB]
   Test 8: TEST OK [0.011 secs, 3356 KB]

All tests OK.

Your program ('lamps') produced all correct answers! This is your submission #2 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----
10
0
-1
-1
------- test 2 ----
10
0
-1
1 -1
------- test 3 ----
20
3
-1
1 3 5 -1
------- test 4 ----
50
100
1 -1
-1
------- test 5 ----
75
250
-1
-1
------- test 6 ----
100
8394
1 7 13 19 25 31 37 43 49 55 -1
64 -1
------- test 7 ----
100
2000
31 86 23 -1
42 -1
------- test 8 ----
100
8950
-1
-1
Keep up the good work!


Thanks for your submission!

Party LampsRuss Cox

There are a number of insights required for this problem.

The main insight is that no matter which switches get pressed, the light pattern will repeat every six lamps. Another insight is that the order of switch presses does not matter, and that pressing a switch twice is the same as not pressing a switch at all.

Any even number of switch presses greater than four might as well just be four switch presses, and and any odd number greater than three might as well just be three presses.

We can treat the light information as describing only the first six lamps (by treating lamp n as lamp (n mod 6)), and only try pressing each switch once or not at all.

To maintain the actual information about which lights are on, we use an integer holding six bits. The least significant bit is light 6, the next least is light 5, and so on. This has the effect that treating these bit strings as normal numbers sorts the same way that we need to print them.

Switches are represented as bit vectors that say which lights get toggled.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXLAMP	6
#define LAMPMASK	((1<<MAXLAMP)-1)

int nlamp;
int nswitch;
int ison;
int known;

int poss[1<<MAXLAMP];

int flip[4] = {
    LAMPMASK,		/* flip all lights */
    LAMPMASK & 0xAA, 	/* flip odd lights */
    LAMPMASK & 0x55,	/* flip even lights */
    LAMPMASK & ((1<<(MAXLAMP-1))|(1<<(MAXLAMP-4)))	/* lights 1, 4 */
};

/*
 * Starting with current light state ``lights'', flip exactly n switches
 * with number >= i.
 */
void
search(int lights, int i, int n)
{
    if(n == 0) {
	if((lights & known) == ison)
	    poss[lights] = 1;
	return;
    }

    for(; i<4; i++)
	search(lights ^ flip[i], i+1, n-1);
}

void
printseq(FILE *fout, int lights)
{
    int i;
    char s[100+1];

    for(i=0; i<nlamp; i++)
	s[i] = (lights & (1<<(MAXLAMP-1 - i%MAXLAMP))) ? '1' : '0';
    s[nlamp] = '\0';
    fprintf(fout, "%s\n", s);
}

void
main(void)
{
    FILE *fin, *fout;
    int a, i, impossible;

    fin = fopen("lamps.in", "r");
    fout = fopen("lamps.out", "w");
    assert(fin != NULL && fout != NULL);

    fscanf(fin, "%d %d", &nlamp, &nswitch);

    for(;;) {
	fscanf(fin, "%d", &a);
	if(a == -1)
	    break;
	a = MAXLAMP-1 - (a-1) % MAXLAMP;
	ison |= 1<<a;
	known |= 1<<a;
    }

    for(;;) {
	fscanf(fin, "%d", &a);
	if(a == -1)
	    break;
	a = MAXLAMP-1 - (a-1) % MAXLAMP;
	assert((ison & (1<<a)) == 0);
	known |= 1<<a;
    }

    if(nswitch > 4)
	if(nswitch%2 == 0)
	    nswitch = 4;
	else
	    nswitch = 3;

    for(; nswitch >= 0; nswitch -= 2)
	    search(LAMPMASK, 0, nswitch);

    impossible = 1;
    for(i=0; i<(1<<MAXLAMP); i++) {
	if(poss[i]) {
	    printseq(fout, i);
	    impossible = 0;
	}
    }
    if(impossible)
	fprintf(fout, "IMPOSSIBLE\n");

    exit(0);
}

 

 

 

【人工智能】全面盘点国内AI九大模型:各擅胜场的智能先锋(豆包、文心一言、讯飞星火、通义千问、Kimi、智谱清言、腾讯元宝、秘塔、天工AI) 追求性价比与全能:选豆包,适合个人与中小企业日常使用;侧重中文创作与理解:选文心一言,文学与商业文案创作首选;需要专业语音交互:选讯飞星火,智能客服、车载场景必备;看重生态与多场景:选通义千问,适配电商、办公等企业需求;处理超长文本任务:选Kimi,文档分析、学术研究的利器;专注深度推理创作:选智谱清言,学术与创意写作的伙伴;依赖腾讯生态协同:选腾讯元宝,日常轻量需求的便捷之选;深耕法律学术领域:选秘塔,专业文本处理的权威工具;布局前沿技术体验:选天工AI,享受接近国际顶尖的智能服务。 阅读详情

相关推荐

告别手动操作!用Python脚本批量处理Siemens NX零件文件(导入/导出/移除参数)

本文详细介绍了如何利用Python脚本实现Siemens NX零件文件的批量处理,包括导入、导出和移除参数等操作。通过NX Open API和Python UF接口,工业设计师可以大幅提升工作效率,告别繁琐的手动操作。文章提供了完整的实战指南,涵盖环境配置、核心功能实现以及性能优化技巧,特别适合需要进行Siemens-NXUG二次开发的工程师参考。

weixin_28222493的博客 261

【USACO2.2.4】Party Lamps派对灯——杨子曰题目

【USACO2.2.4】Party Lamps派对灯——杨子曰题目 题目描述 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码。 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮。 按钮2:当按下此按钮,将改变所有奇数号的灯。 按钮3:当按下此按钮,将改变所有偶数号的灯。 按钮4:当按...

杨子曰:代码是神奇的 797

持续集成介绍及Jenkins自动化测试部署

学习持续集成及jenkins的自动化测试部署

ouihsiad的博客 4577

[USACO2.2]派对灯 Party Lamps 题解

洛谷原题链接:[USACO2.2]派对灯 Party Lamps - 洛谷 太细了太细了太细了!!!首先得想到多转4,然后是4转2,这是基本思路。代码实现更恶心好吧!首先得想到b[9][7]的各种情况,要想到6是循环节,然后进入主程序,第一个点就是当c==0但是还有关灯限制的是IMPOSSIBLE(其实可以有开灯限制,但是数据比较水,开灯限制没管也能过)第二个点是c=0但是没有关灯限制,此时输出全是1。接着进入主要循环,c==1的话根本不会有5,6,7三种情况,得特判去掉,最后就是还得判断虽然前面这几个点

HypnotizeG的博客 474

Docker+vLLM内网离线部署Qwen3 流程

本文介绍了在CentOS 7系统下进行VLLM容器化部署Qwen3-32B大模型的内网离线方案。首先需准备Nvidia显卡驱动、CUDA 12.4和Docker环境。通过联网机器拉取vllm/vllm-openai镜像并导入内网服务器,同时从魔塔社区下载模型文件。部署时使用docker run命令启动容器,配置了GPU资源、内存共享、端口映射等参数,特别指定了模型路径、并行计算、内存利用率等关键参数。该方案实现了大模型在内网环境的安全高效部署,适用于需要离线运行的AI推理场景。

qq_42881308的博客 1036

粒子滤波在基于误差状态量的INS/GNSS组合导航中的使用技巧-On Particle Filter based Error-State INS/GNSS Intergrated Navigation

本文面向以15维惯导(INS)误差为状态量,3维位置误差为量测量的INS/GNSS组合导航系统,​在西工大严恭敏老师的开源工具箱中PF相关库函数基础上实现基于粒子滤波(PF)的惯导误差状态量的递推估计,设计了粒子重置环节并强调了其在本特定场景下的重要性。

I33D33I的博客 1742

派对灯(poj1176 Party Lamps)

Description英文版 To brighten up the gala dinner of the IOI 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 th...

yxmbbd的博客 418

【C++PTA】6-1 Point类的运算 (10 分)

定义Point类,有坐标x,y两个私有成员变量;对Point类重载“+”(相加)、“-”(相减)和“==”(相等)运算符,实现对坐标的改变,要求用友元函数和成员函数两种方法实现。对Point类重载<<运算符,以使得代码 Point p; cout<<p<<endl;可以输出该点对象的坐标。 函数接口定义: 实现Point类。 裁判测试程序样例: /* 请在这里填写答案 */ int main(int argc, char const *argv[]) { P

libertye的博客 2367

party lamps(dfs优化+规律枚举)

Problem description: To brighten up the gala dinner of the IOI'98 we have a set of N coloured lamps numbered from1 to N. The lamps are connected to four buttons:button 1 -- when this button is...

banteng7670的博客 156

利用 Openocd 和 FT2232H 实现 ARMv8(Coretex-A53)高效调试方案

本文详细介绍了如何利用开源工具OpenOCD与高性能USB转接芯片FT2232H,构建一套针对ARMv8架构(如Cortex-A53)的高效、低成本调试方案。内容涵盖从环境搭建、驱动配置、核心调试架构解析到实战问题排查的全过程,帮助开发者实现对多核处理器的透明、可控调试,是嵌入式Linux及底层开发的实用指南。

weixin_29163581的博客 280

【毕设护航】从0构建YOLOv8犬类识别系统,120种犬类精准检测+可视化界面全流程指南

本文介绍了一个基于YOLOv8的120种犬类目标检测与可视化系统毕业设计项目。该项目使用Stanford Dogs数据集,将图像分类任务转化为目标检测任务。主要内容包括:数据预处理(XML标注转换为YOLO格式)、YOLOv8模型训练与测试(包含训练脚本编写和推理实现),以及基于Tkinter的图形界面开发。该项目技术链条完整,涵盖检测、分割等多任务支持,适合作为计算机视觉方向的毕业设计选题,兼具技术深度和展示效果。

LXY_LOVE36的博客 96

Party Lamps【DFS】

Party Lamps describe 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 presse...

Ursue的博客 236

爬虫:一文掌握 Pydoll 的详细使用(彻底改变浏览器自动化,且能绕过绕过验证码!)

Pydoll正在彻底改变浏览器自动化!与其他解决方案不同,它消除了对Web驱动程序的需求,提供了流畅可靠的自动化体验和本机异步性能。

数据知道的博客 1万+

USACO Section 2.2 Party Lamps

/* ID: lucien23 PROG: lamps LANG: C++ */ /* * 此题的技巧之处就是需要注意到任何button只要按下2的倍数次就相当于没有按 * 所以其实只需要考虑4个按钮,每个按钮是否被有效按下过一次就好 * 直接使用枚举法,一共只有2^4=16种情况 * 对于每种情况需要知道被按下的有效次数(也就是被按下过的按钮数),必须满足 * (C-有效次数)%2=0

程序缘 1222

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

USACO Training Section 2.2 Party Lamps

[url="http://ace.delos.com/usacoprob2?a=3nbwNTZTBNm&S=lamps"]英文原题[/url] [url="http://www.wzoi.org/usaco/13%5C104.asp"]中文题译[/url] 题目大意: 一组灯,几个按钮,按一个按钮可以改变若干灯的开关状态。给定按按钮的计数次数和最终状态中若干个灯的状态,要求求出所有可能状...

山中竹笋 176

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

USACO Section 2.2 Party Lamps(dfs)

Party LampsIOI 98 To brighten up the gala dinner of the IOI'98 we have a set ofN(10 <= N <= 100) colored lamps numbered from 1 toN. The lamps are connected to four buttons: Butto...

angtongyou1893的博客 179

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

USACO SECTION 2.2 Party Lamps

Party Lamps IOI 98 To brighten up the gala dinner of the IOI'98 we have a set of N (10 N. The lamps are connected to four buttons: Button 1: When this button is pressed, all the lamps chang

316

USACO-Section 2.2 Party Lamps (枚举)

灯长度是6个一循环,因为按钮1:每1个灯改变1个;按钮2:每2个灯改变1个;按钮3:每2个灯改变1个;按钮4:每3个灯改变1个,最小公倍数为6 ①易得任意一个按钮按下2次,等于该按钮按下0次 打表后观察得:②1,2,3这个3个按钮任意两个按下等同于第三个按下 由①得:任意偶数次按按钮均可以转化为0,2,4次按不同按钮,任意奇数次按按钮均可以转化为1,3次按不同按钮 由②得:所按按钮均不同时,相邻次数可以转化,即(1,2),(2,3),(3,4)这三种按不同按钮次数的组合内可以相互转化 综上:当按按钮次数大于

idealism_xxm的专栏 723

USACO-Section2.2 Party Lamps

USACO-Section2.2 Party Lamps

qq_34600424的博客 265

USACO - Chapter2 Section 2.2 - Party Lamps

Party Lamps

zhuo1ang的博客 311

USACO Section 2.2 Party Lamps - 看清本质就简单!

这题乍一看吓一跳...范围挺大的...想了也有一个小时才想通...看清本质!!     总共就4个按钮...如果我1,2,3,4都按过一次了..再按一下1 or 2 or 3 or 4灯的情况不和只按了2,3,4 or 1,3,4 or 1,2,4 or  1,2,3一样吗?因为一个按钮按两次就相当于没按..那么实际上能出现的最多情况也就是C(1,4)+C(2,4)+C(3,4)+C(4,4)=

zzyzzy12 1150

USACO-Section2.2 Party Lamps [枚举]

题目大意 在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码。 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮。 按钮2:当按下此按钮,将改变所有奇数号的灯。 按钮3:当按下此按钮,将改变所有偶数号的灯。 按钮4:当按下此按钮,将改变所有序号是3*K+1(K>=0)的灯

yoer77的博客 419

USACO section2.2 Party Lamps题解&思路

按两次灯就相当于没按…那么四个按钮就都分别只有两种状态:被按了和没有被按 枚举2^4种情况,如果这些情况需要的按键次数小于等于题目所给的次数且剩余按键次数是2的整数倍,那么这是一种符合要求的操作…然后暴力地比较所有需要比较的位数… 为什么这道题没有代码呢… 因为我发现这道题的操作是有每6位一循环的特点的…然后作死用了位运算…代码太感人…

Rainbow6174's home 657

USACO Section2.2 Party Lamps 解题报告 【icedream61】

    lamps解题报告------------------------------------------------------------------------------------------------------------------------------------------------【题目】   N个灯,编号1~N。有4个开关,和C次改变某个开关状态的机会,试问最终...

weixin_30828379的博客 140

USACO-Section2.2 Party Lamps【深度优先搜索】

题目描述:在IOI98的节日宴会上,我们有N(10<=N<=100)盏彩色灯,他们分别从1到N被标上号码。 这些灯都连接到四个按钮: 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮。 按钮2:当按下此按钮,将改变所有奇数号的灯。 按钮3:当按下此按钮,将改变所有偶数号的灯。 按钮4:当按下此按钮,将改变所有序号是3*K+1(K>=0)的灯。例如:1,4,7…

tjj1998的博客 345
上一篇: USACO section 2.2 Runaround Numbers
下一篇: 林大陈宇老师的原创高斯
剑不飞
博客等级 码龄15年 86粉丝 505原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值