Transform(HDU-5637)
Problem Description
A list of n integers are given. For an integer x you can do the following operations:
+ let the binary representation of x be , you can flip one of the bits.
+ let y be an integer in the list, you can change x to x⊕y, where ⊕ means bitwise exclusive or operation.
There are several integer pairs (S,T). For each pair, you need to answer the minimum operations needed to change S to T.
Input
There are multiple test cases. The first line of input contains an integer T (T≤20), indicating the number of test cases. For each test case:
The first line contains two integer n and m (1≤n≤15,1≤m≤105) -- the number of integers given and the number of queries. The next line contains n integers a1,a2,...,an (1≤ai≤105), separated by a space.
In the next m lines, each contains two integers si and ti (1≤si,ti≤105), denoting a query.
Output
For each test cases, output an integer , where zi is the answer for i-th query.
Sample Input
1
3 3
1 2 3
3 4
1 2
3 9
Sample Output
10
題意:t 組數據,每組給出 n 個數與 m 組詢問,每組詢問有 s、t 兩個數,對于數 s 現給出兩種變換,一種是改變 s 二進制位的某一位,即 0 變 1 或 1 變 0,另一種是讓 s 從給出 n 個數當中的任意一個做異或運算,問從 s 到 t 最少要經過幾步變換,最后輸出每組的組號 i 與該組答案 zi 的和然后模 1E9+7
思路:假設?s^x^y^z^w^...^q=t 是最小操作次數,由于其等價于?0^x^y^z^w^...^q=s^t,因此只需要根據所給的 n 個數將 1E5?范圍內的所有步數求出來存到 res[] 數組中,最后根據 s、t 的值直接可以得到 res[s^t] 然后進行計算即可。
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-6 #define MOD 1000000007 #define INF 0x3f3f3f3f #define N 300001 #define LL long long using namespace std; int a[N],res[N]; bool vis[N]; void bfs(int n){memset(vis,0,sizeof(vis));for(int i=1;i<=2e5;i*=2)//在給出的a[i]基礎上向后補1、2、4、8...a[n++]=i;queue<int> Q;Q.push(0);res[0]=0;vis[0]=true;while(!Q.empty()){int top=Q.front();Q.pop();for(int i=0;i<n;i++){if(vis[a[i]^top]==false){Q.push(a[i]^top);//相應異或元素進隊res[a[i]^top]=res[top]+1;//步數+1vis[a[i]^top]=true;//標記}}} } int main() {int t;scanf("%d",&t);while(t--){int n,m;scanf("%d%d",&n,&m);for(int i=0;i<n;i++)scanf("%d",&a[i]);bfs(n);LL sum=0;int s,t;for(int i=1;i<=m;i++){scanf("%d%d",&s,&t);sum=(sum+(LL)i*res[s^t])%MOD;}printf("%d\n",(int)sum);}return 0; }?
總結
以上是生活随笔為你收集整理的Transform(HDU-5637)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 删数问题(信息学奥赛一本通-T1321)
- 下一篇: 搜索 —— 暴力搜索