USACO Party Lamps 【Binary code solvution】【规律】

写这道题目的时候遇到了一个令人诧异的问题,就是平台上跑来的结果和我本机跑起来的结果不一样。

后来Debug了之后才发现是我数组开小了,只开到100 的数组竟然都去访问他170位的地址肯定要跪成翔啊...

好吧,解释一下题意。

有N盏台灯,C次操作

每次操作可以按一次按钮,一共一个四个按钮

可以得出的规律是每6个台灯是一次循环。

易得证。(其实不难发现Button 1 不用考虑,Button 2 和 Button 3 对立, 那么Button 4 的作用就是让1, 4, 7, 11改变状态

那么可以发现 7 和 1 其实是一模一样的,他们都受Button 1, 3, 4操控,依次类推即可)

还不会用BitSet来写这道题目...得学习一下了,我只开了个数组sta_ans[w][i][j][k][l][m][n] 来代表第w次操作后,后面六个代表的是二进制

这样的话每次访问都是O(1)的效率会非常快

结果:

USER: Jeremy Wu [wushuai2]
TASK: lamps
LANG: C++

Compiling...
Compile: OK

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

All tests OK.
/*
ID: wushuai2
PROG: lamps
LANG: C++
*/
//#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <stack>
#include <string>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <vector>
#include <algorithm>
#define Max(a,b) (((a) > (b)) ? (a) : (b))
#define Min(a,b) (((a) < (b)) ? (a) : (b))
#define Abs(x) (((x) > 0) ? (x) : (-(x)))
#define MOD 1000000007
#define pi acos(-1.0)
#define RV(num) ((num) > 0 ? 0 : 1)

using namespace std;

typedef long long           ll      ;
typedef unsigned long long  ull     ;
typedef unsigned int        uint    ;
typedef unsigned char       uchar   ;

template<class T> inline void checkmin(T &a,T b){if(a>b) a=b;}
template<class T> inline void checkmax(T &a,T b){if(a<b) a=b;}

const double eps = 1e-7      ;
const int M = 660000         ;
const ll P = 10000000097ll   ;
const int INF = 0x3f3f3f3f   ;
const int MAX_N = 20         ;
const int MAXSIZE = 101000000;

int N, C;
int ans[10], sum;
int sta_ans[10011][2][2][2][2][2][2];

bool check(int i, int j, int k, int l, int m, int n){
    int aaa[] = {0, i, j, k, l, m, n};
    for(int ii = 1; ii < 7; ++ii){
        if(ans[ii] == -1 || aaa[ii] == ans[ii]){
            continue;
        } else return false;
    }
    return true;
}

int main() {
    ofstream fout ("lamps.out");
    ifstream fin ("lamps.in");
    int i, j, k, l, m, n, t, s, c, w, q, num;
    memset(ans, -1, sizeof(ans));
    fin >> N;
    fin >> C;
    while(fin >> num){
        if(-1 == num)   break;
        if(num % 6 == 0)  ans[6] = 1;
        else    ans[num % 6] = 1;
    }
    while(fin >> num){
        if(-1 == num)   break;
        if(num % 6 == 0)  ans[6] = 0;
        else    ans[num % 6] = 0;
    }

    sta_ans[0][1][1][1][1][1][1] = 1;
    for(w = 1; w <= C; ++w){
        for(i = 0; i < 2; ++i){
            for(j = 0; j < 2; ++j){
                for(k = 0; k < 2; ++k){
                    for(l = 0; l < 2; ++l){
                        for(m = 0; m < 2; ++m){
                            for(n = 0; n < 2; ++n){
                                if(sta_ans[w - 1][i][j][k][l][m][n] == 1){
                                    sta_ans[w][RV(i)][RV(j)][RV(k)][RV(l)][RV(m)][RV(n)] = 1;
                                    sta_ans[w][i][RV(j)][k][RV(l)][m][RV(n)] = 1;
                                    sta_ans[w][RV(i)][j][RV(k)][l][RV(m)][n] = 1;
                                    sta_ans[w][RV(i)][j][k][RV(l)][m][n] = 1;
                                }

                            }
                        }
                    }
                }
            }
        }

    }
    for(i = 0; i < 2; ++i){
        for(j = 0; j < 2; ++j){
            for(k = 0; k < 2; ++k){
                for(l = 0; l < 2; ++l){
                    for(m = 0; m < 2; ++m){
                        for(n = 0; n < 2; ++n){
                            if(check(i, j, k, l, m, n)){
                                if(sta_ans[C][i][j][k][l][m][n] == 1){
                                    ++sum;
                                    for(int ii = 1; ii <= N; ++ii){
                                        if(ii % 6 == 0){
                                            fout << n;
                                        } else if(ii % 6 == 1){
                                            fout << i;
                                        } else if(ii % 6 == 2){
                                            fout << j;
                                        } else if(ii % 6 == 3){
                                            fout << k;
                                        } else if(ii % 6 == 4){
                                            fout << l;
                                        } else if(ii % 6 == 5){
                                            fout << m;
                                        }
                                    }
                                    fout << endl;
                                }
                            }

                        }
                    }
                }
            }
        }
    }
    if(!sum){
        fout << "IMPOSSIBLE" << endl;
    }

    fin.close();
    fout.close();
    return 0;
}
时间: 2024-10-08 07:22:58

USACO Party Lamps 【Binary code solvution】【规律】的相关文章

USACO 2.2 Party Lamps 【高能等效+规律枚举】

题在这:https://www.luogu.org/problem/show?pid=1468#sub 按钮1:当按下此按钮,将改变所有的灯:本来亮着的灯就熄灭,本来是关着的灯被点亮. 按钮2:当按下此按钮,将改变所有奇数号的灯. 按钮3:当按下此按钮,将改变所有偶数号的灯. 按钮4:当按下此按钮,将改变所有序号是3*K+1(K>=0)的灯.例如:1,4,7... 此题关键:找到4个按钮之间的关系,可发现最终状态只有少数(可枚举的)几种,又:四个操作的影响周期取最小公倍数后,发现   6个灯为一

usaco Party Lamps

题意很简单,可以看翻译http://www.wzoi.org/usaco/13%5C104.asp 因为同一个开关按两次的等于没按,所以当C大于4时,可以将C一直减2,减到小于等于4为止,然后开始枚举,分析题目之后发现可以用一个6位二进制表示前6个等,后面的等每6个和前面的一样 0和1异或上0不变,异或上1之后0变1,1变0 给当前状态异或上不同的二进制就是按了开关之后的状态. DFS就能解 /* ID: modengd1 PROG: lamps LANG: C++ */ #include <i

POJ 1850 Code(找规律)

Code Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 7913   Accepted: 3709 Description Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system

Jointly Learning Binary Code for Large-scale Face Image Retrieval and Attributes Prediction

资源下载:http://download.csdn.net/detail/lilai619/9489623

hdu 5375 Gray code(DP)

hdu 5375 Gray code Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed

HDOJ 5375 Gray code DP

标准格雷码的性质:二进制a1 a2 ... an,对应的格雷码为a1 (a1 xor a2) ... (an-1 xor an) 题目就可以转为O(n)的dp dp[i][j]表示二进制第i位为j时前i位对应最大分数. Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 298    Accepted Submissio

Adaptive Code Via C#读书笔记(1)

原书链接: http://www.amazon.com/Adaptive-Code-via-principles-Developer-ebook/dp/B00OCLLYTY/ref=dp_kinw_strp_1 这本书的标题极其失败:它不是讲C#而是讲敏捷的.不知道是因为Agile这次词用的太多了还是怎么样,弄了个Adaptive在标题里,副标题里倒是有Agile,可惜太不显眼了.如果让我来翻译的话我觉得标题可以是:代码的敏捷之道(基于C#语言进行描述).可能长了点,但毕竟能让人一眼就知道这本书

hdu5375 Gray code(动态规划)

题目: Gray code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 860    Accepted Submission(s): 490 Problem Description The reflected binary code, also known as Gray code after Frank Gray, is a bi

hdu5375 Gray code

The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only onebit (binary digit). The reflected binary code was originally designed to prevent spurious output from electr