HackerRank - Sansa and XOR
2015-04-16 14:52
453 查看
Really interesting problem! My 1st solution was DP based, like bottom-up pattern, but it got TLE since it is O(n^2). Then I thought there must be a O(n) solution. And yes there is :) It is an art of counting - what you learnt in your Combinatorics class :)
#include <cmath> #include <cstdio> #include <vector> #include <bitset> #include <map> #include <set> #include <string> #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> in(n, 0); for (int i = 0; i < n; i++) cin >> in[i]; unsigned int ret = 0; vector<int> cnt(n, 0); for (int i = 0; i < (n / 2) + n % 2; i++) cnt[n - i - 1] = cnt[i] = ((i + 1) % 2 && (n - i) % 2) ? 1 : 0; for (int i = 0; i < n; i++) if (cnt[i]) ret ^= in[i]; cout << ret << endl; } return 0; }
相关文章推荐
- HackerRank "Xor and Sum"
- HackerRank "AND xor OR"
- C++ HackerRank|AND xor OR
- hackerrank Roads and Libraries(DFS/并查集)
- HackerRank "Kundu and Tree" !!
- 异或 The Great XOR - HackerRank - the-great-xor 题解
- Hackerrank Sherlock and The Beast
- [Hackerrank题目选做] Jim And Challenges
- HackerRank "Array and simple queries" !
- hackerrank [Week of Code 33] Bonnie and Clyde
- 【公式推导+杨辉三角+异或性质+进制表示】XOR Matrix HackerRank - xor-matrix
- Hackerrank manasa-and-combinatorics(数学推导)
- 文章标题 HackerRank - mehta-and-the-typical-supermarket (容斥原理)
- Roads and Libraries HackerRank - torque-and-development
- HackerRank Hiring Contest - D Array and Queries (单调栈思想)
- HackerRank: Sam and sub-strings
- HackerRank - "Snakes and Ladders: The Quickest Way Up"
- The Great XOR (HackerRank the-great-xor)
- [hackerrank]Unique Divide And Conquer
- HackerRank "Kitty and Katty"