【郑轻邀请赛 G】密室逃脱

【题目链接】:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2133

【题意】

【题解】

考虑每一个二进制数的最高位->第i位;
肯定是1(这里不讨论0的情况)
然后对于其余n-1个二进制数;
如果它们在第i位和它一样->也是1的话,那么异或结果是0
那么不管第i位后面的位怎么变;它肯定是变小的->我们要求的
如果它们在第i为和它不一样->即为0,那么异或结果是1;
则它肯定是变大了;
综上
我们只要确定每个数的最高位所在的位置i
然后确定其他数字在第i位上是为1的数的个数cnt;
然后加上cnt就好;

【Number Of WA

0

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) cin >> x
#define pri(x) cout << x
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e6+100;

int T,n,m,top[N],tot[N];
LL ans;
string s;

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false);
    cin >> T;
    while (T--)
    {
        ans = 0;
        cin >> n >> m;
        rep1(j,1,m) tot[j] = 0;
        rep1(i,1,n) top[i] = 0;
        rep1(i,1,n)
        {
            cin >> s;
            bool fi = true;
            rep1(j,1,m)
            {
                if (s[j-1]=='1')
                {
                    if (fi) top[i] = j,fi = false;
                    tot[j]++;
                }
            }
        }
        rep1(i,1,n)
            ans+=tot[top[i]];
        cout << ans << endl;
    }
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(122)  评论(0编辑  收藏  举报