zoukankan      html  css  js  c++  java
  • Bit Magic HDU 4421 2-Sat

    Bit Magic

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1383    Accepted Submission(s): 398


    Problem Description
    Yesterday, my teacher taught me about bit operators: and (&), or (|), xor (^). I generated a number table a[N], and wrote a program to calculate the matrix table b[N][N] using three kinds of bit operator. I thought my achievement would get teacher's attention.
    The key function is the code showed below.

    There is no doubt that my teacher raised lots of interests in my work and was surprised to my talented programming skills. After deeply thinking, he came up with another problem: if we have the matrix table b[N][N] at first, can you check whether corresponding number table a[N] exists?
     
    Input
    There are multiple test cases.
    For each test case, the first line contains an integer N, indicating the size of the matrix. (1 <= N <= 500).
    The next N lines, each line contains N integers, the jth integer in ith line indicating the element b[i][j] of matrix. (0 <= b[i][j] <= 2 31 - 1)
     
    Output
    For each test case, output "YES" if corresponding number table a[N] exists; otherwise output "NO".
     
    Sample Input
    2 0 4 4 0 3 0 1 24 1 0 86 24 86 0
     
    Sample Output
    YES NO
     
    Source
     
    Recommend
    zhuyuanchen520
     
    /*
     * Author:  
     * Created Time:  2013/10/25 20:53:54
     * File Name: A.cpp
     * solve: A.cpp
     */
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 510;
    
    int sgn(const double &x) {  return (x > eps) - (x < -eps); }
    
    int b[maxn][maxn];
    int c[maxn][maxn];
    int a[maxn];
    int n;
    bool cal(int x,int y)
    {
        a[0] = x;
        rep(i,0,n)
            rep(j,0,n)
            c[i][j] = (b[i][j] >> y) & 1;//枚举每一位
    
        rep(i,1,n)
            a[i] = (c[i][i-1] == 0 ? a[i-1] : 1 - a[i-1]); 
    
        rep(i,0,n)
            rep(j,0,n)
            {
                if(i == j)
                {
                    if(c[i][j])
                        return 0;
                }else if(i%2 == 1 && j%2 == 1)
                {
                    if(c[i][j] != (a[i]|a[j]))
                        return 0;
                }else if(i%2 == 0 && j%2 == 0)
                {
                    if(c[i][j] != (a[i]&a[j]))
                        return 0;
                }else
                {
                    if(c[i][j] != (a[i] ^ a[j]))
                        return 0;
                }
            }
        return 1;
    }
    bool solve()
    {
        repf(i,0,31)
            if(!cal(0,i) && !cal(1,i))
                return 0;
        return 1;
    }
    int main() 
    {
        //freopen("in.txt","r",stdin);
        while(scanf("%d",&n) == 1)
        {
            rep(i,0,n)
                rep(j,0,n)
                scanf("%d",&b[i][j]);
            if(solve())
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
            
        }
        return 0;
    }

    后续附上2-Sat的解法

  • 相关阅读:
    C++ 4种强制类型转换
    HTTP与HTTPS异同/HTTP1.0与HTTP1.1差别
    大数据处理-Trie树
    Linux进程状态转换图
    纯css实现背景图片半透明内容不透明的方法-opacity属性正确使用
    由vue理解passive修饰符引起的思考
    Vue+VSCode开发环境配置备忘(代码格式设置)
    哎呦喂web 前端三日老师 《精通Flex布局》
    flex实战之移动页面确定按钮置底布局
    Poptip插件拖动造成IOS下与同页面下mescroll.js也被拖动的解决,即对e.preventDefault();的理解
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3394612.html
Copyright © 2011-2022 走看看