HDU 6129 Just do it

題目鏈接:HDU6129

Just do it

Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1234    Accepted Submission(s): 724


Problem Description
There is a nonnegative integer sequence a1...n of length n. HazelFan wants to do a type of transformation called prefix-XOR, which means a1...n changes into b1...n, where bi equals to the XOR value of a1,...,ai. He will repeat it for m times, please tell him the final sequence.
 

Input
The first line contains a positive integer T(1T5), denoting the number of test cases.
For each test case:
The first line contains two positive integers n,m(1n2×105,1m109).
The second line contains n nonnegative integers a1...n(0ai2301).
 

Output
For each test case:
A single line contains n nonnegative integers, denoting the final sequence.
 

Sample Input
2 1 1 1 3 3 1 2 3
 

Sample Output
1 1 3 1
 

題意:實際上就是每次整體異或是從頭開始每個數依次和前面的數異或,一共整體異或m次,問最後序列的每個元素。

題目分析:列出每次異或的個數發現是楊輝三角數C(i+m-2,i-1),然後判斷奇偶,具體的證明網上有。

//
//  main.cpp
//  1010 Just do it
//
//  Created by teddywang on 2017/8/15.
//  Copyright © 2017年 teddywang. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
int n,m;
int s[222222];
int b[222222];
int T;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        memset(b,0,sizeof(b));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&s[i]);
        }
        for(int i=1;i<=n;i++)
        {
            int x=i+m-2;
            int y=i-1;
            if((x&y)==y)
            {
                for(int j=i;j<=n;j++) b[j]^=s[j-i+1];
            }
        }
        for(int i=1;i<=n;i++)
        {
            printf("%d",b[i]);
            if(i==n) printf("\n");
            else printf(" ");
        }
    }
}


發佈了168 篇原創文章 · 獲贊 4 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章