2019长安大学ACM校赛网络同步赛 LXOR (规律,数位DP)
鏈接:https://ac.nowcoder.com/acm/contest/897/L
來源:牛客網
XOR
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 32768K,其他語言65536K
64bit IO Format: %lld
題目描述
Exclusive or is a logical operation that outputs true only when inputs differ(one is true, the other is false). It is symbolized by the infix operators such as XOR,
⊕
⊕.
This time, brave QQQ raises a problem to you. Given an interval [l, r], you need to calculate how many numbers x between l and r, where x satisfies
x
⊕
4
x
⊕
5
x
=
0
x⊕4x⊕5x=0.
輸入描述:
The first line contains an integer number T, the number of test cases.
i
t
h
ith of each next T lines contains two integers l, r(
1
≤
l
≤
r
≤
10
18
1≤l≤r≤1018).
輸出描述:
For each test case print the answer.
示例1
輸入
復制
2
2 6
1 109
輸出
復制
4
39
題意:
思路:
根據異或的規律,我們知道x^x=0, ^是異或運算,即兩個相等的數異或起來為0,
又因為異或運算滿足交換律和分配律。
所以 x⊕4x⊕5x=0. 可以得到,(x⊕4x)⊕5x=0.
那么當x⊕4x=5x 使滿足條件,我們還知道x⊕4x=x+4x 當且僅當 x與4x 在二進制狀態下,任一位不同時為1.
而4*x 就是x在二進制狀態下 尾部補兩個0,也就是左移2位,那么為了滿足上面的條件也就要滿足 二進制數中沒有兩個1中間只有一個數。
例如二進制中不能有 101 ,111 ,這種子串。
這顯然就是數位dp了,直接爆搜肯定過不去,加一個記憶化搜索即可。
細節見代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl using namespace std; typedef long long ll; ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;} ll lcm(ll a, ll b) {return a / gcd(a, b) * b;} ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2) { ans = ans * a % MOD; } a = a * a % MOD; b /= 2;} return ans;} inline void getInt(int *p); const int maxn = 1000010; const int inf = 0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/int a[700]; int cnt; ll dp[70][2][2]; ll dfs(int dep, int last1, int last2, bool limit) {ll res = 0ll;if (dep == 0) {return 1ll;} else {if (limit) {int up = a[dep];for (int i = 0; i <= up; ++i) {if (i) {if (last2!=1) {res += dfs(dep - 1, i, last1, limit && (i == a[dep]));}} else {res += dfs(dep - 1, i, last1, limit && (i == a[dep]));}}return res;} else {if(dp[dep][last1][last2]!=-1){return dp[dep][last1][last2];}int up = 1;for (int i = 0; i <= up; ++i) {if (i) {if (last2!=1){res += dfs(dep - 1, i, last1, limit && (i == a[dep]));}} else {res += dfs(dep - 1, i, last1, limit && (i == a[dep]));}}dp[dep][last1][last2]=res;return res;}}}ll solve(ll x) {cnt = 0;while (x) {if (x & 1) {a[++cnt] = 1;} else {a[++cnt] = 0;}x >>= 1;}return dfs(cnt, 0, 0, 1); } int main() {//freopen("D:\\code\\text\\input.txt","r",stdin);//freopen("D:\\code\\text\\output.txt","w",stdout);int t;gbtb;cin >> t;ll l, r;memset(dp,-1,sizeof(dp));while (t--) {cin >> l >> r;cout << solve(r) - solve(l - 1) << endl;}return 0; }inline void getInt(int *p) {char ch;do {ch = getchar();} while (ch == ' ' || ch == '\n');if (ch == '-') {*p = -(getchar() - '0');while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 - ch + '0';}} else {*p = ch - '0';while ((ch = getchar()) >= '0' && ch <= '9') {*p = *p * 10 + ch - '0';}} }轉載于:https://www.cnblogs.com/qieqiemin/p/11228817.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的2019长安大学ACM校赛网络同步赛 LXOR (规律,数位DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 再谈borland与MS对BUG的不同态
- 下一篇: 每日一算法 ---- 打印九九乘法表