Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); ++i) const int MOD = int(1e9)+7; struct mint { ll x; mint(ll x=0) : x((x%MOD+MOD)%MOD) {} mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint operator+(const mint a) const { return mint(*this) += a; } }; ostream& operator<<(ostream& os, const mint& a) { return os << a.x; } const int M = 60; mint dp[M+1][2][2][2]; int main() { ll L, R; cin >> L >> R; dp[0][0][0][0] = 1; rep(i, M) { int Li = L>>(M-1-i)&1; // L の上から i 桁目 int Ri = R>>(M-1-i)&1; // R の上から i 桁目 rep(j, 2) rep(l, 2) rep(r, 2) rep(xi, 2) rep(yi, 2) { int j2 = j, l2 = l, r2 = r; // 遷移前の状態で初期化 if (xi > yi) continue; // (xi, yi) = (1, 0) なら無視 if (!j && (xi < yi)) continue; // (1, 1) が現れる前に (0, 1) が現れたら無視 if (xi&yi) j2 = 1; // (1, 1) が現れたら j2 を更新 // 条件 x ≧ L の処理 if (!l && (xi < Li)) continue; if (xi > Li) l2 = 1; // 条件 y ≦ R の処理 if (!r && (yi > Ri)) continue; if (yi < Ri) r2 = 1; dp[i+1][j2][l2][r2] += dp[i][j][l][r]; // 遷移式 } } mint ans = 0; rep(l, 2) rep(r, 2) ans += dp[M][1][l][r]; cout << ans << '\n'; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define int long long #define pi pair<int,int> #define ff first #define ss second #define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(nullptr) #include "string" const int M = 1e9 + 7; int mod( int m ,int mod = M) { m %= mod; return (m + mod) % mod; } // find y - x = x ^ y && L <= x <= y <= R int dp[60][2][2][2]; int L,R; int solve(int id, int l, int r, int cur) { if(id < 0) return 1; int &tmp = dp[id][l][r][cur]; if(tmp != -1) return tmp; tmp = 0; if(l or !((L >> id) & 1ll)) tmp = mod(tmp + solve(id - 1, l, r | ((R >> id) & 1ll), cur) ); if((l or !((L >> id) & 1LL)) && (r or ((R >> id) & 1ll)) && cur) tmp = mod(tmp + solve(id - 1, l, r, cur)); if(r or ((R >> id) & 1ll)) tmp = mod(tmp + solve(id - 1, l | !((L >> id) & 1ll) , r, 1) ); return tmp; } int32_t main(){ boost; int t = 1; //cin >> t; while(t--) { cin >> L >> R; memset(dp, -1, sizeof dp); int ans = solve(59, 0, 0, 0); cout << ans << endl; } }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll MOD = 1000000007ll; const int N_DIGITS = 60; ll L[N_DIGITS], R[N_DIGITS]; ll Dp[1 + N_DIGITS][2][2][2]; // これまでで L<x, y<R, x,y>0 が 不成立/成立 const bool Bs[2] = {false, true}; void go(ll &tgt, ll src) { (tgt += src) %= MOD; } int main() { ll l, r; cin >> l >> r; for (int i = N_DIGITS - 1; i >= 0; --i) { L[i] = l % 2; R[i] = r % 2; l /= 2; r /= 2; } Dp[0][0][0][0] = 1; for (int i = 0; i < N_DIGITS; ++i) { for (bool overL : Bs) { for (bool underR : Bs) { for (bool some : Bs) { ll src = Dp[i][overL][underR][some]; // 0,0 if (overL || L[i] == 0) { go(Dp[i + 1][overL][underR || R[i] == 1][some], src); } // 0,1 if ((overL || L[i] == 0) && (underR || R[i] == 1) && some) { go(Dp[i + 1][overL][underR][true], src); } // 1,1 if (underR || R[i] == 1) { go(Dp[i + 1][overL || L[i] == 0][underR][true], src); } } } } /* cout << "l " << L[i] << " r " << R[i] << " :"; for (bool overL : Bs) { for (bool underR : Bs) { for (bool some : Bs) { cout << " " << Dp[i][overL][underR][some]; } } } cout << endl; */ } ll res = 0; for (bool overL : Bs) { for (bool underR : Bs) { for (bool some : Bs) { (res += Dp[N_DIGITS][overL][underR][some]) %= MOD; } } } cout << res << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define N 200005 #define LL long long const int mo = 1e9+7; LL dp[70][2][2][2],num1[70],num2[70] ; LL dfs(int pos,bool L,bool R,bool flg) { if(!pos) return 1; if(~dp[pos][L][R][flg]) return dp[pos][L][R][flg]; LL res = 0; int up1 = L? num1[pos]:0,up2 = R? num2[pos]:1; if(up2) res += dfs(pos-1,num1[pos] == 1 && L,num2[pos] == 1 && R,1),res %= mo; if(up1==0 && up2 && flg) res += dfs(pos-1,num1[pos] == 0 && L,num2[pos] == 1 && R,1),res %= mo; if(!up1) res += dfs(pos-1,num1[pos] == 0 && L,num2[pos] == 0 && R,flg),res %= mo; return dp[pos][L][R][flg] = res; } LL Solve(LL L,LL R) { int cot1 = 0,cot2 = 0; while(L) num1[++cot1] = L&1,L >>= 1; while(R) num2[++cot2] = R&1,R >>= 1; return dfs(cot2,1,1,0); } int main() { memset(dp,-1,sizeof(dp)); LL L,R; cin >>L>>R; cout<<Solve(L,R); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
/*input */ #include <bits/stdc++.h> #define up(i,a,b) for(int (i) = (a);(i)<=(b);++(i)) #define down(i,b,a) for(int (i) = (b);i>=(a);--i) #define debug(x) cerr << (x) << '\n'; #define bits(x,i) ((x >> i) & 1) #define mid ((l+r)/2) #define pr pair<int,int> using namespace std; const int logA = 60; const int mod = 1e9 + 7; long long dp[logA][2][2][2]; long long l,r; long long solve(int pos,int isLow, int isHigh,int firstBit){ if (pos < 0) return 1; long long& res = dp[pos][isLow][isHigh][firstBit]; if (res != -1) return res; res = 0; int low = (isLow ? 0 : bits(l, pos)), high = (isHigh ? 1 : bits(r, pos)); for(int choice = low; choice <= high;++choice){ res += solve(pos - 1,isLow | (choice != low),isHigh | (choice != high), firstBit | choice == 1); if (choice == 1 && low == 0 && firstBit == 1) res += solve(pos-1, isLow, isHigh,1); res %= mod; } return res; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cin >> l >> r; memset(dp, -1, sizeof(dp)); cout << solve(59, 0,0,0) << '\n'; // debug int ans = 0; // for(int i=l;i<=r;++i){ // for(int j=i;j<=r;++j) if (j % i == (j ^ i)) { // ans++; // // cout << j << ' ' << i << '\n'; // } // } // cout << ans << '\n'; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define pb push_back #define fo(i, n) for(int i = 1; i <= n; ++i) typedef long long ll; typedef pair<int, int> pii; const int mod = 1e9 + 7; const int N = 1005; ll L, R; ll a[N], b[N]; ll dp[70][2][2][2]; ll calc(ll pos, int is_l, int is_r, int ch = 0) { if(pos < 0) return 1; if(dp[pos][is_l][is_r][ch] != -1) return dp[pos][is_l][is_r][ch]; ll &res = dp[pos][is_l][is_r][ch]; res = 0; // 1 1 if(!is_r or b[pos]) (res += calc(pos - 1, is_l && a[pos], is_r, 1)) %= mod; // 0 1 if((!is_r or b[pos]) and (!is_l or !a[pos]) and ch) (res += calc(pos - 1, is_l && !a[pos], is_r, 1)) %= mod; // 0 0 if(!is_l or !a[pos]) (res += calc(pos - 1, is_l && !a[pos], is_r and !b[pos], ch)) %= mod; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> L >> R; for(int i = 61; i >= 0; --i) { a[i] = (L >> i) & 1; b[i] = (R >> i) & 1; } memset(dp, -1, sizeof dp); cout << calc(61, 1, 1); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (n); ++i) const int MAX = 1e5 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; int main(){ long long L; cin >> L; long long R; cin >> R; long long dp[61][2][2][2] = {}; // Zero initialization. dp[60][0][0][0] = 1; for(int i = 59; i >= 0; --i){ int r = R >> i & 1; int l = L >> i & 1; rep(j, 2)rep(k, 2)rep(s, 2){ rep(x, 2)rep(y, 2){ if(y == 0 && x == 1) continue; int nj, nk, ns; nj = j, nk = k, ns = s; if(s == 0 && y == 1 && x == 0) continue; if(x == 1 && y == 1) ns = 1; if(k == 0 && r == 0 && y == 1) continue; if(r == 1 && y == 0) nk = 1; if(j == 0 && l == 1 && x == 0) continue; if(l == 0 && x == 1) nj = 1; dp[i][nj][nk][ns] += dp[i + 1][j][k][s]; dp[i][nj][nk][ns] %= MOD; } } } long long ans = 0; rep(j, 2)rep(k, 2)rep(s, 2){ ans += dp[0][j][k][s]; ans %= MOD; } cout << ans << endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <stdio.h> #include <string.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> int acs(const void *a, const void *b){return *(int*)a - *(int*)b;} /* 1,2,3,4.. */ int des(const void *a, const void *b){return *(int*)b - *(int*)a;} /* 8,7,6,5.. */ #define MAXN (100000) #define MOD (1000000007) uint64_t dp[61][2][2][2]; int main(void) { uint64_t l,r,ans; scanf("%ld %ld",&l, &r); dp[60][0][0][0] = 1; for(int i=59;i>=0;i--) { int lb = (l>>i)&1; int rb = (r>>i)&1; for(int j=0;j<2;j++)for(int k=0;k<2;k++)for(int s=0;s<2;s++) { for(int x=0;x<2;x++)for(int y=0;y<2;y++) { int nj = j, nk = k, ns = s; if(x && !y) continue; if(!s && (x!=y)) continue; if(!s && x && y) ns = 1; if(!j && !x && lb) continue; if(!j && x && !lb) nj = 1; if(!k && y && !rb) continue; if(!k && !y && rb) nk = 1; dp[i][nj][nk][ns] += dp[i+1][j][k][s]; dp[i][nj][nk][ns] %= MOD; } } } ans = 0; for(int j=0;j<2;j++)for(int k=0;k<2;k++)for(int s=0;s<2;s++) { ans = (ans + dp[0][j][k][s])%MOD; } printf("%ld\n",ans); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> using namespace std; //#define int long long typedef long long ll; const int M=1e9+7; int f[65][2][2],dl[65],dr[65]; ll l,r; int dfs(int pos,bool fl,bool fr){ if (!pos) return 1; int &t=f[pos][fl][fr]; if (~t) return t; t=0;int up=fr?dr[pos]:1,dn=fl?dl[pos]:0; for (int y=dn;y<=up;y++) for (int x=dn;x<=y;x++) (t+=dfs(pos-1,fl&(x==dn),fr&(y==up)))%=M; return t; } int solve(ll l,ll r){ int lenl=0,lenr=0; for (;l;l>>=1) dl[++lenl]=l&1; for (;r;r>>=1) dr[++lenr]=r&1; int ans=0; for (int i=lenl;i<=lenr;i++) (ans+=dfs(i-1,i==lenl,i==lenr))%=M; return ans; } signed main(){ memset(f,-1,sizeof(f)); scanf("%lld%lld",&l,&r); printf("%d",solve(l,r)); }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
import sys sys.setrecursionlimit(2147483647) INF=float("inf") input=lambda :sys.stdin.buffer.readline().rstrip() class ModInt(object): __MOD=10**9+7 def __init__(self,x): self.__x=x%self.__MOD def __repr__(self): return str(self.__x) def __add__(self,other): return ModInt(self.__x+other.__x) if isinstance(other,ModInt) else ModInt(self.__x+other) def __sub__(self,other): return ModInt(self.__x-other.__x) if isinstance(other,ModInt) else ModInt(self.__x-other) def __mul__(self,other): return ModInt(self.__x*other.__x) if isinstance(other,ModInt) else ModInt(self.__x*other) def __truediv__(self,other): return ModInt(self.__x*pow(other.__x,self.__MOD-2,self.__MOD)) if isinstance(other, ModInt) else ModInt(self.__x*pow(other, self.__MOD-2,self.__MOD)) def __pow__(self,other): return ModInt(pow(self.__x,other.__x,self.__MOD)) if isinstance(other,ModInt) else ModInt(pow(self.__x,other,self.__MOD)) __radd__=__add__ def __rsub__(self,other): return ModInt(other.__x-self.__x) if isinstance(other,ModInt) else ModInt(other-self.__x) __rmul__=__mul__ def __rtruediv__(self,other): return ModInt(other.__x*pow(self.__x,self.__MOD-2,self.__MOD)) if isinstance(other,ModInt) else ModInt(other*pow(self.__x,self.__MOD-2,self.__MOD)) def __rpow__(self,other): return ModInt(pow(other.__x,self.__x,self.__MOD)) if isinstance(other,ModInt) else ModInt(pow(other,self.__x,self.__MOD)) def resolve(): from itertools import product L,R=map(int,input().split()) D=61 dp=[[[[ModInt(0)]*2 for _ in range(2)] for _ in range(2)] for _ in range(D+1)] dp[D][0][0][0]=ModInt(1) for d in range(D-1,-1,-1): lb=L>>d&1; rb=R>>d&1 for i,j,m,x,y in product([0,1],repeat=5): ni,nj,nm=i,j,m if(x>y): continue # i:L<=X if(i==0 and lb>x): continue if(lb<x): ni=1 # j:Y<=R if(j==0 and y>rb): continue if(y<rb): nj=1 # m:MSB if(m==0 and x!=y): continue if(x==1 and y==1): nm=1 dp[d][ni][nj][nm]+=dp[d+1][i][j][m]; print(sum(dp[0][i][j][m] for i,j,m in product([0,1],repeat=3))) resolve()
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include "bits/stdc++.h" #define MAXN 100009 #define INF 1000000007 #define mp(x,y) make_pair(x,y) #define all(v) v.begin(),v.end() #define pb(x) push_back(x) #define wr cout<<"----------------"<<endl; #define ppb() pop_back() #define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++) #define ff first #define ss second #define my_little_dodge 46 #define debug(x) cerr<< #x <<" = "<< x<<endl; using namespace std; typedef long long ll; typedef pair<int,int> PII; template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;} template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;} int dp[60][2][2][2][2][2]; string A,B; int f(int x,int y,int e){ if(!e) return (!x and y); return !(x and !y); } int rec(int pos,int a,int b,int c,int d,int e){//a-y,b-x,y-x if(pos==60) return (!a and !b and !c and !f(e,0,d)); int &ret=dp[pos][a][b][c][d][e]; if(~ret) return ret;ret=0; for(int i=0;i<2;i++)//y for(int j=0;j<2;j++)//x if(!c){ ret+=rec(pos+1,f(A[pos]-'0',i,a),f(B[pos]-'0',j,b),f(i,j,c), f(e,i,d),j); if(ret>=INF) ret-=INF; } return ret; } string men(ll x){ string res; while(x>0){ res+=(x%2)+'0'; x/=2; } while(res.size()<60) res+='0'; return res; } int f(ll a,ll b){ memset(dp,-1,sizeof dp); A=men(a);B=men(b); return rec(0,0,0,0,0,0); } int main(){ //freopen("file.in", "r", stdin); ll l,r; scanf("%lld%lld",&l,&r); int ans=f(r,r)-f(r,l-1); if(ans<0) ans+=INF; printf("%d\n",ans); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <cmath> #include <bitset> #include <vector> #include <map> #include <set> #include <queue> #include <deque> #include <algorithm> #include <complex> #include <unordered_map> #include <unordered_set> #include <random> #include <cassert> #include <fstream> #include <utility> #include <functional> #define popcount __builtin_popcount using namespace std; typedef long long int ll; typedef pair<int, int> P; const ll MOD=1e9+7; int main() { ll l, r; cin>>l>>r; ll dp[2][2][60]={}; for(int i=0; i<60; i++){ if(l>=(1ll<<(i+1)) || r<(1ll<<i)) continue; int p=0, q=0; if((1ll<<i)>l) p=1; if(r>=(1ll<<(i+1))) q=1; dp[p][q][i]=1; } for(int i=59; i>=1; i--){ (dp[1][1][i-1]+=3*dp[1][1][i])%=MOD; if((r&(1ll<<(i-1)))==0){ (dp[1][0][i-1]+=dp[1][0][i])%=MOD; }else{ (dp[1][1][i-1]+=dp[1][0][i])%=MOD; (dp[1][0][i-1]+=2*dp[1][0][i])%=MOD; } if((l&(1ll<<(i-1)))!=0){ (dp[0][1][i-1]+=dp[0][1][i])%=MOD; }else{ (dp[1][1][i-1]+=dp[0][1][i])%=MOD; (dp[0][1][i-1]+=2*dp[0][1][i])%=MOD; } if((r&(1ll<<(i-1)))==0 && (l&(1ll<<(i-1)))!=0){ }else if((r&(1ll<<(i-1)))==0){ (dp[0][0][i-1]+=dp[0][0][i])%=MOD; }else if((l&(1ll<<(i-1)))!=0){ (dp[0][0][i-1]+=dp[0][0][i])%=MOD; }else{ (dp[0][1][i-1]+=dp[0][0][i])%=MOD; (dp[1][0][i-1]+=dp[0][0][i])%=MOD; (dp[0][0][i-1]+=dp[0][0][i])%=MOD; } } ll ans=0; for(int i=0; i<2; i++) for(int j=0; j<2; j++) (ans+=dp[i][j][0])%=MOD; cout<<ans<<endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<iostream> #include<bitset> using namespace std; typedef long long ll; const ll MOD = 1e9 + 7; bitset<64> L, R; ll memo[60][2][2][2]; ll dfs(int pos, int flagX, int flagY, int flagZ) { if (pos == -1) return 1; if (memo[pos][flagX][flagY][flagZ] != -1) { return memo[pos][flagX][flagY][flagZ]; } ll ret = 0; if (flagX || L[pos] == 0) { ret = (ret + dfs(pos - 1, flagX, (R[pos] == 1 ? 1 : flagY), flagZ)) % MOD; } if ((flagX || L[pos] == 0) && (flagY || R[pos] == 1) && flagZ) { ret = (ret + dfs(pos - 1, flagX, flagY, flagZ)) % MOD; } if (flagY || R[pos] == 1) { ret = (ret + dfs(pos - 1, (L[pos] == 0 ? 1 : flagX), flagY, 1)) % MOD; } return memo[pos][flagX][flagY][flagZ] = ret; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll l, r; cin >> l >> r; L = bitset<64>(l); R = bitset<64>(r); for (int i = 0; i < 60; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { for (int l = 0; l < 2; ++l) { memo[i][j][k][l] = -1; } } } } cout << dfs(59, 0, 0, 0) << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <cstdio> #include <iostream> using namespace std; #define Mod 1000000007 #define ll long long ll l,r; ll p[130]; int main(){ cin>>l>>r; p[0]=1; for(int i=1;i<=120;i++){ p[i]=p[i-1]*3%Mod; } for(int i=62;i>=0;i--){ if((((1ll<<i)&l)^((1ll<<i)&r))!=0){ break; } if(l&(1ll<<i)){ l^=(1ll<<i); r^=(1ll<<i); } } ll ans=0; ll high,low; for(int i=62;i>=0;i--){ if((1ll<<i)&r){ high=i; break; } } for(int i=62;i>=0;i--){ if((1ll<<i)&l){ low=i; break; } } if(l==r&&l==0){ puts("1"); return 0; } if(l==0){ ll now=1; for(int i=high;i>=0;i--){ if((1ll<<i)&r){ ans+=p[i]*now%Mod; ans%=Mod; now<<=1; now%=Mod; } } ans+=now; ans%=Mod; cout<<ans<<endl; return 0; } ll now=1; for(int i=high-1;i>=0;i--){ if((1ll<<i)&r){ ans+=p[i]*now%Mod; ans%=Mod; now<<=1; now%=Mod; } } ans+=now; ans%=Mod; now=1; for(int i=low-1;i>=0;i--){ if(!((1ll<<i)&l)){ ans+=p[i]*now%Mod; ans%=Mod; now<<=1; now%=Mod; } } ans+=now; ans%=Mod; for(int i=low+1;i<high;i++){ ans+=p[i]; ans%=Mod; } cout<<ans<<endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <iostream> #include <algorithm> long long int l, r; long long int mod = 1e9 + 7; long long int memo[65][3][3][3] = { 0 }; int getBit(long long int nr, long long int pos) { return ((1LL << pos) & nr) > 0; } long long int f(int pos, int flagx, int flagy, int msb) { if (pos == -1) return 1; if (memo[pos][flagx][flagy][msb] != -1) return memo[pos][flagx][flagy][msb]; long long int cur = 0; //x0 y0 int lbit = getBit(l, pos); int rbit = getBit(r, pos); if (flagx || lbit == 0) cur += f(pos - 1, flagx, rbit ? rbit : flagy, msb); //x0 y1 if ((flagx || lbit == 0) && (flagy || rbit == 1) && msb) cur += f(pos - 1, flagx, flagy, msb); //x1 y1 if (flagy || rbit == 1) cur += f(pos - 1, !lbit ? 1 : flagx, flagy, 1); cur %= mod; memo[pos][flagx][flagy][msb] = cur; return cur; } int main() { std::cin >> l >> r; for (int i = 0; i < 64; ++i) { for (int j = 0; j < 2; ++j) { for (int k = 0; k < 2; ++k) { for (int q = 0; q < 2; ++q) memo[i][j][k][q] = -1; } } } std::cout << f(62, 0, 0, 0); }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include"bits/stdc++.h" using namespace std; using ll = int64_t; ll L, R; constexpr ll MAX_BIT = 64; constexpr ll MOD = 1e9 + 7; bitset<MAX_BIT> L_B, R_B; map<tuple<ll, bool, bool, bool>, pair<bool, ll>> memo; ll solve(ll i, bool X_ge_L, bool Y_le_R, bool appear_LSB) { if (i == MAX_BIT) { return 1; } auto curr_tuple = make_tuple(i, X_ge_L, Y_le_R, appear_LSB); if (memo[curr_tuple].first) { return memo[curr_tuple].second; } memo[curr_tuple].first = true; ll result = 0; //(1)x 0, y 0とする // こうすることができるのはすでにx >= Lが確定している場合か、Lの対応位置が0である場合 if (X_ge_L || !L_B[MAX_BIT - 1 - i]) { //Rの対応位置が1だったらy <= Rが確定する (result += solve(i + 1, X_ge_L, R_B[MAX_BIT - 1 - i] || Y_le_R, appear_LSB)) %= MOD; } //(2)x 0, y 1とする // こうすることができるのはすでにx >= Lが確定している場合か、Lの対応位置が0である場合 かつ // すでにy <= Rが確定している場合か、Rの対応位置が1である場合 かつ // すでにx 1, y 1となる部分が最左ビットとして現れている if ((X_ge_L || !L_B[MAX_BIT - 1 - i]) && (Y_le_R || R_B[MAX_BIT - 1 - i]) && appear_LSB) { (result += solve(i + 1, X_ge_L, Y_le_R, appear_LSB)) %= MOD; } //(3)x 1, y 1とする // こうすることができるのはすでにy <= Rが確定している場合か、Rの対応位置が1である場合 if (Y_le_R || R_B[MAX_BIT - 1 - i]) { //x 1, y 1という部分が現れたとしてappear_LSBを立てる (result += solve(i + 1, !L_B[MAX_BIT - 1 - i] || X_ge_L, Y_le_R, true)) %= MOD; } //制約の関係からx 1, y 0となることはないので以上で全部 return memo[curr_tuple].second = result; } int main() { cin >> L >> R; L_B = bitset<MAX_BIT>(L); R_B = bitset<MAX_BIT>(R); cout << solve(0, false, false, false) << endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i = 0; i < (int)(n); i++) uint64_t p3[100]; uint64_t mod = 1000000007; map<pair<uint64_t, uint64_t>, uint64_t> memo; uint64_t solve(uint64_t l, uint64_t r, int b){ if(l > r) return 0; if(l == r) return 1; if(l == 0 && r == (1ull << b) - 1){ return p3[b]; } auto it = memo.find(make_pair(l, r)); if(it != memo.end()){ return it->second; } uint64_t h = 1ull << (b - 1); uint64_t ret = 0; // 0, 0 if(l < h){ ret += solve(l, min(r, h - 1), b - 1); } // 0, 1 if(l < h && r >= h){ ret += solve(l, r - h, b - 1); } // 1, 1 if(r >= h){ ret += solve(max(l, h) - h, r - h, b - 1); } ret %= mod; memo[make_pair(l, r)] = ret; return ret; } int main(){ uint64_t l, r; cin >> l >> r; p3[0] = 1; REP(i, 100) if(i > 0){ p3[i] = p3[i - 1] * 3 % mod; } uint64_t ret = 0; REP(b, 62){ uint64_t m = 1ull << b; uint64_t n = m * 2 - 1; if(l <= n && r >= m){ ret += solve(max(l, m) - m, min(r, n) - m, b); ret %= mod; } } cout << ret << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <vector> #include <iostream> #include <bitset> typedef long long int ll; using namespace std; ll l, r; bitset<60> L, R; ll memo[60][2][2][2]; void init(ll l, ll r){ L = bitset<60> (l); R = bitset<60> (r); //std::cout << L << '\n'; //std::cout << R << '\n'; for(int i=0;i<60;i++) for(int j=0;j<2;j++) for(int k=0;k<2;k++)for(int t=0;t<2;t++) memo[i][j][k][t] = -1; } ll f(ll pos, int flagX, int flagY, int flagZ){ if(pos==-1) return 1; if(memo[pos][flagX][flagY][flagZ]!=-1) return memo[pos][flagX][flagY][flagZ]; ll ret = 0; if(flagX||!L[pos]) ret += f(pos-1, flagX, (R[pos]?1:flagY), flagZ); if((flagX||!L[pos])&&(flagY||R[pos])&&flagZ) ret += f(pos-1, flagX, flagY, flagZ); if(flagY||R[pos]) ret += f(pos-1, (!L[pos]?1:flagX), flagY, 1); ret %= 1000000007; return memo[pos][flagX][flagY][flagZ] = ret; } int main(int argc, char const *argv[]) { std::cin >> l >> r; init(l, r); std::cout << (f(59, 0, 0, 0)) << '\n'; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; using Int = int_fast64_t; constexpr Int mod = 1e9+7; const vector<vector<vector<Int>>> a = { { {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 2, 0}, {0, 0, 1, 3} }, { {1, 0, 0, 0}, {1, 2, 0, 0}, {1, 0, 2, 0}, {0, 1, 1, 3} }, { {0, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 3} }, { {1, 0, 0, 0}, {0, 2, 0, 0}, {0, 0, 1, 0}, {0, 1, 0, 3} } }; Int l, r; vector<vector<Int>> dp; vector<Int> p(vector<vector<Int>> b, vector<Int> v){ vector<Int> res(v.size(), 0); for(size_t i=0; i<v.size(); ++i) for(size_t j=0; j<b[i].size(); ++j) res[i] = (res[i] + (b[i][j] * v[j]) % mod) % mod; return res; } int main(){ cin.tie(0); ios::sync_with_stdio(false); cin >> l >> r; Int cl = 0; while((l >> cl) > 0) ++cl; Int cr = 0; while((r >> cr) > 0) ++cr; // cerr << cr << " " << cl << "\n"; dp.resize(cr, vector<Int>(4, 0)); if(cl == cr){ dp[0][0] = 1; }else{ dp[0][1] = 1; for(size_t i=1; (Int)i<cr-cl; ++i) dp[i][3] = 1; dp[cr-cl][2] = 1; } // for(size_t i=0; i<dp.size(); ++i) // for(size_t j=0; j<4; ++j) // cerr << dp[i][j] << " \n"[j==3]; for(size_t i=1; i<dp.size(); ++i){ size_t j = dp.size() - i - 1; size_t k = ((l >> j) & 1) * 2 + ((r >> j) & 1); // cerr << j << " " << k << "\n"; vector<Int> t = p(a[k], dp[i-1]); for(size_t l=0; l<4; ++l) dp[i][l] = (dp[i][l] + t[l]) % mod; } Int ans = 0; for(auto i:dp.back()) ans = (ans + i) % mod; cout << ans << "\n"; // for(size_t i=0; i<dp.size(); ++i) // for(size_t j=0; j<4; ++j) // cerr << dp[i][j] << " \n"[j==3]; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <iostream> using namespace std; long long L, R; long long dp[65][2][2][2][2]; long long mod = 1000000007; long long solve(int pos, int bit1, int gir1, int gir2, int gir3) { // pos : 現在の桁数 // bit1 : 大きい方の前のビット // gir1 : x がギリギリかどうか // gir2 : y がギリギリかどうか // gir3 : y <= 2x がギリギリかどうか if (pos == -1) { if (bit1 == 1 && gir3 == 1) return 0; return 1; } if (dp[pos][bit1][gir1][gir2][gir3] >= 1) return dp[pos][bit1][gir1][gir2][gir3] - 1LL; long long ret = 0; // ケース 1 : 両方 0 にするパターン if ((gir1 == 0 || (L & (1LL << pos)) == 0) && (gir3 == 0 || bit1 == 0)) { long long c1 = pos - 1; long long c2 = 0; long long c3 = gir1; long long c4 = gir2; if ((R & (1LL << pos)) != 0) c4 = 0; long long c5 = gir3; ret += solve(c1, c2, c3, c4, c5); ret %= mod; } // ケース 1 : 両方 1 にするパターン if ((gir2 == 0 || (R & (1LL << pos)) != 0)) { long long c1 = pos - 1; long long c2 = 1; long long c3 = gir1; if ((L & (1LL << pos)) == 0) c3 = 0; long long c4 = gir2; long long c5 = gir3; if (bit1 == 0) c5 = 0; ret += solve(c1, c2, c3, c4, c5); ret %= mod; } // ケース 3 : 小さい方を 0、大きい方を 1 にするパターン if ((gir1 == 0 || (L & (1LL << pos)) == 0) && (gir2 == 0 || (R & (1LL << pos)) != 0) && (gir3 == 0 || bit1 == 0)) { long long c1 = pos - 1; long long c2 = 1; long long c3 = gir1; long long c4 = gir2; long long c5 = gir3; ret += solve(c1, c2, c3, c4, c5); ret %= mod; } dp[pos][bit1][gir1][gir2][gir3] = ret + 1; return ret; } int main() { cin >> L >> R; long long ans = solve(60, 0, 1, 1, 1); cout << ans << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> #define ts cout<<"ok"<<endl #define int long long #define hh puts("") #define pc putchar #define mo 1000000007 //#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) //char buf[1<<21],*p1=buf,*p2=buf; using namespace std; const int N=65; int l,r,lenl,lenr,pl[N],pr[N],dp[N][2][2],ans; inline int read(){ int ret=0,ff=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();} while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();} return ret*ff; } void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);} void writeln(int x){write(x),hh;} void writesp(int x){write(x),pc(' ');} int dfs(int pos,int x1,int x2){ if(!pos) return 1; if(dp[pos][x1][x2]!=-1) return dp[pos][x1][x2]; int &res=dp[pos][x1][x2];res=0; int t1=x1?pl[pos]:0,t2=x2?pr[pos]:1; for(int y=t1;y<=t2;y++) for(int x=t1;x<=y;x++) res=(res+dfs(pos-1,x1&(x==t1),x2&(y==t2)))%mo; return res; } signed main(){ memset(dp,-1,sizeof(dp)); l=read(),r=read(); while(l){ pl[++lenl]=l&1; l>>=1; } while(r){ pr[++lenr]=r&1; r>>=1; } for(int i=lenl;i<=lenr;i++) ans=(ans+dfs(i-1,i==lenl,i==lenr))%mo;//枚举哪个最高位为1 write(ans); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> using namespace std; #define fs first #define sc second #define pb push_back #define mp make_pair #define eb emplace_back #define ALL(A) A.begin(),A.end() #define RALL(A) A.rbegin(),A.rend() typedef long long LL; typedef pair<LL,LL> P; const LL mod=1000000007; const LL LINF=1LL<<60; const int INF=1<<30; int dx[]={1,0,-1,0}; int dy[]={0,1,0,-1}; LL l,r; LL dp[100][2][2][2]; LL rec(int k,bool yr,bool xl,bool f){ if(k==-1) return f; if(~dp[k][yr][xl][f]) return dp[k][yr][xl][f]; LL ret=0; int y=yr?(r>>k&1):1; int x=xl?(l>>k&1):0; if(!f){ ret = (ret + rec(k-1,yr,x&xl,true))%mod; if(x==0) ret = (ret + rec(k-1,false,xl,false))%mod; } else{ for (int i = 0; i <= y; i++) { if(i==0){ if(x==0){ ret = (ret + rec(k-1,(y==i)&yr,xl,f))%mod; } } else{ if(x==1){ ret = (ret + rec(k-1,y&yr,xl,f))%mod; } else{ ret = (ret + rec(k-1,y&yr,xl,f))%mod; ret = (ret + rec(k-1,y&yr,false,f))%mod; } } } } return dp[k][yr][xl][f] = ret; } int main(){ cin >> l >> r; memset(dp,-1,sizeof(dp)); LL t = r; int s=0; while(t){ s++; t/=2; } cout << rec(s-1,true,true,false) << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
L, R = map(int, input().split()) mod = 10 ** 9 +7 l = '{:060b}'.format(L) r = '{:060b}'.format(R) memo = [[[[-1]*2 for i in range(2)] for j in range(2)] for k in range(60)] def f(pos, flagL, flagR, flagM): if pos == 60: return 1 if memo[pos][flagL][flagR][flagM] != -1: return memo[pos][flagL][flagR][flagM] ret = 0 #(0, 0) if flagL or l[pos] == '0': ret+=f(pos+1, flagL, 1 if r[pos]=='1' else flagR, flagM) #(0, 1) if flagM and (flagL or l[pos]=='0') and (flagR or r[pos]=='1'): ret += f(pos+1, flagL, flagR, flagM) #(1, 1) if flagR or r[pos]=='1': ret += f(pos+1, 1 if l[pos]=='0' else flagL, flagR, 1) memo[pos][flagL][flagR][flagM] = ret%mod return ret%mod print(f(0, 0, 0, 0)%mod)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> #define ts cout<<"ok"<<endl #define int long long #define hh puts("") #define pc putchar #define mo 1000000007 //#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) //char buf[1<<21],*p1=buf,*p2=buf; using namespace std; const int N=65; int l,r,lenl,lenr,pl[N],pr[N],dp[N][2][2],ans; inline int read(){ int ret=0,ff=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-') ff=-1;ch=getchar();} while(isdigit(ch)){ret=ret*10+(ch^48);ch=getchar();} return ret*ff; } void write(int x){if(x<0){x=-x,pc('-');}if(x>9) write(x/10);pc(x%10+48);} void writeln(int x){write(x),hh;} void writesp(int x){write(x),pc(' ');} //问题可以转化成y-x=y^x的对数,那么y二进制下为1,x为0或1,y二进制下为0,x必为0 int dfs(int pos,int x1,int x2){ if(!pos) return 1; if(dp[pos][x1][x2]!=-1) return dp[pos][x1][x2]; int &res=dp[pos][x1][x2];res=0; int t1=x1?pl[pos]:0,t2=x2?pr[pos]:1; for(int y=t1;y<=t2;y++) for(int x=t1;x<=y;x++) res=(res+dfs(pos-1,x1&(x==t1),x2&(y==t2)))%mo; return res; } signed main(){ memset(dp,-1,sizeof(dp)); l=read(),r=read(); while(l){ pl[++lenl]=l&1; l>>=1; } while(r){ pr[++lenr]=r&1; r>>=1; } for(int i=lenl;i<=lenr;i++) ans=(ans+dfs(i-1,i==lenl,i==lenr))%mo;//枚举哪个最高位为1 write(ans); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef pair<int,int>P; const int MOD=1000000007; const int INF=0x3f3f3f3f; const ll INFL=0x3f3f3f3f3f3f3f3f; ll dp[62][2][2][2]; ll solve(ll L,ll R){ dp[61][0][0][0]=1; for(int i=60;i>=0;i--)rep(j,2)rep(k,2)rep(t,2){ int bit=R>>i&1; int lim=(k?1:bit); for(int l=0;l<=lim;l++){ int bit2=L>>i&1; int lim2=(t?0:bit2); if(l==1&&j==0)lim2=1; for(int m=lim2;m<=l;m++){ (dp[i][j||l][k||l<bit][t||bit2<m]+=dp[i+1][j][k][t])%=MOD; } } } ll ans=0; rep(j,2)rep(k,2)rep(t,2){ (ans+=dp[0][j][k][t])%=MOD; } return ans; } int main(){ ll l,r;cin>>l>>r; cout<<solve(l,r)<<endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i)) using namespace std; template <int32_t MOD> struct mint { int32_t value; mint() : value() {} mint(int64_t value_) : value(value_ < 0 ? value_ % MOD + MOD : value_ >= MOD ? value_ % MOD : value_) {} inline mint<MOD> operator + (mint<MOD> other) const { int32_t c = this->value + other.value; return mint<MOD>(c >= MOD ? c - MOD : c); } inline mint<MOD> & operator += (mint<MOD> other) { this->value += other.value; if (this->value >= MOD) this->value -= MOD; return *this; } }; int64_t msb(int64_t n) { if (not n) return 0; return 1ull << (63 - __builtin_clzll(n)); } constexpr int MOD = 1e9 + 7; mint<MOD> solve(int64_t l, int64_t r) { array<array<mint<MOD>, 2>, 2> cur = {}; for (int64_t i = 1ull << 62; i; i >>= 1) { array<array<mint<MOD>, 2>, 2> prv = cur; cur = {}; if (msb(l) <= i and i <= msb(r)) { cur[i == msb(l)][i == msb(r)] += 1; } REP (p, 2) REP (q, 2) REP (xy, 3) { bool x = (xy >= 2); bool y = (xy >= 1); if (p and (l & i) and not x) continue; if (q and not (r & i) and y) continue; bool np = p and ((bool)(l & i) == x); bool nq = q and ((bool)(r & i) == y); cur[np][nq] += prv[p][q]; } } mint<MOD> cnt = 0; REP (p, 2) REP (q, 2) { cnt += cur[p][q]; } return cnt; } int main() { int64_t l, r; cin >> l >> r; cout << solve(l, r).value << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
L, R = map(int, input().split()) MOD = 10**9 + 7 *BL, = map(int, bin(L)[2:]) *BR, = map(int, bin(R)[2:]) a = len(BL) b = len(BR) BL = [0]*(len(BR) - len(BL)) + BL BL.reverse() BR.reverse() memo = [[[-1]*2 for i in range(2)] for j in range(70)] def dfs(i, p, q): if memo[i][p][q] != -1: return memo[i][p][q] if i == -1: memo[i][p][q] = 1 return 1 r = 0 if p == q == 0: if BL[i] == 1 and BR[i] == 0: memo[i][0][0] = 0 return 0 if BL[i] == 1 or BR[i] == 0: r = dfs(i-1, 0, 0) else: r = (dfs(i-1, 0, 1) + dfs(i-1, 1, 0) + dfs(i-1, 0, 0)) % MOD elif p == 0: if BL[i] == 1: r = dfs(i-1, 0, 1) else: r = (2*dfs(i-1, 0, 1) + dfs(i-1, 1, 1)) % MOD elif q == 0: if BR[i] == 0: r = dfs(i-1, 1, 0) else: r = (2*dfs(i-1, 1, 0) + dfs(i-1, 1, 1)) % MOD else: r = 3 * dfs(i-1, 1, 1) memo[i][p][q] = r return r ans = 0 for l in range(a-1, b): ans += dfs(l-1, +(not a-1 == l), +(not b-1 == l)) print(ans % MOD)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <cstdio> #include <algorithm> using namespace std; typedef long long LL; #define Mod 1000000007 LL l, r; int wr[100], wl[100], Dp[100][8]; inline int Inc(int u, int v) { return u + v - (u + v >= Mod ? Mod : 0); } bool CheckR(int state, int w, int br) { return (state & 1) || (br <= wr[w]); } bool CheckL(int state, int w, int bl) { return (state & 2) || (bl >= wl[w]); } bool CheckS(int state, int bl, int br) { return (state & 4) || (bl == br); } int Transfer(int state, int w, int bl, int br) { int res = 0; if ((state & 1) || br < wr[w]) res |= 1; if ((state & 2) || bl > wl[w]) res |= 2; if ((state & 4) || (bl == 1)) res |= 4; return res; } int Solve() { int sz = 0; for (LL y = r; y; y >>= 1) wr[++ sz] = (y & 1); for (int i = 1; i <= sz; i ++) wl[i] = (l >> (i - 1) & 1); Dp[sz + 1][0] = 1; for (int w = sz + 1; w; w --) for (int state = 0; state < 8; state ++) { if (!Dp[w][state]) continue ; for (int br = 0; br < 2; br ++) for (int bl = 0; bl <= br; bl ++) { if (!CheckR(state, w - 1, br) || !CheckL(state, w - 1, bl) || !CheckS(state, bl, br)) continue ; int _state = Transfer(state, w - 1, bl, br); Dp[w - 1][_state] = Inc(Dp[w - 1][_state], Dp[w][state]); } } int res = 0; for (int state = 0; state < 8; state ++) res = Inc(res, Dp[1][state]); return res; } int main() { scanf("%lld%lld", &l, &r); printf("%d\n", Solve()); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
def solve(L,R): n=R.bit_length() dp=[[0 for i in range(8)] for i in range(n)] if R&1==1 and L&1==1: dp[0][0]=1 dp[0][1]=1 dp[0][2]=2 dp[0][3]=3 dp[0][4]=1 dp[0][5]=1 dp[0][6]=2 dp[0][7]=3 elif R&1==1 and L&1==0: dp[0][0]=2 dp[0][1]=3 dp[0][2]=2 dp[0][3]=3 dp[0][4]=2 dp[0][5]=3 dp[0][6]=2 dp[0][7]=3 elif R&1==0 and L&1==1: dp[0][0]=0 dp[0][1]=0 dp[0][2]=1 dp[0][3]=1 dp[0][4]=1 dp[0][5]=1 dp[0][6]=2 dp[0][7]=3 else: dp[0][0]=1 dp[0][1]=1 dp[0][2]=1 dp[0][3]=1 dp[0][4]=2 dp[0][5]=3 dp[0][6]=2 dp[0][7]=3 for i in range(1,n): for j in range(8): if j==0: if R>>i &1==1 and L>>i &1==1: dp[i][j]=dp[i-1][1] elif R>>i &1==1 and L>>i &1==0: dp[i][j]=dp[i-1][4]+dp[i-1][3] elif R>>i&1 ==0 and L>>i&1 ==0: dp[i][j]=dp[i-1][0] elif j==1: if R>>i &1==1 and L>>i &1==1: dp[i][j]=dp[i-1][1] elif R>>i &1==1 and L>>i &1==0: dp[i][j]=dp[i-1][5]+dp[i-1][3]+dp[i-1][1] elif R>>i&1==0 and L>>i&1==0: dp[i][j]=dp[i-1][1] elif j==2: if R>>i&1==1: dp[i][j]=dp[i-1][6]+dp[i-1][3] else: dp[i][j]=dp[i-1][2] elif j==3: if R>>i&1==1: dp[i][j]=dp[i-1][7]+2*dp[i-1][3] else: dp[i][j]=dp[i-1][3] elif j==4: if L>>i&1==1: dp[i][j]=dp[i-1][5] else: dp[i][j]=dp[i-1][7]+dp[i-1][4] elif j==5: if L>>i&1==1: dp[i][j]=dp[i-1][5] else: dp[i][j]=2*dp[i-1][5]+dp[i-1][7] elif j==6: dp[i][j]=dp[i-1][6]+dp[i-1][7] else: dp[i][j]=3*dp[i-1][7] return dp[-1][0] mod=10**9+7 L,R=map(int,input().split()) print(solve(L,R)%mod)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
import sys import math sys.setrecursionlimit(10 ** 6) input = sys.stdin.readline def main(): md = 10 ** 9 + 7 l, r = map(int, input().split()) n = int(math.log2(r)) + 1 # dp[i][t][f][g] i桁目まで見て最上位が決定済みか(t)、L<xが確定(f)、y<Rが確定 dp = [[[[0] * 2 for _ in range(2)] for __ in range(2)] for ___ in range(n + 1)] dp[0][0][0][0] = 1 ans = 0 for i in range(n): lk = l >> (n - 1 - i) & 1 rk = r >> (n - 1 - i) & 1 for t in range(2): for f in range(2): for g in range(2): pre = dp[i][t][f][g] for x, y in [(0, 0), (0, 1), (1, 1)]: nt, nf, ng = t, f, g if t == 0 and (x, y) == (0, 1): continue if f == 0 and lk > x: continue if g == 0 and y > rk: continue if (x, y) == (1, 1): nt = 1 if lk == 0 and x == 1: nf = 1 if y == 0 and rk == 1: ng = 1 if i == n - 1: ans = (ans + pre) % md else: dp[i + 1][nt][nf][ng] = (dp[i + 1][nt][nf][ng] + pre) % md print(ans) main()
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <memory.h> #include <iostream> #define ll long long #define N 63 #define mod 1000000007 int dp[N][2][2][2]; ll L, R; int add(int x, int y) { int ret = (x + y); if (ret >= mod) { ret -= mod; } return ret; } int solveDp(int ind, int msb, int xeq, int yeq) { if (ind < 0) { if (msb) { return 1; } return 0; } int &ret = dp[ind][msb][xeq][yeq]; if (ret != -1) { return ret; } ret = 0; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { if (i > j) { continue; } if (xeq && i == 0 && L&(1LL << ind)) { continue; } if (yeq && j == 1 && (R&(1LL << ind)) == 0) { continue; } int nxeq, nyeq; nxeq = xeq; nyeq = yeq; if (xeq && i == 1 && (L&(1LL << ind)) ==0 ) { nxeq = 0; } if (yeq && j == 0 && (R&(1LL << ind))) { nyeq = 0; } if (msb == 0) { if (i != j) { continue; } if (i == 1) { ret = add(ret, solveDp(ind - 1, 1, nxeq, nyeq)); } else { ret = add(ret, solveDp(ind - 1, 0, nxeq, nyeq)); } } else { ret = add(ret, solveDp(ind - 1, 1, nxeq, nyeq)); } } } return ret; } void solve() { scanf("%lld %lld", &L, &R); memset(dp, -1, sizeof(dp)); printf("%d\n", solveDp(N-1, 0, 1, 1)); } int main() { //freopen("input.txt", "r", stdin); solve(); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define SZ(x) (int)(x.size()) using ll = long long; using ld = long double; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vector<int>>; using vll = vector<ll>; using vvll = vector<vector<ll>>; const double eps = 1e-10; const int MOD = 1000000007; const int INF = 1000000000; const ll LINF = 1ll<<50; template<typename T> void printv(const vector<T>& s) { for(int i=0;i<(int)(s.size());++i) { cout << s[i]; if(i == (int)(s.size())-1) cout << endl; else cout << " "; } } ll l, r; ll dp[2][2][2][62]; ll f(int d, int msb, int lt, int rt) { if(d == -1) return msb == 1; if(dp[msb][lt][rt][d] >= 0) return dp[msb][lt][rt][d]; ll ret = 0; for(int a=0;a<2;++a) { for(int b=0;b<2;++b) { if(a == 0 && lt && (l & (1ll<<d)) > 0) continue; if(b == 1 && rt && (r & (1ll<<d)) == 0) continue; if(a>b) continue; if(msb == 0 && a != b) continue; int nlt = lt && (((l>>d)&1)==a); int nrt = rt && (((r>>d)&1)==b); int nmsb = msb || (a == 1 && b == 1); ret += f(d-1, nmsb, nlt, nrt); } } return dp[msb][lt][rt][d] = ret % MOD; } int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(10); cin >> l >> r; for(int i=0;i<2;++i) { for(int j=0;j<2;++j) { for(int k=0;k<2;++k) { for(int l=0;l<62;++l) { dp[i][j][k][l] = -1; } } } } cout << f(61, 0, 1, 1) << endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; int dp[60][2][2][2] = {}; long long L, R; void add(int &x, int y) { x = (x + y) % MOD; } bool bit(long long X, int i) { return (X >> i & 1); } int f(int pos, int x, int y, int s) { if(pos == -1) return 1; if(dp[pos][x][y][s] != -1) return dp[pos][x][y][s]; int ret = 0; if(x || !bit(L, pos)) add(ret, f(pos - 1, x, (bit(R, pos) ? 1 : y), s)); if((x || !bit(L, pos)) && (y || bit(R, pos)) && s) add(ret, f(pos - 1, x, y, s)); if(y || bit(R, pos)) add(ret, f(pos - 1, (!bit(L, pos) ? 1 : x), y, 1)); dp[pos][x][y][s] = ret; return ret; } int main() { cin >> L >> R; memset(dp, -1, sizeof(dp)); cout << f(59, 0, 0, 0) << endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> #define dbug(x) cout<<#x<<"="<<x<<endl using namespace std; template <typename T> void read(T &t) { t=0; char ch=getchar(); int f=1; while ('0'>ch||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do {(t*=10)+=ch-'0';ch=getchar();} while ('0'<=ch&&ch<='9'); t*=f; } typedef long long ll; const ll mod=(1e9)+7; int mx=63; int d[70],e[70]; ll dp[70][2][2][3]; void update(ll &x,ll y) { x+=y; x%=mod; } ll solve(ll L,ll R) { for (int i=0;i<mx;i++) { if (R&(1LL<<i)) d[i]=1; else d[i]=0; if (L&(1LL<<i)) e[i]=1; else e[i]=0; } //for (int i=0;i<mx;i++) printf("%d",d[i]); printf("\n"); //for (int i=0;i<mx;i++) printf("%d",e[i]); printf("\n"); memset(dp,0,sizeof(dp)); dp[mx][1][1][0]=1; int C; for (int i=mx;i>=1;i--) { for (int j=0;j<=1;j++) for (int k=0;k<=1;k++) for (int B=0;B<=2;B++) { //printf("%d %d %d %d %lld\n",i,j,k,B,dp[i][j][k][B]); for (int a=0;a<=1;a++) for (int b=0;b<=1;b++) { if (j&&a>d[i-1]) continue; if (k&&b>e[i-1]) continue; if (a<b) continue; if (B==2) C=2; else { if (b||B) C=1; else if (a-b) C=2; else C=0; } update(dp[i-1][j&(a==d[i-1])][k&(b==e[i-1])][C],dp[i][j][k][B]); } } } ll res=0; for (int j=0;j<=1;j++) for (int k=0;k<=1;k++) update(res,dp[0][j][k][1]); //printf("%lld\n",res); return res; } int main() { //freopen("1.txt","r",stdin); //freopen("1.out","w",stdout); ll L,R; read(L); read(R); ll ans=solve(R,R)-solve(L-1,R); ans=(ans%mod+mod)%mod; printf("%lld\n",ans); /*ans=0; for (int x=1;x<=R;x++) for (int y=x;y<=R;y++) if (y%x==(x^y)) ans++; printf("%lld\n",ans);*/ return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
L, R = map(int, input().split()) MOD = 10 ** 9 + 7 l = '{:060b}'.format(L)[::-1] # 0左詰の二進数60桁のstr を逆向きにスライス r = '{:060b}'.format(R)[::-1] # (桁数60 は R<=10**18<2**60 より) memo = [[[[-1 for l in range(2)] for k in range(2)] for j in range(2)] for i in range(60)] # flagZは、既にx=y=1の位があったかチェックしている(MSB) def f(pos, flagX, flagY, flagZ): if pos == -1: return 1 if memo[pos][flagX][flagY][flagZ] != -1: return memo[pos][flagX][flagY][flagZ] ret = 0 # x 0, y 0 if flagX or l[pos] == '0': ret += f(pos - 1, flagX, 1 if r[pos] == '1' else flagY, flagZ) # x 0, y 1 if (flagX or l[pos] == '0') and (flagY or r[pos] == '1') and flagZ: ret += f(pos - 1, flagX, flagY, flagZ) # x 1, y 1 if flagY or r[pos] == '1': ret += f(pos - 1, 1 if l[pos] == '0' else flagX, flagY, 1) ret %= MOD memo[pos][flagX][flagY][flagZ] = ret return ret ans = f(59, 0, 0, 0) #最大桁からスタート print(ans)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> using namespace std; #define Mo(a) (a)>=mo?(a)-=mo:(a) long long l,r; const int mo=1e9+7; int Mem[70][2][2][2],len,R[100],cnt,a,ans,L[100]; long long le; int DFS(int l,int mark,int h,int f) {//mark= <l h= >0 f= <r // printf("%d %d %d %d %d %d\n",l,mark,h,f,f?R[l]:1,mark?L[l]:0); if(l==0)return h; if(~Mem[l][mark][h][f])return Mem[l][mark][h][f]; int re=0,ma=f?R[l]:1,mi=mark?L[l]:0; if(ma) { if(mi==0&&h) re+=DFS(l-1,mark&&mi==0,h,f&&ma==1); re+=DFS(l-1,mark&&mi==1,1,f&&ma==1); Mo(re); } if(mi==0) { re+=DFS(l-1,mark&&mi==0,h,f&&ma==0); Mo(re); } Mem[l][mark][h][f]=re; return re; } int DO() { len=0; while(r)R[++len]=r&1,r>>=1; le=1; for(int i=1; i<=len; i++,le<<=1) L[i]=((l&le)>0); return DFS(len,1,0,1); } int main() { memset(Mem,-1,sizeof(Mem)); scanf("%lld %lld",&l,&r); printf("%d",DO()); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define mod 1000000007 long long l,r,ans,dp[65][2][2]; long long solve(int idx,bool f1,bool f2) { if (idx<0) return 1; if (dp[idx][f1][f2]!=-1) return dp[idx][f1][f2]; long long ret=0; bool b1=(r&(1LL<<idx)),b2=(l&(1LL<<idx)); for (int i=0;i<2;i++) { if (f1 && i==1 && !b1) continue; for (int j=0;j<=i;j++) { if (f2 && !j && b2) continue; ret=(ret+solve(idx-1,(f1 && i==b1),(f2 && j==b2)))%mod; } } return dp[idx][f1][f2]=ret; } int main() { cin >> l >> r; int j=60; for (;!(r&(1LL<<j));j--); for (int i=j;;i--) { memset(dp,-1,sizeof(dp)); ans=(ans+solve(i-1,(i==j),(l&(1LL<<i))))%mod; if (l&(1LL<<i)) break; } cout << ans; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> #define MOD 1000000007LL using namespace std; typedef long long ll; typedef pair<int,int> P; ll l,r; ll bit[2][65]; ll dp[66][2][2]; ll ans; void solve(ll l,ll r){ if(l>r)return; //printf("%lld %lld\n",l,r); int v=0; ll tl=l,tr=r; while(l>0){ bit[0][v]=l%2LL; v++; l/=2LL; } v=0; while(r>0){ bit[1][v]=r%2LL; v++; r/=2LL; } memset(dp,0,sizeof(dp)); if(bit[0][v-1]==0){ dp[v-1][1][0]=1; }else{ dp[v-1][0][0]=1; } for(int i=v-2;i>=0;i--){ for(int j=0;j<2;j++){ for(int k=0;k<2;k++){ for(int a=0;a<2;a++){ if(j==0 && a==0 && bit[0][i]==1)continue; int nj=j; if(a==1 && bit[0][i]==0)nj=1; for(int b=0;b<2;b++){ if(k==0 && b==1 && bit[1][i]==0)continue; if(a==1 && b==0)continue; int nk=k; if(b==0 && bit[1][i]==1)nk=1; dp[i][nj][nk]+=dp[i+1][j][k]; dp[i][nj][nk]%=MOD; } } } } } for(int i=0;i<2;i++){ for(int j=0;j<2;j++){ ans+=dp[0][i][j]; ans%=MOD; } } l=tl; r=tr; ll ig=1; for(int i=0;i<v-1;i++){ ig*=2LL; } ig--; solve(l,ig); } int main(void){ scanf("%lld%lld",&l,&r); if(r==1LL){ printf("1\n"); return 0; } solve(l,r); printf("%lld\n",ans); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; using ll = long long; // https://atcoder.jp/contests/abc138/submissions/7075855 を参考 #define mod 1000000007 // y と x は2進数で同じ桁数である必要がある // このとき, y % x = y - x // 繰り下がりが無いことが必要十分 ll L, R; int dp[61][2][2][2]; // 現在の桁, Lの制約の有無, Rの制約の有無, 桁数が合ってるかどうか int solve(int i, bool L_Free, bool R_Free, bool flag){ if(i == -1) return 1; int &res = dp[i][L_Free][R_Free][flag]; if(res != -1) return res; res = 0; int lX = 0, rX = 1, lY = 0, rY = 1; if (! L_Free) lX = (L >> i) & 1; if (! R_Free) rY = (R >> i) & 1; for (int x = lX; x <= rX; ++x) { for (int y = lY; y <= rY; ++y) { if (x <= y && (flag || x == y) ) { res += solve(i-1, L_Free | (x > lX), R_Free | (y < rY), flag | (x == 1)); res %= mod; } } } return res; } int main() { cin >> L >> R; memset(dp, -1, sizeof dp); cout << solve(60, 0, 0, 0); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> #define fo(i,a,b) for((i)=(a);i<=(b);i++) #define rfo(i,a,b) for((i)=(a);i>=(b);i--) #define inrange(x,y,z) (((x)>=(y))&&((x)<=(z))) #define ALL(vec) ((vec).begin(),(vec).end()) #define SOR(vec) sort(ALL(vec)) #define UNI(vec) (vec).erase(unique(ALL(vec)),(vec).end()) using namespace std; #define tvple pair<int,pair<long long,long long> > #define make_tvple(x,y,z) make_pair((x),make_pair((y),(z))) #define mo 1000000007 map<tvple,int> mp; int p3[70],aans; long long l,r; int solve(int d,long long l,long long r){ tvple tmp=make_tvple(d,l,r); if(mp.count(tmp)) return mp[tmp]; long long s=(1ll<<d); long long t=(1ll<<(d+1))-1; if((r<s)||(l>t)) return mp[tmp]=0; if(l<=s&&t<=r) return mp[tmp]=p3[d]; r=min(r,t); l=max(l,s); long long mid=(s+(1ll<<(d-1))); long long dif=(1ll<<(d-1)); if(r<mid) return mp[tmp]=solve(d-1,l-dif,r-dif); if(l>=mid) return mp[tmp]=solve(d-1,l-s,r-s); int re=(solve(d-1,l-dif,r)+solve(d-1,dif,r-s))%mo; if(l-dif<=r-s) re=(re+solve(d-1,l-dif,r-s))%mo; return mp[tmp]=re; } int main(){ #ifdef FILIN #ifndef DavidDesktop freopen(FILIN,"r",stdin); freopen(FILOUT,"w",stdout); #endif #endif ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); p3[0]=1; for(int i=1;i<=69;i++){ p3[i]=((p3[i-1]+p3[i-1])%mo+p3[i-1])%mo; } cin>>l>>r; for(int i=0;i<=60;i++){ aans=(aans+solve(i,l,r))%mo; } cout<<aans<<endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
l,r = map(int,input().split()) lb = l.bit_length() rb = r.bit_length() ans = 0 mod = 10**9+7 def ignorel(r): ret = 0 cntr = 0 rb = r.bit_length() for i in range(rb-1)[::-1]: if r&1<<i: ret += 2**cntr*3**i cntr += 1 ret += 2**cntr return ret%mod def ignorer(l): cntl = 0 ret = 0 lb = l.bit_length() for i in range(lb)[::-1]: if l&1<<i == 0: ret += 2**cntl*3**i cntl += 1 ret += 2**cntl return ret%mod for i in range(lb+1,rb): ans += 3**(i-1) ans %= mod if lb != rb: ans += ignorer(l) ans += ignorel(r) ans %= mod print(ans) else: def from0(x): if x == 0: return 1 elif x == 1: return 3 xb = x.bit_length() ret = 3**(xb-1)+2*from0(x-(1<<(xb-1))) return ret def calc(l,r): ret = 0 lb = l.bit_length() rb = r.bit_length() if l > r: return 0 if l == r: return 1 if l == 2**(lb-1): return ignorel(r) elif r == 2**rb-1: return ignorer(l) for i in range(lb)[::-1]: if l&1<<i == 0 and r&1<<i: ret += from0((1<<i)-1-(l&(1<<i)-1)) ret += from0(r&((1<<i)-1)) ret += calc((l&((1<<i)-1))+(1<<i),(r&((1<<i)-1))+(1<<i)) break return ret%mod ans += calc(l,r) print(ans%mod)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
mod = 10**9 + 7 K = 60 L, R = map(int, input().split()) Rb, Lb = [0], [0] for i in range(K): Rb.append((R>>i)%2) Lb.append((L>>i)%2) if Rb[-1]: Rm = i+1 if Lb[-1]: Lm = i+1 dp =[[1, 1, 1, 1] for _ in range(K+1)] r = 0 for i in range(1, K+1): dp[i][0] = dp[i-1][0] * 3 % mod if Rb[i]: dp[i][1] = dp[i-1][1] * 2 + dp[i-1][0] % mod else: dp[i][1] = dp[i-1][1] if Lb[i]: dp[i][2] = dp[i-1][2] else: dp[i][2] = dp[i-1][2] * 2 + dp[i-1][0] % mod if Rb[i] and Lb[i]: dp[i][3] = dp[i-1][3] elif Rb[i]: dp[i][3] = dp[i-1][1] + dp[i-1][2] + dp[i-1][3] % mod elif Lb[i]: dp[i][3] = 0 else: dp[i][3] = dp[i-1][3] if Rm > i and Lm < i: r += dp[i-1][0] elif Rm > i and Lm == i: # このとき確実にLb[i] == 1 r += dp[i-1][2] elif Rm == i and Lm < i: r += dp[i-1][1] elif Rm == i and Lm == i: r += dp[i-1][3] else: r += 0 r = r % mod print(r)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
l,r=map(int,input().split()) M=len(bin(r))-2 L=format(l,"b").zfill(M) R=format(r,"b") mod=10**9+7 dp=[[0 for l in range(70)] for i in range(5)] if L[0]=="1": dp[2][0]=1 else: dp[3][0]=1 dp[0][0]=1 for i in range(1,M): if L[i]=="1" and R[i]=="1": dp[1][i]=(dp[0][i-1]+dp[1][i-1])%mod dp[2][i]=(dp[2][i-1])%mod dp[3][i]=(2*dp[3][i-1])%mod dp[4][i]=(dp[3][i-1]+3*dp[4][i-1])%mod elif L[i]=="1" and R[i]=="0": dp[1][i]=dp[1][i-1]+dp[0][i-1] dp[3][i]=dp[3][i-1] dp[4][i]=(dp[4][i-1]*3)%mod elif L[i]=="0" and R[i]=="1": dp[0][i]=dp[0][i-1] dp[1][i]=(2*dp[1][i-1]+dp[2][i-1])%mod dp[2][i]=dp[2][i-1] dp[3][i]=(dp[2][i-1]+dp[3][i-1]*2)%mod dp[4][i]=(dp[0][i-1]+dp[1][i-1]+dp[3][i-1]+dp[4][i-1]*3)%mod else: dp[0][i]=dp[0][i-1] dp[1][i]=(dp[1][i-1]*2)%mod dp[2][i]=dp[2][i-1] dp[3][i]=(dp[3][i-1])%mod dp[4][i]=(dp[0][i-1]+dp[1][i-1]+dp[4][i-1]*3)%mod ans=0 for i in range(5): ans+=dp[i][M-1] ans%=mod print(ans)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
MOD = 10**9 + 7 L, R = [int(item) for item in input().split()] L_blen = L.bit_length() R_blen = R.bit_length() # dp[y loose/tight][x loose/tight][index] dp = [[[0] * (R_blen+1) for _ in range(2)] for _ in range(2)] for i in range(R_blen + 1): L_bit = L & (1 << R_blen - i) R_bit = R & (1 << R_blen - i) # Form R's MSB to L's LSB can be the initial bit curbit_idx = R_blen - i + 1 if curbit_idx <= R_blen and curbit_idx >= L_blen: dp[R_blen == curbit_idx][L_blen == curbit_idx][i] += 1 # R=0, L=0 if not R_bit and not L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i-1] dp[0][0][i] += dp[0][1][i-1] # y=0, x=0 dp[1][1][i] += dp[1][1][i-1] dp[1][0][i] += dp[1][0][i-1] dp[0][0][i] += dp[0][0][i-1] dp[0][1][i] += dp[0][1][i-1] # y=1, x=0 dp[0][1][i] += dp[0][1][i-1] dp[0][0][i] += dp[0][0][i-1] # R=1, L=0 if R_bit and not L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i-1] dp[1][0][i] += dp[1][0][i-1] dp[0][0][i] += dp[0][1][i-1] dp[1][0][i] += dp[1][1][i-1] # y=0, x=0 dp[0][0][i] += dp[0][0][i-1] dp[0][0][i] += dp[1][0][i-1] dp[0][1][i] += dp[0][1][i-1] dp[0][1][i] += dp[1][1][i-1] # y=1, x=0 dp[0][0][i] += dp[0][0][i-1] dp[1][0][i] += dp[1][0][i-1] dp[0][1][i] += dp[0][1][i-1] dp[1][1][i] += dp[1][1][i-1] # R=0, L=1 if not R_bit and L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i-1] dp[0][1][i] += dp[0][1][i-1] # y=0, x=0 dp[0][0][i] += dp[0][0][i-1] dp[1][0][i] += dp[1][0][i-1] # y=1, x=0 dp[0][0][i] += dp[0][0][i-1] # R=1, L=1 if R_bit and L_bit: # y=1, x=1 dp[0][0][i] += dp[0][0][i-1] dp[1][0][i] += dp[1][0][i-1] dp[0][1][i] += dp[0][1][i-1] dp[1][1][i] += dp[1][1][i-1] # y=0, x=0 dp[0][0][i] += dp[1][0][i-1] dp[0][0][i] += dp[0][0][i-1] # y=1, x=0 dp[1][0][i] += dp[1][0][i-1] dp[0][0][i] += dp[0][0][i-1] # Take MOD for i in range(2): for j in range(2): dp[i][j][i] %= MOD ans = 0 for i in range(2): for j in range(2): ans += dp[i][j][-1] ans %= MOD print(ans)
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define int long long const int MAX = 1000000007; vector<int> k[26]; signed main(){ int L, R; cin>>L>>R; int l=0,r=0,q=1; while(L>=(q<<l))l++; while(R>=(q<<r))r++; int dpl[l],dpr[r],u; dpl[0]=1;u=1; for(int i=1;i<l;i++){ if(1&(L>>(i-1))){ dpl[i]=dpl[i-1]; }else{ dpl[i]=(2*dpl[i-1]+u)%MAX; } u=(u*3)%MAX; } // cerr<<'t'; u=1;dpr[0]=1; for(int i=1;i<r;i++){ if(1&(R>>(i-1))){ dpr[i]=(2*dpr[i-1]+u)%MAX; }else{ dpr[i]=dpr[i-1]; } u=(u*3)%MAX; } //cerr<<'u'; if(l!=r){ u=1; for(int i=0;i<l;i++)u=(u*3)%MAX; int ans=(dpl[l-1]+dpr[r-1])%MAX; for(int i=l+1;i<r;i++){ ans=(ans+u)%MAX; u=(u*3)%MAX; } cout<<ans; }else{ int ans=0; for(int i=l-1;i>=0;i--){ if((1&(R>>i))&&(!(1&(L>>i)))){ ans=(ans+dpr[i]+dpl[i])%MAX; } if((1&(L>>i))&&(!(1&(R>>i)))){ ans-=1; break; } } ans++; cout<<ans; } return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) using ll = long long; const int MD = 1e9+7; int la, ra; ll l, r, ans; ll exp(ll b, ll n){ ll res = 1; for(; n; n/=2,(b*=b)%=MD) if(n%2) (res *= b) %= MD; return res; } int main(){ scanf("%lld%lld", &l, &r); for(; l>>la; la++); la--; for(; r>>ra; ra++); ra--; r = ~r; if(ra != la) for(int i = la; i <= ra; i++){ if(la < i && i < ra) (ans += exp(3,i)) %= MD; else{ ll dp1 = 1, dp2 = 0; for(int j = i-1; j >= 0; j--){ if((la == i ? l : r)>>j&1) (dp2 *= 3) %= MD; else{ dp2 = (dp2*3 + dp1) % MD; (dp1 *= 2) %= MD; } } (ans += dp2 + dp1) %= MD; } }else{ ll dp1 = 1, dp2 = 0, dp3 = 0, dp4 = 0; r = ~r; int j = ra-1; for(; j >= 0; j--) if((r^l)>>j&1) break; for(; j >= 0; j--){ int lb = l>>j&1, rb = r>>j&1; if(lb && rb){ dp4 =(dp4*3 + dp2) % MD; (dp2 *= 2) %= MD; }else if(!lb && rb){ dp4 = (dp4*3 + dp2 + dp3) % MD; dp2 = (dp2*2 + dp1) % MD; dp3 = (dp3*2 + dp1) % MD; }else if(lb && !rb){ (dp4 *= 3) %= MD; dp1 = 0; }else if(!lb && !rb){ dp4 =(dp4*3 + dp3) % MD; (dp3 *= 2) %= MD; } } ans = (dp1 + dp2 + dp3 +dp4) % MD; } printf("%lld\n", ans); }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL long long const int mod = 1e9 + 7; LL dp[66][2][2],L, R; LL dfs(int x, bool Left, bool Right, bool zero) { if(x < 0) return 1; if(dp[x][Left][Right] >= 0 && !zero) return dp[x][Left][Right]; LL ans = 0; int l = 0, r = 1; if(Left) l = L >> x & 1; if(Right) r = R >> x & 1; for(int i = l ; i <= 1 ; i++) { for (int j = i ; j <= r ; j++) { if (j == 1 && zero) { if(i == 1) ans += dfs(x-1, Left && i == l, Right && j == r, zero && j == 0); } else ans += dfs(x-1, Left && i == l, Right && j == r, zero && j == 0); } } ans %= mod; if(!zero) dp[x][Left][Right] = ans; return ans; } int main() { scanf("%lld%lld", & L, & R); memset(dp, -1, sizeof(dp)); printf("%lld \n", dfs(60, 1, 1, 1)); //system("pause"); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> #define mp make_pair #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define debug(x) cerr << #x << " " << x << '\n' using namespace std; using ll = long long; using pii = pair<int,int>; using pli = pair<ll,int>; const int INF = 0x3f3f3f3f, N = 65; const ll LINF = 1e18 + 5; const int mod = 1e9 + 7; ll dp[N][2][2][2], L, R; ll dfs(int pos, int limx, int limy, int lead, ll x, ll y) { if(pos==-1) return 1; if(dp[pos][limx][limy][lead]!=-1) return dp[pos][limx][limy][lead]; int upx = limx ? (x>>pos)&1 : 1; int upy = limy ? (y>>pos)&1 : 1; ll ans = 0; for(int i=0; i<=upx; i++) for(int j=0; j<=upy; j++) { if(lead && i==j) (ans += dfs(pos-1, limx&&i==upx, limy&&j==upy, lead&&i==0, x, y)) %= mod; else if(!lead && j>=i) (ans += dfs(pos-1, limx&&i==upx, limy&&j==upy, lead, x, y)) %= mod; } return dp[pos][limx][limy][lead] = ans; } ll solve(ll l, ll r) { memset(dp, -1, sizeof(dp)); int len = 0; ll x = max(l, r); for(int i=0; i<63; i++) if((x>>i)&1) len = i; ll ans = dfs(len, 1, 1, 1, l, r); return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> L >> R; ll ans = (solve(R, R) - solve(L-1, R) -solve(R, L-1) + solve(L-1, L-1))%mod; ans = (ans + mod)%mod; cout << ans; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include "bits/stdc++.h" using namespace std; typedef long long ll; const int INF = (1<<30); const ll INFLL = (1ll<<60); const ll MOD = (ll)(1e9+7); #define l_ength size void mul_mod(ll& a, ll b){ a *= b; a %= MOD; } void add_mod(ll& a, ll b){ a = (a<MOD)?a:(a-MOD); b = (b<MOD)?b:(b-MOD); a += b; a = (a<MOD)?a:(a-MOD); } ll dp[63][2][2][2][2]; int a[63],b[63],f[10]; int main(void){ int i,j,k; ll l,r,ans=0ll; cin >> l >> r; for(i=0; i<60; ++i){ b[i] = l&1; l >>= 1; a[i] = r&1; r >>= 1; } reverse(a,a+60); reverse(b,b+60); dp[0][1][1][0][1] = 1ll; for(i=0; i<60; ++i){ for(j=0; j<64; ++j){ for(k=0; k<6; ++k){ f[k] = ((j&(1<<k))?1:0); } if(f[4] == 0 && f[5] == 1){ continue; } if(f[0] && f[4]>a[i]){ continue; } if(f[1] && f[5]<b[i]){ continue; } if(f[3] && f[2]>f[5]){ continue; } add_mod(dp[i+1][((f[0]&&(f[4]==a[i]))?1:0)][((f[1]&&(f[5]==b[i]))?1:0)][f[4]][((f[3] && (f[2]==f[5]))?1:0)],dp[i][f[0]][f[1]][f[2]][f[3]]); } } for(j=0; j<16; ++j){ for(k=0; k<4; ++k){ f[k] = (j&(1<<k))?1:0; } if(f[3]){ continue; } add_mod(ans,dp[60][f[0]][f[1]][f[2]][f[3]]); } cout << ans << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int LGN = 60; const int Z = 1e9+7; void addm(int &a, int b) { a = a + b >= Z ? a + b - Z : a + b; } int main() { ios::sync_with_stdio(false); ll l, r; cin >> l >> r; int dp[LGN+1][2][2][2]; fill(&dp[0][0][0][0], &dp[LGN+1][0][0][0], 0); fill(&dp[0][0][0][0], &dp[1][0][0][0], 1); for (int i = 1; i <= LGN; i++) { bool bl = l >> (i - 1) & 1, br = r >> (i - 1) & 1; for (int fl = 0; fl <= 1; fl++) { for (int fr = 0; fr <= 1; fr++) { for (int sb = 0; sb <= 1; sb++) { if (!(bl && fl)) { addm(dp[i][fl][fr][sb], dp[i-1][fl][br?0:fr][sb]); } if (!sb && !(bl && fl) && !(!br && fr)) { addm(dp[i][fl][fr][sb], dp[i-1][fl][fr][sb]); } if (!(!br && fr)) { addm(dp[i][fl][fr][sb], dp[i-1][!bl?0:fl][fr][0]); } } } } } cout << dp[LGN][1][1][1] << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; using i64 = long long; #define rep(i,s,e) for(i64 (i) = (s);(i) < (e);(i)++) #define all(x) x.begin(),x.end() #define let auto const i64 dp[62][2][2][2]; i64 dp2[62][2][2][2]; int main() { i64 L, R; cin >> L >> R; L--; R++; i64 mod = 1e9 + 7; dp[61][0][0][0] = 1; for(i64 d = 60; d >= 0; d--) { for(i64 flag = 0;flag <= 1;flag++) { i64 A = ((R >> d) & 1); i64 B = ((L >> d) & 1); if(flag == 1) A = 1; for(i64 next = 0; next <= A; next++) { i64 nf = !!(next < A || flag); if(next == 0) { if(B == next) { dp[d][nf][0][0] += dp[d + 1][flag][0][0]; dp[d][nf][0][0] %= mod; dp[d][nf][1][0] += dp[d + 1][flag][1][0]; dp[d][nf][0][0] %= mod; } dp[d][nf][1][1] += dp[d + 1][flag][1][1]; dp[d][nf][1][1] %= mod; } else { if(B == next) { dp[d][nf][1][0] += dp[d + 1][flag][0][0] + dp[d + 1][flag][1][0]; dp[d][nf][1][0] %= mod; } if(B < next) { dp[d][nf][1][0] += dp[d + 1][flag][1][0]; dp[d][nf][1][0] %= mod; dp[d][nf][1][1] += dp[d + 1][flag][0][0] + dp[d + 1][flag][1][0]; dp[d][nf][1][1] %= mod; } dp[d][nf][1][1] += dp[d + 1][flag][1][1] * 2; dp[d][nf][1][1] %= mod; } } } } cout << dp[0][1][1][1] << endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
import java.util.Arrays; public class Main { private static void solve() { long l = nl(); long r = nl(); long x = f(r, r); long y = f(l - 1, r); int mod = (int) 1e9 + 7; System.out.println((x - y + mod) % mod); } private static long f(long a, long b) { int mod = (int) 1e9 + 7; // dp[n桁目][started][xがa上限][yがb上限] long[][][] dp = new long[65][2][2]; int as = 1; int bs = 1; for (int i = 63; i >= 0; i--) { int abit = (int) ((a >> i) & 1); int bbit = (int) ((b >> i) & 1); if ((abit == 1 || as == 0) && (bbit == 1 || bs == 0)) { dp[i][as][bs]++; } // a && b if (abit == 1 && bbit == 1) { dp[i][1][1] += dp[i + 1][1][1]; // (1, 1) dp[i][0][0] += dp[i + 1][1][1]; // (0, 0) dp[i][0][1] += dp[i + 1][1][1]; // (0, 1) } else if (abit == 0 && bbit == 1) { dp[i][1][1] += dp[i + 1][1][1]; // (0, 1) dp[i][1][0] += dp[i + 1][1][1]; // (0, 0) } else if (abit == 0 && bbit == 0) { dp[i][1][1] += dp[i + 1][1][1]; // (0, 0) } else { // abit == 1 && bbit == 0 dp[i][0][1] += dp[i + 1][1][1]; // (0, 0) } // !a && !b dp[i][0][0] += dp[i + 1][0][0] * 3; // (0, 0) (0, 1), (1, 1); // b only if (bbit == 1) { dp[i][0][1] += dp[i + 1][0][1] * 2; // (1, 1), (0, 1) dp[i][0][0] += dp[i + 1][0][1]; // (0, 0); } else { dp[i][0][1] += dp[i + 1][0][1]; // (0, 0) } // a only if (abit == 1) { dp[i][1][0] += dp[i + 1][1][0]; // (1, 1) dp[i][0][0] += dp[i + 1][1][0] * 2; // (0, 1), (0, 0); } else { dp[i][1][0] += dp[i + 1][1][0] * 2; // (0, 0), (0, 1); } if (abit == 1) { as = 0; } if (bbit == 1) { bs = 0; } for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { dp[i][j][k] %= mod; } } } long ret = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { ret += dp[0][i][j]; ret %= mod; } } return ret; } public static void main(String[] args) { new Thread(null, new Runnable() { @Override public void run() { long start = System.currentTimeMillis(); String debug = args.length > 0 ? args[0] : null; if (debug != null) { try { is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug)); } catch (Exception e) { throw new RuntimeException(e); } } reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768); solve(); out.flush(); tr((System.currentTimeMillis() - start) + "ms"); } }, "", 64000000).start(); } private static java.io.InputStream is = System.in; private static java.io.PrintWriter out = new java.io.PrintWriter(System.out); private static java.util.StringTokenizer tokenizer = null; private static java.io.BufferedReader reader; public static String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new java.util.StringTokenizer(reader.readLine()); } catch (Exception e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } private static double nd() { return Double.parseDouble(next()); } private static long nl() { return Long.parseLong(next()); } private static int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private static char[] ns() { return next().toCharArray(); } private static long[] nal(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nl(); return a; } private static int[][] ntable(int n, int m) { int[][] table = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[i][j] = ni(); } } return table; } private static int[][] nlist(int n, int m) { int[][] table = new int[m][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { table[j][i] = ni(); } } return table; } private static int ni() { return Integer.parseInt(next()); } private static void tr(Object... o) { if (is != System.in) System.out.println(java.util.Arrays.deepToString(o)); } }
JAVA
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
import java.util.*; public class Main { Scanner sc; long L, R; private static long MOD = 1000000007L; private static final long[] MASK; static { MASK = new long[64]; MASK[0] = 0L; for (int i = 1; i < 64; i++) MASK[i] = MASK[i-1] * 2 + 1; } private static final long[] POW3; static { POW3 = new long[64]; POW3[0] = 1L; for (int i = 1; i < 64; i++) POW3[i] = (POW3[i-1] * 3L) % MOD; } public Main() { sc = new Scanner(System.in); } private void calc() { L = sc.nextLong(); R = sc.nextLong(); System.out.println(count(0, 0, 63)); System.out.flush(); } private long count(long x, long y, int maskn) { if ( (x|y) != 0 && (y>>>1) >= x) return 0L; if (R < x || R < y) return 0L; if ((y | MASK[maskn]) < L || (x | MASK[maskn]) < L) return 0L; if (L <= x && (y | MASK[maskn]) <= R) return POW3[maskn]; maskn--; long nextBit = MASK[maskn]+1L; long su = count(x | nextBit, y | nextBit, maskn); long sl = count(x, y, maskn); if (x == y) return (su + sl + count(x, y | nextBit, maskn)) % MOD; else if ( L < x ) return (2 * su + sl) % MOD; else if ( (y | MASK[maskn+1]) < R) return (su + 2 * sl) % MOD; return (su + sl + count(x, y | nextBit, maskn)) % MOD; } public static void main(String[] args) { new Main().calc(); } }
JAVA
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws IOException { Main mainObj = new Main(); mainObj.solve(); } public void solve() throws IOException { FastScanner fs = new FastScanner(); long l = fs.nextLong(); long r = fs.nextLong(); int mod = 1_000_000_007; long[][][][] dp = new long[61][2][2][2]; dp[60][0][0][0] = 1; for(int i = 59; i >= 0; i--) { for(int j = 0; j <2; j++) { for(int k = 0; k < 2; k++) { for(int s = 0; s < 2; s++) { long pre = dp[i+1][j][k][s]; long lb = (l >> i) & 1; long rb = (r >> i) & 1; for(int x = 0; x < 2; x++) { for(int y = 0; y < 2; y++) { int nj = j; int nk = k; int ns = s; if(x == 1 && y == 0) { continue; } if(s == 0 && x == 0 && y == 1) { continue; } if(x == 1 && y == 1) { ns = 1; } if(j == 0 && lb == 1 && x == 0) { continue; } if(k == 0 && rb == 0 && y == 1) { continue; } if(lb == 0 && x == 1) { nj = 1; } if(rb == 1 && y == 0) { nk = 1; } dp[i][nj][nk][ns] = (dp[i][nj][nk][ns] + pre) % mod; } } } } } } long ans = 0; for(int j = 0; j < 2; j++) { for(int k = 0; k < 2; k++) { for(int s = 0; s < 2; s++) { ans = (ans + dp[0][j][k][s])%mod; } } } System.out.println(ans); } public class FastScanner { BufferedReader reader; private StringTokenizer st; public FastScanner() { st = null; reader = new BufferedReader(new InputStreamReader(System.in)); } public String next() throws IOException { if (st == null || !st.hasMoreElements()) { st = new StringTokenizer(reader.readLine()); } return st.nextToken(); } public String nextLine() throws IOException { st = null; String readLine = null; readLine = reader.readLine(); return readLine; } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } public int[] nextIntArr(int n) throws NumberFormatException, IOException { int[] retArr = new int[n]; for (int i = 0; i < n; i++) { retArr[i] = nextInt(); } return retArr; } public long[] nextLongArr(int n) throws NumberFormatException, IOException { long[] retArr = new long[n]; for (int i = 0; i < n; i++) { retArr[i] = nextLong(); } return retArr; } public void close() throws IOException { reader.close(); } } }
JAVA
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#pragma GCC optimize("Ofast") #include<bits/stdc++.h> #define int long long #define gmax(x,y) x=max(x,y) #define gmin(x,y) x=min(x,y) #define F first #define S second #define P pair #define FOR(i,a,b) for(int i=a;i<=b;i++) #define rep(i,a,b) for(int i=a;i<b;i++) #define V vector #define RE return #define ALL(a) a.begin(),a.end() #define MP make_pair #define PB emplace_back #define PF emplace_front #define FILL(a,b) memset(a,b,sizeof(a)) #define lwb lower_bound #define upb upper_bound using namespace std; int la,lb,a[100],b[100]; const int mod=1e9+7; int dp[100][2][2]; int dfs(int p,bool f1,bool f2){ if(p==0)RE 1; if(dp[p][f1][f2]!=-1)RE dp[p][f1][f2]; int l=f1?a[p]:0,r=f2?b[p]:1,re=0; FOR(i,l,r){ FOR(j,l,i)re=(re+dfs(p-1,f1&&j==l,f2&&i==r))%mod; } RE dp[p][f1][f2]=re; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int x,y; cin>>x>>y; while(x)a[++la]=x&1,x=x/2; while(y)b[++lb]=y&1,y=y/2; int ans=0; FILL(dp,-1); FOR(i,la,lb)ans=(ans+dfs(i-1,i==la,i==lb))%mod; cout<<ans<<'\n'; RE 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <iostream> #include <string> #include <stdio.h> #include <algorithm> #include <vector> #define llint long long #define mod 1000000007 using namespace std; llint L, R; llint dp[63][2][2][2]; int main(void) { cin >> L >> R; dp[62][0][0][0] = 1; for(int i = 62; i > 0; i--){ for(int j = 0; j < 2; j++){ for(int k = 0; k < 2; k++){ for(int l = 0; l < 2; l++){ //cout << i << " " << j << " " << k << " " << l << " " << dp[i][j][k][l] << endl; for(int u = 0; u < 2; u++){ for(int d = 0; d < 2; d++){ if(u == 0 && d == 1) continue; if(l == 0 && u == 1 && d == 0) continue; if(j == 0 && d == 0 && (L&(1LL<<(i-1)))) continue; if(k == 0 && u == 1 && (R&(1LL<<(i-1))) == 0) continue; int nj = j, nk = k, nl = l; if(d == 1 && (L&(1LL<<(i-1))) == 0) nj = 1; if(u == 0 && (R&(1LL<<(i-1)))) nk = 1; if(u == 1 && d == 1) nl = 1; (dp[i-1][nj][nk][nl] += dp[i][j][k][l]) %= mod; } } } } } } llint ans = 0; for(int j = 0; j < 2; j++){ for(int k = 0; k < 2; k++){ for(int l = 0; l < 2; l++){ ans += dp[0][j][k][l], ans %= mod; } } } cout << ans << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <iostream> #include <vector> #include <chrono> #include <random> #include <cstring> std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count()); const int ms = 64; int l[ms], r[ms]; void read(int a[ms]) { long long x; std::cin >> x; for(int i = ms-1; i >= 0; x /= 2, i--) { a[i] = (int)(x % 2); } } const int MOD = 1e9 + 7; int memo[ms][2][2][2][2][2]; int dp(int on, int L1, int R1, int L2, int R2, int got) { if(on == ms) return 1; int &ans = memo[on][L1][R1][L2][R2][got]; if(ans != -1) { return ans; } ans = 0; for(int i = 0; i < 2; i++) { for(int j = 0; j < 2; j++) { bool can = i >= j; if(got == 0) { can = can && i == j; } if(i == 0) can = can && (!L1 || l[on] == 0); else can = can && (!R1 || r[on] == 1); if(j == 0) can = can && (!L2 || l[on] == 0); else can = can && (!R2 || r[on] == 1); if(can) ans = (ans + dp(on+1, L1 && l[on] == i, R1 && r[on] == i, L2 && l[on] == j, R2 && r[on] == j, got || i)) % MOD; } } return ans; } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); read(l); read(r); memset(memo, -1, sizeof memo); std::cout << dp(0, 1, 1, 1, 1, 0) << '\n'; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.charset.Charset; import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { boolean local = false; boolean async = false; Charset charset = Charset.forName("ascii"); FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset); Task task = new Task(io, new Debug(local)); if (async) { Thread t = new Thread(null, task, "dalt", 1 << 27); t.setPriority(Thread.MAX_PRIORITY); t.start(); t.join(); } else { task.run(); } if (local) { io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M"); } io.flush(); } public static class Task implements Runnable { final FastIO io; final Debug debug; int inf = (int) 1e8; Modular mod = new Modular((int) 1e9 + 7); Log2 log2 = new Log2(); Power power = new Power(mod); public Task(FastIO io, Debug debug) { this.io = io; this.debug = debug; } @Override public void run() { solve(); // for (int i = 1; i < 100; i++) { // for (int j = 1; j <= i; j++) { // debug.debug("l", j); // debug.debug("r", i); // debug.assertTrue(bruteForce(j, i) == solve(j, i)); // } // } } int[][][][] dp; boolean[] lBits; boolean[] rBits; public int bruteForce(long l, long r) { int sum = 0; for (long i = l; i <= r; i++) { for (long j = l; j <= i; j++) { if (Long.highestOneBit(i) == Long.highestOneBit(j) && (i & j) == j) { sum++; } } } return sum; } public void solve() { long l = io.readLong(); long r = io.readLong(); io.cache.append(solve(l, r)); } public int solve(long l, long r) { lBits = new boolean[64]; rBits = new boolean[64]; toBits(lBits, 0, l); toBits(rBits, 0, r); reverse(lBits); reverse(rBits); //1st chosen, up bound, low bound int[][][][] dp = new int[2][2][2][64]; dp[0][1][1][0] = 1; for (int i = 1; i < 64; i++) { //000 dp[1][0][0][i] = mod.plus(dp[1][0][0][i], dp[0][0][0][i - 1]); dp[0][0][0][i] = mod.plus(dp[0][0][0][i], dp[0][0][0][i - 1]); //001 if (lBits[i]) { dp[1][0][1][i] = mod.plus(dp[1][0][1][i], dp[0][0][1][i - 1]); } else { dp[1][0][0][i] = mod.plus(dp[1][0][0][i], dp[0][0][1][i - 1]); dp[0][0][1][i] = mod.plus(dp[0][0][1][i], dp[0][0][1][i - 1]); } //010 //011 if (rBits[i] && lBits[i]) { dp[1][1][1][i] = mod.plus(dp[1][1][1][i], dp[0][1][1][i - 1]); } else if (rBits[i]) { dp[1][1][0][i] = mod.plus(dp[1][1][0][i], dp[0][1][1][i - 1]); dp[0][0][1][i] = mod.plus(dp[0][0][1][i], dp[0][1][1][i - 1]); } else if (lBits[i]) { } else { dp[0][1][1][i] = mod.plus(dp[0][1][1][i], dp[0][1][1][i - 1]); } //100 dp[1][0][0][i] = mod.plus(dp[1][0][0][i], mod.mul(3, dp[1][0][0][i - 1])); //101 if (lBits[i]) { dp[1][0][1][i] = mod.plus(dp[1][0][1][i], dp[1][0][1][i - 1]); } else { dp[1][0][1][i] = mod.plus(dp[1][0][1][i], mod.mul(2, dp[1][0][1][i - 1])); dp[1][0][0][i] = mod.plus(dp[1][0][0][i], dp[1][0][1][i - 1]); } //110 if (rBits[i]) { dp[1][1][0][i] = mod.plus(dp[1][1][0][i], mod.mul(2, dp[1][1][0][i - 1])); dp[1][0][0][i] = mod.plus(dp[1][0][0][i], dp[1][1][0][i - 1]); } else { dp[1][1][0][i] = mod.plus(dp[1][1][0][i], dp[1][1][0][i - 1]); } //111 if (lBits[i] && rBits[i]) { dp[1][1][1][i] = mod.plus(dp[1][1][1][i], dp[1][1][1][i - 1]); } else if (rBits[i]) { dp[1][1][0][i] = mod.plus(dp[1][1][0][i], dp[1][1][1][i - 1]); dp[1][1][1][i] = mod.plus(dp[1][1][1][i], dp[1][1][1][i - 1]); dp[1][0][1][i] = mod.plus(dp[1][0][1][i], dp[1][1][1][i - 1]); } else if (lBits[i]) { } else { dp[1][1][1][i] = mod.plus(dp[1][1][1][i], dp[1][1][1][i - 1]); } } int sum = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { sum = mod.plus(dp[1][i][j][63], sum); } } return sum; } public int valueOf(boolean x) { return x ? 1 : 0; } public void reverse(boolean[] data) { int l = 0; int r = data.length - 1; while (l < r) { boolean tmp = data[l]; data[l] = data[r]; data[r] = tmp; l++; r--; } } public void toBits(boolean[] bits, int i, long x) { if (x == 0) { return; } bits[i] = (x & 1) == 1; toBits(bits, i + 1, x >>> 1); } } /** * Mod operations */ public static class Modular { int m; public Modular(int m) { this.m = m; } public int valueOf(int x) { x %= m; if (x < 0) { x += m; } return x; } public int valueOf(long x) { x %= m; if (x < 0) { x += m; } return (int) x; } public int mul(int x, int y) { return valueOf((long) x * y); } public int mul(long x, long y) { x = valueOf(x); y = valueOf(y); return valueOf(x * y); } public int plus(int x, int y) { return valueOf(x + y); } public int plus(long x, long y) { x = valueOf(x); y = valueOf(y); return valueOf(x + y); } @Override public String toString() { return "mod " + m; } } /** * Bit operations */ public static class BitOperator { public int bitAt(int x, int i) { return (x >> i) & 1; } public int bitAt(long x, int i) { return (int) ((x >> i) & 1); } public int setBit(int x, int i, boolean v) { if (v) { x |= 1 << i; } else { x &= ~(1 << i); } return x; } public long setBit(long x, int i, boolean v) { if (v) { x |= 1L << i; } else { x &= ~(1L << i); } return x; } /** * Determine whether x is subset of y */ public boolean subset(long x, long y) { return intersect(x, y) == x; } /** * Merge two set */ public long merge(long x, long y) { return x | y; } public long intersect(long x, long y) { return x & y; } public long differ(long x, long y) { return x - intersect(x, y); } } /** * Power operations */ public static class Power { final Modular modular; public Power(Modular modular) { this.modular = modular; } public int pow(int x, long n) { if (n == 0) { return 1; } long r = pow(x, n >> 1); r = modular.valueOf(r * r); if ((n & 1) == 1) { r = modular.valueOf(r * x); } return (int) r; } public int inverse(int x) { return pow(x, modular.m - 2); } public int pow2(int x) { return x * x; } public long pow2(long x) { return x * x; } public double pow2(double x) { return x * x; } } /** * Log operations */ public static class Log2 { public int ceilLog(int x) { return 32 - Integer.numberOfLeadingZeros(x - 1); } public int floorLog(int x) { return 31 - Integer.numberOfLeadingZeros(x); } public int ceilLog(long x) { return 64 - Long.numberOfLeadingZeros(x - 1); } public int floorLog(long x) { return 63 - Long.numberOfLeadingZeros(x); } } public static class FastIO { public final StringBuilder cache = new StringBuilder(1 << 13); private final InputStream is; private final OutputStream os; private final Charset charset; private StringBuilder defaultStringBuf = new StringBuilder(1 << 13); private byte[] buf = new byte[1 << 13]; private int bufLen; private int bufOffset; private int next; public FastIO(InputStream is, OutputStream os, Charset charset) { this.is = is; this.os = os; this.charset = charset; } public FastIO(InputStream is, OutputStream os) { this(is, os, Charset.forName("ascii")); } private int read() { while (bufLen == bufOffset) { bufOffset = 0; try { bufLen = is.read(buf); } catch (IOException e) { throw new RuntimeException(e); } if (bufLen == -1) { return -1; } } return buf[bufOffset++]; } public void skipBlank() { while (next >= 0 && next <= 32) { next = read(); } } public int readInt() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } int val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public long readLong() { int sign = 1; skipBlank(); if (next == '+' || next == '-') { sign = next == '+' ? 1 : -1; next = read(); } long val = 0; if (sign == 1) { while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } } else { while (next >= '0' && next <= '9') { val = val * 10 - next + '0'; next = read(); } } return val; } public double readDouble() { boolean sign = true; skipBlank(); if (next == '+' || next == '-') { sign = next == '+'; next = read(); } long val = 0; while (next >= '0' && next <= '9') { val = val * 10 + next - '0'; next = read(); } if (next != '.') { return sign ? val : -val; } next = read(); long radix = 1; long point = 0; while (next >= '0' && next <= '9') { point = point * 10 + next - '0'; radix = radix * 10; next = read(); } double result = val + (double) point / radix; return sign ? result : -result; } public String readString(StringBuilder builder) { skipBlank(); while (next > 32) { builder.append((char) next); next = read(); } return builder.toString(); } public String readString() { defaultStringBuf.setLength(0); return readString(defaultStringBuf); } public int readLine(char[] data, int offset) { int originalOffset = offset; while (next != -1 && next != '\n') { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(char[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (char) next; next = read(); } return offset - originalOffset; } public int readString(byte[] data, int offset) { skipBlank(); int originalOffset = offset; while (next > 32) { data[offset++] = (byte) next; next = read(); } return offset - originalOffset; } public char readChar() { skipBlank(); char c = (char) next; next = read(); return c; } public void flush() throws IOException { os.write(cache.toString().getBytes(charset)); os.flush(); cache.setLength(0); } public boolean hasMore() { skipBlank(); return next != -1; } } public static class Debug { private boolean allowDebug; public Debug(boolean allowDebug) { this.allowDebug = allowDebug; } public void assertTrue(boolean flag) { if (!allowDebug) { return; } if (!flag) { fail(); } } public void fail() { throw new RuntimeException(); } public void assertFalse(boolean flag) { if (!allowDebug) { return; } if (flag) { fail(); } } private void outputName(String name) { System.out.print(name + " = "); } public void debug(String name, int x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, long x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, double x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, int[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, long[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, double[] x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.toString(x)); } public void debug(String name, Object x) { if (!allowDebug) { return; } outputName(name); System.out.println("" + x); } public void debug(String name, Object... x) { if (!allowDebug) { return; } outputName(name); System.out.println(Arrays.deepToString(x)); } } }
JAVA
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
L,R = map(int,input().split()) mod = 10**9+7 m = 64 +1 fac = [1]*m ninv = [1]*m finv = [1]*m for i in range(2,m): fac[i] = fac[i-1]*i%mod ninv[i] = (-(mod//i)*ninv[mod%i])%mod finv[i] = finv[i-1]*ninv[i]%mod def comb(n,k): return (fac[n]*finv[k]%mod)*finv[n-k]%mod def f(L,R): if L>R : return 0 R = bin(R)[2:] N = len(R) ret = f(L,int("0"+"1"*(N-1),2)) L = bin(L)[2:] if len(L) != N : L = "1"+"0"*(N-1) for i in range(N): if R[i] == "0" : continue R2 = R[:i] + "0" + "?"*(N-i-1) if i==0: R2 = R for j in range(N): if L[j] == "1" and j!=0 : continue L2 = L[:j] + "1" + "?"*(N-j-1) if j==0 : L2 = L if L2[0] == "0" : break tmp = 1 for r,l in zip(R2[1:],L2[1:]): if r=="0" and l=="1" : tmp *= 0 if r=="?" and l=="?" : tmp *= 3 if r=="?" and l=="0" : tmp *= 2 if r=="1" and l=="?" : tmp *= 2 ret += tmp ret %= mod return ret%mod print(f(L,R))
PYTHON3
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; i++) #define rep2(i, x, n) for(int i = x; i <= n; i++) #define rep3(i, x, n) for(int i = x; i >= n; i--) #define elif else if typedef long long ll; typedef pair<ll, ll> P; const ll MOD = 1e9+7; const ll MOD2 = 998244353; const ll INF = 1e18; int main(){ ll L, R; cin >> L >> R; int l[61], r[61]; rep(i, 61){ l[i] = L%2, r[i] = R%2; L /= 2, R /= 2; } ll dp[61][2][2][2]; rep(i, 61)rep(j, 2)rep(k, 2)rep(m, 2) dp[i][j][k][m] = 0; dp[60][0][0][0] = 1; rep3(i, 59, 0){ rep(j, 2)rep(k, 2)rep(m, 2){ ll pre = dp[i+1][j][k][m]; rep(x, 2)rep(y, 2){ if(x == 1 && y == 0) continue; int nj = j, nk = k, nm = m; if(x == 0 && l[i] == 1 && j == 0) continue; if(x == 1 && l[i] == 0) nj = 1; if(y == 1 && r[i] == 0 && k == 0) continue; if(y == 0 && r[i] == 1) nk = 1; if(x == 0 && y == 1 && m == 0) continue; if(x == 1 && y == 1) nm = 1; dp[i][nj][nk][nm] += pre; dp[i][nj][nk][nm] %= MOD; } } } ll ans = 0; rep(j, 2)rep(k, 2)rep(m, 2){ ans += dp[0][j][k][m]; ans %= MOD; } cout << ans << endl; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <iostream> using namespace std; typedef long long ll; ll mod = 1e9 + 7; ll dp[61][2][2][2]; int main(int argc, char *argv[]) { ll L, R; cin >> L >> R; dp[60][0][0][0] = 1; for (int i = 59; i >= 0; i--) { int lb = L >> i & 1; int rb = R >> i & 1; for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { for (int s = 0; s < 2; s++) { ll pre = dp[i + 1][j][k][s] % mod; for (int x = 0; x < 2; x++) { for (int y = 0; y < 2; y++) { if (x && !y) { continue; } int nj = j, nk = k, ns = s; if (!s && x != y) { continue; } if (x && y) { ns = 1; } if (!j && !x && lb) { continue; } if (x && !lb) { nj = 1; } if (!k && y && !rb) { continue; } if (!y && rb) { nk = 1; } dp[i][nj][nk][ns] += pre % mod; dp[i][nj][nk][ns] %= mod; } } } } } } ll ans = 0; for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { for (int s = 0; s < 2; s++) { ans += dp[0][j][k][s] % mod; ans %= mod; } } } cout << ans << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) #define fi first #define se second template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; } template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = 0; //---------------------------------// ll dp[61][2][2][2]; ll L, R; ll dfs(int pos, bool ismax, bool ismin, bool zero) { if (pos == -1) return 1; ll &res = dp[pos][ismax][ismin][zero]; if (~res) return res; res = 0; if (!ismax || ismax && R >> pos & 1) res += dfs(pos - 1, ismax, ismin & (L >> pos & 1), false); if (!ismin || ismin && L >> pos & 1 ^ 1) { if (!zero && (!ismax || ismax && R >> pos & 1)) res += dfs(pos - 1, ismax, ismin, zero); res += dfs(pos - 1, ismax & (R >> pos & 1 ^ 1), ismin, zero); } res %= MOD; return res; } int main() { cin >> L >> R; memset(dp, -1, sizeof(dp)); cout << dfs(60, true, true, true) << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> #define all(A) begin(A), end(A) #define rall(A) rbegin(A), rend(A) #define sz(A) int(A.size()) #define pb push_back using namespace std; typedef long long ll; typedef pair <int, int> pii; int main () { ios::sync_with_stdio(false); cin.tie(0); ll l, r; cin >> l >> r; const int B = 62; const int MOD = 1e9 + 7; int dp[B][2][2][2]; memset(dp, -1, sizeof dp); function <int(int,int,int,int)> rec = [&] (int pos, int gt_x, int lt_y, int msb) { if (pos == -1) return 1; int& ret = dp[pos][gt_x][lt_y][msb]; if (ret != -1) return ret; ret = 0; int bit_l = (l >> pos) & 1; int bit_r = (r >> pos) & 1; // x = 0, y = 0 if (gt_x or (bit_l == 0)) { ret += rec(pos - 1, gt_x, lt_y or (bit_r == 1), msb); if (ret > MOD) ret -= MOD; } // x = 0, y = 1 if ((gt_x or (bit_l == 0)) and (lt_y or (bit_r == 1)) and msb) { ret += rec(pos - 1, gt_x, lt_y, msb); if (ret > MOD) ret -= MOD; } // x = 1, y = 1 if (lt_y or (bit_r == 1)) { ret += rec(pos - 1, gt_x or (bit_l == 0), lt_y, 1); if (ret > MOD) ret -= MOD; } return ret; }; cout << rec(B - 1, 0, 0, 0) << '\n'; return (0); }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
/* [abc138] F - Coincidence */ #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<int, int> pii; typedef pair<ll, int> pli; typedef pair<ll, ll> pll; #define ALL(c) (c).begin(), (c).end() const int MAX_D = 64; const ll MOD = 1e9 + 7; ll L, R; ll memo[MAX_D][2][2][2]; ll f(int d, int m, int l, int r) { if (d == -1) { return m == 1; } if (memo[d][m][l][r] >= 0) { return memo[d][m][l][r]; } ll ret = 0; for (int a = 0; a < 2; a++) { for (int b = 0; b < 2; b++) { if ((l && a < ((L >> d) & 1)) || (r && b > ((R >> d) & 1)) || (a > b) || (!m && a != b)) { continue; } int nm = m || (a & b); int nl = l && (((L >> d) & 1) == a); int nr = r && (((R >> d) & 1) == b); (ret += f(d - 1, nm, nl, nr)) %= MOD; } } return memo[d][m][l][r] = ret; } ll solve() { memset(memo, -1, sizeof(memo)); return f(MAX_D - 1, 0, 1, 1); } int main() { cin >> L >> R; cout << solve() << endl; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bitset> #include <iostream> using namespace std; using lint = long long; const int MOD = 1000000007; int getCountOfPair(bitset<64> l, bitset<64> r) { bool flag = false, diff = false, both = false; int i = 63; lint count[3] = {l != r, 0, 1}; do { flag |= l[i]; diff |= l[i] != r[i]; } while (!r[i--]); for (; i >= 0; i--) { count[1] = (count[1] * 3 + (count[0] * !l[i] + count[2] * r[i]) * diff) % MOD; count[0] = ((count[0] << (!l[i] * flag * diff)) + both * !l[i] * r[i]) % MOD; count[2] = ((count[2] << (r[i] * diff)) + both * !l[i] * r[i]) % MOD; flag |= l[i]; bool check = diff; diff |= l[i] != r[i]; check ^= diff; both = (both * (!l[i] | r[i])) | check; } return int((count[0] + count[1] + count[2] + both) % MOD); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int answer; lint l, r; cin >> l >> r; answer = getCountOfPair(l, r); cout << answer; return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <cstdio> long L, R, MOD=1e9+7; long dp[66][2][2][2]; int main() { scanf("%ld%ld", &L, &R); dp[0][0][0][0] = 1; bool x[3] = {1, 0, 0}, y[3] = {1, 1, 0}; for(int i = 0; i < 60; ++i) { bool lb = L>>(59-i)&1, rb = R>>(59-i)&1; for(int l = 0; l < 2; ++l) for(int r = 0; r < 2; ++r) for(int j = 0; j < 2; ++j) for(int k = 0; k < 3; ++k) { if(!l && !x[k] && lb) continue; if(!r && y[k] && !rb) continue; if(!j && x[k] ^ y[k]) continue; int nl = (x[k] ^ lb ? 1 : l); int nr = (y[k] ^ rb ? 1 : r); int nj = (x[k] & y[k] ? 1 : j); (dp[i+1][nl][nr][nj] += dp[i][l][r][j]) %= MOD; } } long ans = dp[60][0][0][1] + dp[60][0][1][1] + dp[60][1][0][1] + dp[60][1][1][1]; printf("%ld\n", ans % MOD); return 0; }
CPP
p02938 AtCoder Beginner Contest 138 - Coincidence
Given are integers L and R. Find the number, modulo 10^9 + 7, of pairs of integers (x, y) (L \leq x \leq y \leq R) such that the remainder when y is divided by x is equal to y \mbox{ XOR } x. What is \mbox{ XOR }? The XOR of integers A and B, A \mbox{ XOR } B, is defined as follows: * When A \mbox{ XOR } B is written in base two, the digit in the 2^k's place (k \geq 0) is 1 if either A or B, but not both, has 1 in the 2^k's place, and 0 otherwise. For example, 3 \mbox{ XOR } 5 = 6. (In base two: 011 \mbox{ XOR } 101 = 110.) Constraints * 1 \leq L \leq R \leq 10^{18} Input Input is given from Standard Input in the following format: L R Output Print the number of pairs of integers (x, y) (L \leq x \leq y \leq R) satisfying the condition, modulo 10^9 + 7. Examples Input 2 3 Output 3 Input 10 100 Output 604 Input 1 1000000000000000000 Output 68038601
6
0
#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) #define fi first #define se second template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; } template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; } using ll = long long; using pii = pair<int, int>; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = 0; //---------------------------------// ll dp[61][2][2][2][2]; ll L, R; ll dfs(int pos, bool ismax, bool ismin, bool ismaxy, bool zero) { if (pos == -1) return 1; ll &res = dp[pos][ismax][ismin][ismaxy][zero]; if (~res) return res; res = 0; if ((!ismax || ismax && R >> pos & 1) && (!ismaxy || ismaxy && R >> pos & 1)) res += dfs(pos - 1, ismax, ismin & (L >> pos & 1), ismaxy, false); if (!ismin || ismin && L >> pos & 1 ^ 1) { if (!zero && (!ismaxy || ismaxy && R >> pos & 1)) res += dfs(pos - 1, ismax & (R >> pos & 1 ^ 1), ismin, ismaxy, zero); res += dfs(pos - 1, ismax & (R >> pos & 1 ^ 1), ismin, ismaxy & (R >> pos & 1 ^ 1), zero); } res %= MOD; return res; } int main() { cin >> L >> R; memset(dp, -1, sizeof(dp)); cout << dfs(60, true, true, true, true) << endl; return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] ant = new int[5]; int dis = 0; for(int i = 0; i < 5; i++) { ant[i] = sc.nextInt(); } dis = sc.nextInt(); for(int i = 0; i < 4; i++) { for(int j = i + 1; j < 5; j++) { if(ant[j] - ant[i] > dis) { System.out.println(":("); System.exit(0); } } } System.out.println("Yay!"); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ Scanner sc =new Scanner(System.in); int a =sc.nextInt(); int b =sc.nextInt(); int c =sc.nextInt(); int d =sc.nextInt(); int e =sc.nextInt(); int k =sc.nextInt(); if(e-a>k) { System.out.println(":("); } else { System.out.println("Yay!"); } } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] a = new int[5]; for(int i=0;i<5;i++){ a[i]=sc.nextInt(); } int k = sc.nextInt(); int b = a[4]-a[0]; if(b>k){ System.out.println(":("); }else{ System.out.println("Yay!"); } } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include<bits/stdc++.h> using namespace std; int main() { int a[5],k; for(int i=0;i<5;i++) cin>>a[i]; cin>>k; int x=a[4]-a[0]; if(x<=k) cout<<"Yay!"; else cout<<":("; return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <stdio.h> int main() { int a, b, c, d, e, k; scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &k); if (e - a > k) printf(":(\n"); else printf("Yay!\n"); return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int [] a = new int[4]; for(int i =0; i<4; i++) { a[i] = sc.nextInt();} int e = sc.nextInt(); int k = sc.nextInt(); sc.close(); for(int i =0; i<4; i++) {if(e-a[i]>k) {System.out.println(":("); System.exit(0);} } System.out.println("Yay!"); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); int d = sc.nextInt(); int e = sc.nextInt(); int k = sc.nextInt(); if (e - a <= k) { System.out.println("Yay!"); } else { System.out.println(":("); } } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); for (int i = 0; i < 3; i++) { sc.next(); } int e = sc.nextInt(); int k = sc.nextInt(); if (k >= e - a) { System.out.println("Yay!"); } else { System.out.println(":("); } } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <bits/stdc++.h> using namespace std; int main() { int a,b,c,d,e,K; cin >> a >> b >> c >> d >> e >> K; if (e-a<=K) cout << "Yay!" << endl; else cout << ":(" << endl; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
a,*_,e,k=[int(input())for x in' '*6];print([':(','Yay!'][e-a<=k])
PYTHON3
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
A = [int(input()) for _ in range(6)] ans = ":(" if A[4] - A[0] > A[5] else "Yay!" print(ans)
PYTHON3
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int b = scanner.nextInt(); int c = scanner.nextInt(); int d = scanner.nextInt(); int e = scanner.nextInt(); int k = scanner.nextInt(); System.out.println(e - a > k ? ":(" : "Yay!"); scanner.close(); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include<iostream> #include<string> #include<cmath> #include<bits/stdc++.h> using namespace std; int main() { int a,b,c,d,e,k; cin>>a>>b>>c>>d>>e>>k; cout<<(e-a <= k ?"Yay!" : ":(" ); }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.Scanner; public class Main { public static void main(String args[]) { Scanner in = new Scanner(System.in); int min = 124; int max = -1; for (int i = 0; i < 5; i++) { int n = in.nextInt(); if (n < min) min = n; if(n > max) max = n; } int k = in.nextInt(); System.out.println((max - min <= k) ? "Yay!" : ":("); in.close(); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include<iostream> using namespace std; int main(void){ int a,b,c,d,e,k; cin>>a>>b>>c>>d>>e>>k; if(e-a<=k){ cout<<"Yay!"<<endl; }else{ cout<<":("<<endl; } return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] a = new int[5]; int[] b = new int[5]; for(int i = 0; i<5; i++) { a[i] = sc.nextInt(); b[i] = a[i]; } int k = sc.nextInt(); for(int i = 0; i<5; i++) { for(int j = 0; j<5; j++) { if(Math.abs(a[i]-b[j]) > k) { System.out.println(":("); return; } } } System.out.println("Yay!"); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <iostream> using namespace std; int main(void){ int a,b,c,d,e,k; cin >> a >> b >> c >> d >> e >> k; if(e-a<=k) cout << "Yay!"; else cout << ":("; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <bits/stdc++.h> using namespace std; int main(){ int a,s,d,f,g,h; cin>>a>>s>>d>>f>>g>>h; if(g-a>h){ cout<<":("; }else{ cout<<"Yay!"; } }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.*; class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a[] = new int[5]; for (int i = 0; i < 5; i++) { a[i] = sc.nextInt(); } int k = sc.nextInt(); for (int i = 0; i < 4; i++) { for (int j = i + 1; j < 5; j++) { if (a[j] - a[i] > k) { System.out.println(":("); return; } } } System.out.println("Yay!"); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, k; cin >> a >> b >> c >> d >> e >> k; cout << ((e - a <= k) ? "Yay!" : ":(") << endl; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <cstdio> #include <iostream> using namespace std; int main() { int a, e, k; scanf("%d%*d%*d%*d%d%d", &a, &e, &k); if (e - a > k) cout << ":("; else cout << "Yay!"; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
a = [int(input()) for i in range(6)] if a[4] - a[0] > a[5]: print(':(') else: print('Yay!')
PYTHON3
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <iostream> using namespace std; int main(int argc, char** argv) { int a,b,c,d,e,k; cin >> a >> b >> c >> d >> e >> k; if(e-a<=k) puts("Yay!"); else puts(":("); return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int[] x = new int[5]; for(int i=0; i<5; i++){ x[i] = sc.nextInt(); } int k = sc.nextInt(); if(x[4]-x[0]>k){ System.out.println(":("); } else if(x[4]-x[0]<=k){ System.out.println("Yay!"); } } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include<iostream> using namespace std; int main(void) { int a,b,c,d,e,k; cin >> a >> b >> c >> d >> e >> k; if(e-a>k) cout << ":("; else cout << "Yay!"; return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include<iostream> int main(){ int a, b, c, d, e, k; std::cin>>a>>b>>c>>d>>e>>k; std::cout << ((e-a<=k) ? "Yay!" : ":(") << std::endl; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <iostream> using namespace std; int main(){ int A, B, C, D, E, k; cin >> A >> B >> C >> D >> E >> k; cout << (E-A <= k ? "Yay!" : ":("); return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include<bits/stdc++.h> using namespace std; int a,b,c,d,e,k; int main() { scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&k); if(e-a>k) puts(":("); else puts("Yay!"); return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
l=[int(input()) for _ in range(6)] print(':(' if l[4]-l[0]>l[5] else 'Yay!')
PYTHON3
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <iostream> using namespace std; int main() { int a, b, c, d, e, k; cin >> a >> b >> c >> d >> e >> k; cout << (e - a > k ? ":(" : "Yay!") << endl; return 0; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
#include <iostream> using namespace std; int main(){ int a,b,c,d,e,k; cin >> a >> b >> c >> d >> e >> k; if(e-a > k) cout << ":(" << endl; else cout << "Yay!" << endl; }
CPP
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] arr = new int[5]; for (int i = 0; i < 5; i++) { arr[i] = sc.nextInt(); } int k = sc.nextInt(); System.out.println(arr[4] - arr[0] > k ? ":(" : "Yay!"); } }
JAVA
p03075 AtCoder Beginner Contest 123 - Five Antennas
In AtCoder city, there are five antennas standing in a straight line. They are called Antenna A, B, C, D and E from west to east, and their coordinates are a, b, c, d and e, respectively. Two antennas can communicate directly if the distance between them is k or less, and they cannot if the distance is greater than k. Determine if there exists a pair of antennas that cannot communicate directly. Here, assume that the distance between two antennas at coordinates p and q (p < q) is q - p. Constraints * a, b, c, d, e and k are integers between 0 and 123 (inclusive). * a < b < c < d < e Input Input is given from Standard Input in the following format: a b c d e k Output Print `:(` if there exists a pair of antennas that cannot communicate directly, and print `Yay!` if there is no such pair. Examples Input 1 2 4 8 9 15 Output Yay! Input 15 18 26 35 36 18 Output :(
6
0
a,b,c,d,e,f=[int(input()) for i in range(6)] print("Yay!" if abs(a-e)<=f else ":(")
PYTHON3