日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

2019长安大学ACM校赛网络同步赛 LXOR (规律,数位DP)

發(fā)布時間:2023/12/18 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2019长安大学ACM校赛网络同步赛 LXOR (规律,数位DP) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

鏈接:https://ac.nowcoder.com/acm/contest/897/L
來源:牛客網(wǎng)

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

題意:

思路:
根據(jù)異或的規(guī)律,我們知道x^x=0, ^是異或運算,即兩個相等的數(shù)異或起來為0,

又因為異或運算滿足交換律和分配律。

所以 x⊕4x⊕5x=0. 可以得到,(x⊕4x)⊕5x=0.

那么當x⊕4x=5x 使?jié)M足條件,我們還知道x⊕4x=x+4x 當且僅當 x與4x 在二進制狀態(tài)下,任一位不同時為1.

而4*x 就是x在二進制狀態(tài)下 尾部補兩個0,也就是左移2位,那么為了滿足上面的條件也就要滿足 二進制數(shù)中沒有兩個1中間只有一個數(shù)。

例如二進制中不能有 101 ,111 ,這種子串。

這顯然就是數(shù)位dp了,直接爆搜肯定過不去,加一個記憶化搜索即可。

細節(jié)見代碼:

#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';}} }

轉(zhuǎn)載于:https://www.cnblogs.com/qieqiemin/p/11228817.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的2019长安大学ACM校赛网络同步赛 LXOR (规律,数位DP)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。