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
p02802 AtCoder Beginner Contest 151 - Welcome to AtCoder
Takahashi participated in a contest on AtCoder. The contest had N problems. Takahashi made M submissions during the contest. The i-th submission was made for the p_i-th problem and received the verdict S_i (`AC` or `WA`). The number of Takahashi's correct answers is the number of problems on which he received an `AC` once or more. The number of Takahashi's penalties is the sum of the following count for the problems on which he received an `AC` once or more: the number of `WA`s received before receiving an `AC` for the first time on that problem. Find the numbers of Takahashi's correct answers and penalties. Constraints * N, M, and p_i are integers. * 1 \leq N \leq 10^5 * 0 \leq M \leq 10^5 * 1 \leq p_i \leq N * S_i is `AC` or `WA`. Input Input is given from Standard Input in the following format: N M p_1 S_1 : p_M S_M Output Print the number of Takahashi's correct answers and the number of Takahashi's penalties. Examples Input 2 5 1 WA 1 AC 2 WA 2 AC 2 WA Output 2 2 Input 100000 3 7777 AC 7777 AC 7777 AC Output 1 0 Input 6 0 Output 0 0
6
0
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); sc.nextLine(); Set<Integer> acList = new HashSet<>(); Map<Integer, Integer> waMap = new HashMap<>(); for (int i = 0; i < m; i++){ int q = sc.nextInt(); String r = sc.next(); if(!acList.contains(q)) { if (r.equals("AC")) { acList.add(q); } else { waMap.put(q, waMap.getOrDefault(q, 0) + 1); } } sc.nextLine(); } System.out.println(acList.size() + " " + acList.stream().mapToInt(q -> waMap.getOrDefault(q, 0)).sum()); } }
JAVA
p02802 AtCoder Beginner Contest 151 - Welcome to AtCoder
Takahashi participated in a contest on AtCoder. The contest had N problems. Takahashi made M submissions during the contest. The i-th submission was made for the p_i-th problem and received the verdict S_i (`AC` or `WA`). The number of Takahashi's correct answers is the number of problems on which he received an `AC` once or more. The number of Takahashi's penalties is the sum of the following count for the problems on which he received an `AC` once or more: the number of `WA`s received before receiving an `AC` for the first time on that problem. Find the numbers of Takahashi's correct answers and penalties. Constraints * N, M, and p_i are integers. * 1 \leq N \leq 10^5 * 0 \leq M \leq 10^5 * 1 \leq p_i \leq N * S_i is `AC` or `WA`. Input Input is given from Standard Input in the following format: N M p_1 S_1 : p_M S_M Output Print the number of Takahashi's correct answers and the number of Takahashi's penalties. Examples Input 2 5 1 WA 1 AC 2 WA 2 AC 2 WA Output 2 2 Input 100000 3 7777 AC 7777 AC 7777 AC Output 1 0 Input 6 0 Output 0 0
6
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int n, m, ans, pan; int vis[maxn], cnt[maxn]; int main() { scanf("%d%d", &n, &m); while(m--) { int p; char s[10]; scanf("%d%s", &p, s); if(s[0] == 'A') { if(!vis[p]) { vis[p] = 1; ++ans; pan += cnt[p]; } } if(s[0] == 'W') { ++cnt[p]; } } printf("%d %d\n", ans, pan); return 0; }
CPP
p02802 AtCoder Beginner Contest 151 - Welcome to AtCoder
Takahashi participated in a contest on AtCoder. The contest had N problems. Takahashi made M submissions during the contest. The i-th submission was made for the p_i-th problem and received the verdict S_i (`AC` or `WA`). The number of Takahashi's correct answers is the number of problems on which he received an `AC` once or more. The number of Takahashi's penalties is the sum of the following count for the problems on which he received an `AC` once or more: the number of `WA`s received before receiving an `AC` for the first time on that problem. Find the numbers of Takahashi's correct answers and penalties. Constraints * N, M, and p_i are integers. * 1 \leq N \leq 10^5 * 0 \leq M \leq 10^5 * 1 \leq p_i \leq N * S_i is `AC` or `WA`. Input Input is given from Standard Input in the following format: N M p_1 S_1 : p_M S_M Output Print the number of Takahashi's correct answers and the number of Takahashi's penalties. Examples Input 2 5 1 WA 1 AC 2 WA 2 AC 2 WA Output 2 2 Input 100000 3 7777 AC 7777 AC 7777 AC Output 1 0 Input 6 0 Output 0 0
6
0
#include<bits/stdc++.h> using namespace std; long long a,b,c,d,e,n,m,pas1,pas2,g[100009]; bool bo[100009]; string s; int main(){ ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0); cin>>n>>m; for(b=1; b<=m; b++){ cin>>c>>s; if(s=="AC"){ if(bo[c]==0){ bo[c]=1; pas1++; pas2+=g[c]; } }else{ g[c]++; } } cout<<pas1<<" "<<pas2; 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 ll long long using namespace std; ll f[64][2][2][2][2];ll l,r;ll ans1=0;int l1[64],r1[64];ll mod=1e9+7; ll dfs(int pos,int tl,int tr,int ts,int th){ if(pos<0) return 1;ll &ans=f[pos][tl][tr][ts][th]; if(ans) return ans; for(int i=0;i<=1;i++){ for(int j=0;j<=1;j++){int fs=ts,fl=tl,fr=tr,fh=th; if(!th&&i)if(i!=j)continue; if((j&i)!=j) continue; if(!tl&&l1[pos]>j) continue; if(!tr&&r1[pos]<i) continue; if(!ts&&j>i) continue; if(j<i) fs=1;if(j>l1[pos])fl=1;if(i<r1[pos])fr=1;if(j) fh=1; ans+=dfs(pos-1,fl,fr,fs,fh); } }ans%=mod; return ans; } int main(){ cin>>l>>r; for(int i=63;i>=0;i--) { l1[i]=(bool)((1ll<<i)&l);r1[i]=(bool((1ll<<i)&r)); } cout<<dfs(63,0,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
import java.io.*; import java.util.*; class Solver { private static final long M = 1_000_000_007L; private final long l, r; private long[][][][] dptable; Solver(long l, long r) { this.l = l; this.r = r; } private int getIndexFromBoolean(boolean b) { return b ? 1 : 0; } private long getBit(long v, int bitPosition) { return (v >> bitPosition) & 1L; } private long solve(int bitPosition, boolean xAlreadyHigherThanL, boolean yAlreadyLowerThanR, boolean mostSignificantBitAlreadyFlipped) { if (bitPosition < 0) { return 1; } long cacheValue = dptable[bitPosition][getIndexFromBoolean(xAlreadyHigherThanL)][getIndexFromBoolean(yAlreadyLowerThanR)][getIndexFromBoolean(mostSignificantBitAlreadyFlipped)]; if (cacheValue >= 0) { return cacheValue; } long output = 0L; // x: 0, y: 0 if (xAlreadyHigherThanL || getBit(l, bitPosition) == 0L) { output += solve( bitPosition - 1, xAlreadyHigherThanL, yAlreadyLowerThanR || getBit(r, bitPosition) == 1L, mostSignificantBitAlreadyFlipped); } // x: 1, y: 1 if (yAlreadyLowerThanR || getBit(r, bitPosition) == 1L) { output += solve( bitPosition - 1, xAlreadyHigherThanL || getBit(l, bitPosition) == 0L, yAlreadyLowerThanR, true); } // x: 0, y: 1 if ((xAlreadyHigherThanL || getBit(l, bitPosition) == 0L) && (yAlreadyLowerThanR || getBit(r, bitPosition) == 1L) && mostSignificantBitAlreadyFlipped) { output += solve( bitPosition - 1, xAlreadyHigherThanL, yAlreadyLowerThanR, true); } output %= M; dptable[bitPosition][getIndexFromBoolean(xAlreadyHigherThanL)][getIndexFromBoolean(yAlreadyLowerThanR)][getIndexFromBoolean(mostSignificantBitAlreadyFlipped)] = output; return output; } public long solve() { dptable = new long[60][2][2][2]; for (long[][][] d1 : dptable) { for (long[][] d2 : d1) { for (long[] d3 : d2) { Arrays.fill(d3, -1); } } } return solve(59, false, false, false); } } public class Main { private static void execute(ContestReader reader, PrintWriter out) { long l = reader.nextLong(); long r = reader.nextLong(); out.println(new Solver(l, r).solve()); } public static void main(String[] args) { ContestReader reader = new ContestReader(System.in); PrintWriter out = new PrintWriter(System.out); execute(reader, out); out.flush(); } } class ContestReader { private BufferedReader reader; private StringTokenizer tokenizer; ContestReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public 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(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; i++) { array[i] = nextInt(); } return array; } public long[] nextLongArray(int n) { long[] array = new long[n]; for (int i = 0; i < n; i++) { array[i] = nextLong(); } return array; } } class Algorithm { private static void swap(Object[] list, int a, int b) { Object tmp = list[a]; list[a] = list[b]; list[b] = tmp; } public static <T extends Comparable<? super T>> boolean nextPermutation(T[] ts) { int rightMostAscendingOrderIndex = ts.length - 2; while (rightMostAscendingOrderIndex >= 0 && ts[rightMostAscendingOrderIndex].compareTo(ts[rightMostAscendingOrderIndex + 1]) >= 0) { rightMostAscendingOrderIndex--; } if (rightMostAscendingOrderIndex < 0) { return false; } int rightMostGreatorIndex = ts.length - 1; while (ts[rightMostAscendingOrderIndex].compareTo(ts[rightMostGreatorIndex]) >= 0) { rightMostGreatorIndex--; } swap(ts, rightMostAscendingOrderIndex, rightMostGreatorIndex); for (int i = 0; i < (ts.length - rightMostAscendingOrderIndex - 1) / 2; i++) { swap(ts, rightMostAscendingOrderIndex + 1 + i, ts.length - 1 - i); } return true; } public static void shuffle(int[] array) { Random random = new Random(); int n = array.length; for (int i = 0; i < n; i++) { int randomIndex = i + random.nextInt(n - i); int temp = array[i]; array[i] = array[randomIndex]; array[randomIndex] = temp; } } public static void shuffle(long[] array) { Random random = new Random(); int n = array.length; for (int i = 0; i < n; i++) { int randomIndex = i + random.nextInt(n - i); long temp = array[i]; array[i] = array[randomIndex]; array[randomIndex] = temp; } } public static void sort(int[] array) { shuffle(array); Arrays.sort(array); } public static void sort(long[] array) { shuffle(array); Arrays.sort(array); } }
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
#include<bits/stdc++.h> #define ll long long using namespace std; const int P=1e9+7; ll L,R; int num[71],len,mi[71]; int dp[71][2][2][2]; int dfs(int x,bool fl1,bool fl2,bool fl3){ if(x==0)return fl1; if(dp[x][fl1][fl2][fl3]!=-1)return dp[x][fl1][fl2][fl3]; int up=fl2?1:num[x],res=0; if(fl3||mi[x]==0)res=(res+dfs(x-1,fl1,num[x]|fl2,fl3))%P; if(up){ res=(res+dfs(x-1,1,fl2,fl3|(mi[x]^1)))%P; if(fl1&&(fl3||mi[x]==0))res=(res+dfs(x-1,1,fl2,fl3))%P; } dp[x][fl1][fl2][fl3]=res; return res; } int Solve(ll x,ll y){ while(x){ num[++len]=x&1; x>>=1; } int tot=0; while(y){ mi[++tot]=y&1; y>>=1; } return dfs(len,0,0,0); } int main(){ memset(dp,-1,sizeof(dp)); scanf("%lld%lld",&L,&R); printf("%d\n",Solve(R,L)); }
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 i1(): return int(input()) def i2(): return [int(i) for i in input().split()] [l,r]=i2() z=10**9+7 dp=[[[[0 for l in range(2)]for k in range(2)]for j in range(2)]for i in range(61)] dp[60][0][0][0]=1 for n in range(60)[::-1]: lb=l>>n&1 rb=r>>n&1 for x in range(2): for y in range(2): for i in range(2): for j in range(2): for k in range(2): ni=i+0 nj=j+0 nk=k+0 if x>y: continue if k==0 and x!=y: continue if x&y: nk=1 if i==0 and x<lb: continue if x>lb: ni=1 if j==0 and y>rb: continue if y<rb: nj=1 dp[n][ni][nj][nk]+=dp[n+1][i][j][k] dp[n][ni][nj][nk]%=z print((dp[0][1][1][1]+dp[0][0][1][1]+dp[0][1][0][1]+dp[0][0][0][1])%z)
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; 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); } } } } } 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
/*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'; }
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; using vi = vector<int>; using vll = vector<ll>; using vvi = vector<vector<int>>; using vvl = vector<vector<ll>>; ll P = 1000000007; ll L, R; ll memo[64][2][2][2] = {0}; ll f(int k, int m, int l, int r) { if (k < 0) return m == 1; if (memo[k][m][l][r] >= 0) return memo[k][m][l][r]; ll ret = 0; for (int li = 0; li < 2; li++) for (int ri = 0; ri < 2; ri++) { if (li == 0 && l && (L & (1LL << k)) > 0) continue; if (ri == 1 && r && (R & (1LL << k)) == 0) continue; if (li > ri) continue; if (m == 0 && li != ri) continue; ret += f(k - 1, m || (li == 1 && ri == 1), l && ((L >> k) & 1) == li, r && ((R >> k) & 1) == ri); } return memo[k][m][l][r] = ret % P; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); 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 l = 0; l < 2; l++) memo[i][j][k][l] = -1; cout << f(61, 0, 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> #define rep(i, n) for (int i = 0; i < (n); i++) using ll = long long; using namespace std; ll f[61][2][2][2]; const int M=1000000007; int main() { ll L, R; cin >> L >> R; /* i 从左往右前 i 位 , j 是否>L 1是, 0表示==L k 是否<R 1是, 0表示==R x L的第i位 y R的第i位 //l 0 表示 小的数==大的数 1表示 小的数<大的数 m 0 表示 第一个1 填了没有。 d 表示 小的数填d e 表示 大的数填e */ f[60][0][0][0]=1; for (int i=59; i>=0; i--) rep(j,2) rep(k,2) rep(m,2) { int x=(L>>i)&1; int y=(R>>i)&1; int low=j?0:x; int high=k?1:y; for (int d=low;d<=high; d++) for (int e=d;e<=high; e++) { if (!m && (d^e)) continue; ll& t=f[i][j||d>low][k||e<high][m||d]; t = (t+f[i+1][j][k][m])%M; } } ll ans=0; rep(j,2) rep(k,2) ans=(ans+f[0][j][k][1])%M; 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 rep(i, n) for(int i = 0; i < (n); ++i) typedef long long ll; typedef pair<ll, ll> P; const int MAX = 1e5 + 10; const int INF = 1e9; const int MOD = 1e9 + 7; ll L, R; ll dp[61][2][2][2]; int main(){ cin >> L >> R; 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 && x) continue; int nj, nk, ns; nj = j, nk = k, ns = s; // s:既に最上位ビットを通過したか if(!s && y && !x) continue; if(x && y) ns = 1; // j:L <= x を既に満たしているか if(!j && l && !x) continue; if(!l && x) nj = 1; // k:y <= R を既に満たしているか if(!k && !r && y) continue; if(r && !y) nk = 1; dp[i][nj][nk][ns] += dp[i + 1][j][k][s]; dp[i][nj][nk][ns] %= MOD; } } } ll 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 <bits/stdc++.h> using namespace std; const long long int MOD = 1e9 + 7; long long int dp[61][2][2][2];// 10-18 < 2^60 int main(){ long long int l, r; cin >> l >> r; dp[60][0][0][0] = 1; for(int i=59; i>=0; i--){// digit DP for(int j=0; j<2; j++){ for(int k=0; k<2; k++){ for(int s=0; s<2; s++){ for(int nx=0; nx<2; nx++){ for(int ny=0; ny<2; ny++){ if(nx == 1 && ny == 0) continue; int nj = j, nk = k, ns = s; // s : MSB if(s == 0 && nx != ny) continue; if(nx == 1 && ny == 1) ns = 1; // j : L <= x if(j == 0 && nx == 0 && ((l>>i) & 1)) continue;// x < L if(nx == 1 && !((l>>i) & 1)) nj = 1; // k : y <= R if(k == 0 && ny == 1 && !((r>>i) & 1)) continue;// y > R if(ny == 0 && ((r>>i) & 1)) nk = 1; dp[i][nj][nk][ns] += dp[i+1][j][k][s]; dp[i][nj][nk][ns] %= MOD; } } } } } } long long int 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]; 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
mod = 10**9+7 def count(L, R): res = 0 for j in range(70): res = (res + subcount(j, L, R)) % mod return res def subcount(j, L, R): if j >= L.bit_length(): return 0 dp = [[0]*4 for _ in range(70)] cnt = 0 if R.bit_length() != j+1: cnt += 1 if L.bit_length() != j+1: cnt += 2 dp[j][cnt] = 1 for i in range(j, 0, -1): yf = R & (1<<(i-1)) xf = L & (1<<(i-1)) d0 = dp[i][0] if yf and xf: dp[i-1][0] += d0 dp[i-1][2] += d0 dp[i-1][3] += d0 elif yf: dp[i-1][0] += d0 dp[i-1][1] += d0 elif xf: dp[i-1][2] += d0 else: dp[i-1][0] += d0 d1 = dp[i][1] if xf: dp[i-1][1] += d1 dp[i-1][3] += 2*d1 else: dp[i-1][1] += 2*d1 d2 = dp[i][2] if yf: dp[i-1][2] += 2*d2 dp[i-1][3] += d2 else: dp[i-1][2] += d2 d3 = dp[i][3] dp[i-1][3] += 3*d3 dp[i-1][0] %= mod dp[i-1][1] %= mod dp[i-1][2] %= mod dp[i-1][3] %= mod return sum(dp[0][i] for i in range(4)) L, R = map(int, input().split()) print((count(R, R) - count(L-1, 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
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int mod=1e9+7; ll L,R; ll f[64][2][2][2][2]; int l[100],r[100]; ll dfs(int x,int fl,int fr,int s,int ht) { if(x<0)return 1; // x:第x位 (二进制转十进制下权值是1的为第0位) // fl: y之前是否有填过小于 R的某一位的数字 例如:R:11000 y:10??? fl即为1 表示后面数字可以往大了填 // fr: x之前是否有填过大于 L的某一位的数字 类似fl // s: 之前x是否有某一位 < y的某一位 如果有,后面的位数就没有相对大小限制了 (确保 x<=y) //ht:确保 x,y 最高位一样都为1 (满足推导的要求) ll &ans=f[x][fl][fr][s][ht]; if(ans) return ans; for(int i=0;i<2;i++)//y { for(int j=0;j<2;j++)//x { int tl=fl,tr=fr,ts=s,tt=ht; if(!tt&&i&&i!=j)continue; if((j&i)!=j)continue; if(!tl&&j<l[x])continue; if(!tr&&i>r[x])continue; if(!ts&&j>i)continue; if(j>l[x])tl=1;if(i<r[x])tr=1;if(j<i)ts=1;if(j)tt=1; ans+=dfs(x-1,tl,tr,ts,tt); } } ans%=mod; return ans; } int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>L>>R; for(int i=63;i>=0;i--) { l[i]=(bool)((1ll<<i)&L);r[i]=(bool)((1ll<<i)&R); } cout<<dfs(63,0,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> using namespace std; typedef long long ll; #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } const int MOD = TEN(9) + 7; inline void add(int &x, int y) { x += y; if (x >= MOD) x -= MOD; } const int maxn = 62; int dp[maxn][2][2][2]; //L<x, y<R, highest (x <= y can be ignored) int main() { ll L, R; cin >> L >> R; --L; ++R; int now = 0; dp[0][0][0][0] = 1; for (int i = 60; i >= 0; --i) { int p = (L >> i) & 1; int q = (R >> i) & 1; rep(a, 2) rep(b, 2) rep(c, 2) { if (dp[now][a][b][c] == 0) continue; rep(r, 2) rep(s, 2) { //for x, y int na = a, nb = b, nc = c; if (p == 1 && r == 0 && !a) continue; if (p == 0 && r == 1) na = 1; if (s == 1 && q == 0 && !b) continue; if (s == 0 && q == 1) nb = 1; if (!c && r + s == 1) continue; if (r || s) nc = 1; if (r > s) continue; add(dp[now + 1][na][nb][nc], dp[now][a][b][c]); } } now++; } cout << dp[now][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> #define pb push_back #define mp make_pair #define int long long #define inf 1000000007 #define LINF 100000000000000007LL #define ll long long using namespace std; int dp[62][2][2]; // bit, r=y? l=x? allzero? signed main(){ int l,r; cin>>l>>r; bool fl = false, fr = false, ffl = false, ffr = false; for(int i=60;i>=0;i--){ int br = ( r >> i ) % 2; int bl = ( l >> i ) % 2; if( !fl && bl ){ fl = true; ffl = true; } if( !fr && br ){ fr = true; ffr = true; } if( fr && ( !fl || ffl ) ){ dp[i][ffr][ffl]++; } if( br == 0 && bl == 0){ dp[i][0][0] += 3*dp[i+1][0][0] + dp[i+1][0][1]; dp[i][1][0] += dp[i+1][1][0]; dp[i][0][1] += 2*dp[i+1][0][1]; dp[i][1][1] += dp[i+1][1][1]; } if( br == 1 && bl == 0){ dp[i][0][0] += 3*dp[i+1][0][0] + dp[i+1][0][1] + dp[i+1][1][0]; dp[i][1][0] += 2*dp[i+1][1][0] + dp[i+1][1][1]; dp[i][0][1] += 2*dp[i+1][0][1] + dp[i+1][1][1]; dp[i][1][1] += dp[i+1][1][1]; } if( br == 0 && bl == 1){ dp[i][0][0] += 3*dp[i+1][0][0]; dp[i][1][0] += dp[i+1][1][0]; dp[i][0][1] += dp[i+1][0][1]; } if( br == 1 && bl == 1){ dp[i][0][0] += 3*dp[i+1][0][0] + dp[i+1][1][0]; dp[i][1][0] += 2*dp[i+1][1][0]; dp[i][0][1] += dp[i+1][0][1]; dp[i][1][1] += dp[i+1][1][1]; } dp[i][0][0] %=inf; dp[i][0][1]%=inf; dp[i][1][0] %=inf; dp[i][1][1]%=inf; ffl = false; ffr = false; } int ans = dp[0][0][0]+dp[0][1][0]+dp[0][0][1]+dp[0][1][1]; cout<<ans%inf<<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 next Next #define int long long const int mod=1e9+7; int dp[65][2][2][2]; /*char buf[1<<21],*p1=buf,*p2=buf; inline int gc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}*/ #define gc getchar inline int read() { int ret=0,f=0;char c=gc(); while(!isdigit(c)){if(c=='-')f=1;c=gc();} while(isdigit(c)){ret=ret*10+c-48;c=gc();} if(f)return -ret;return ret; } int solve(int l,int r,int len,int lim1,int lim2,int zero) { if(len<0)return 1; int &ans=dp[len][lim1][lim2][zero]; if(ans!=-1)return ans; ans=0; int up2=lim2?((r>>len)&1):1; int up1=lim1?((l>>len)&1):0; for(int i=up1;i<=1;i++) for(int j=0;j<=up2;j++) { if (i>j)continue; if(zero&&i!=j)continue; (ans+=solve(l,r,len-1,lim1&&i==up1,lim2&&j==up2,zero&&j==0))%=mod; } return ans; } signed main() { memset(dp,-1,sizeof(dp)); int L=read(),R=read(); cout<<solve(L,R,60,1,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 <iostream> #include <algorithm> #include <vector> using namespace std; long long L, R; vector<bool> l, r; const long long mod = 1000000007; vector<vector<vector<vector<long long>>>> memo(60, vector<vector<vector<long long>>>(2, vector<vector<long long>>(2, vector<long long>(2, -1)))); long long f(int pos, bool flagX, bool flagY, bool flagZ) { if (pos == -1) return 1; if (memo.at(pos).at(flagX).at(flagY).at(flagZ) != -1) return memo.at(pos).at(flagX).at(flagY).at(flagZ); long long ret = 0; if (flagX || !l.at(pos)) { if (r.at(pos)) ret += f(pos - 1, flagX, 1, flagZ); else ret += f(pos - 1, flagX, flagY, flagZ); } if ((flagX || !l.at(pos)) && (flagY || r.at(pos)) && flagZ) { ret += f(pos - 1, flagX, flagY, flagZ); } if (flagY || r.at(pos)) { if (l.at(pos)) ret += f(pos - 1, flagX, flagY, 1); else ret += f(pos - 1, 1, flagY, 1); } ret %= mod; memo.at(pos).at(flagX).at(flagY).at(flagZ) = ret; return ret; } int main() { cin >> L >> R; for (int i = 59; i >= 0; --i) { if (L & ((long long)1 << i)) l.push_back(1); else l.push_back(0); if (R & ((long long)1 << i)) r.push_back(1); else r.push_back(0); } reverse(l.begin(), l.end()); reverse(r.begin(), r.end()); long long ans = f(59, 0, 0, 0); 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
import bisect import collections import itertools def getint(): return int(input()) def getints(): return list(map(int, input().split())) def getint2d(rows): return [getints() for _ in range(rows)] def getgrid(rows): return [input() for _ in range(rows)] def array1d(n, value): return [value for _ in range(n)] def array2d(n, m, value): return [array1d(m, value) for _ in range(n)] min_val,max_val=getints() def get_key(pos, is_small, is_large, found): return ((pos * 2 + is_small) * 2 + is_large) * 2 + found mod = 10**9 + 7 cache = [-1] * get_key(64,1,1,1) def solve(pos, is_small, is_large, found): if pos < 0: return 1 key = get_key(pos,is_small,is_large,found) res = cache[key] if res >= 0: return res res = 0 for x in [0,1]: for y in [0,1]: if x == 1 and y == 0: continue if not is_small and x == 0 and (min_val >> pos & 1) == 1: continue if not is_large and y == 1 and (max_val >> pos & 1) == 0: continue if not found and x != y: continue new_is_small = True if x > (min_val >> pos & 1) else is_small new_is_large = True if y < (max_val >> pos & 1) else is_large new_found = True if x == y and x == 1 else found res += solve(pos-1, new_is_small, new_is_large, new_found) res = res % mod cache[key] = res return res print(solve(63, False, False, False))
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
#pragma GCC optimize ("O3") #pragma GCC optimize ("unroll-loops") #include "bits/stdc++.h" using namespace std; #define pb push_back #define F first #define S second #define f(i,a,b) for(int i = a; i < b; i++) using ll = long long; using db = long double; using ii = pair<int, int>; const int N = 1e6 + 5, LG = 19, MOD = 1e9 + 7; const int SQ =225; const long double EPS = 1e-7; ll XL, XR; ll L, R; int dp[66][2][2]; int solve(int i, bool f2, bool f3){ if(i < 0) return 1; int &ret = dp[i][f2][f3]; if(~ret) return ret; ret = 0; int val = (XR >> i & 1); int val2 = (XL >> i & 1); f(a,0,2) f(b,a,2){ if(a <= val2 || f3){ if(b <= val || f2){ ret += solve(i - 1, f2 | (b < val), f3 | (a < val2)); if(ret >= MOD)ret -= MOD; } } } return ret; } int solve(ll x1,ll x2){ memset(dp, -1, sizeof dp); XL = x1; XR = x2; bool ok = false; bool ok2 = false; int ret = 0; for(int i = 59; i >= 0; --i){ if(XL >> i & 1 || ok){ ret += solve(i-1,ok2,ok); if(ret >= MOD)ret -= MOD; } ok |= (XL >> i & 1); ok2 |= (XR >> i & 1); } return ret; } int32_t main(){ #ifdef ONLINE_JUDGE ios_base::sync_with_stdio(0); cin.tie(0); #endif // cout << solve(R,R) << '\n'; cin >> L >> R; cout << (solve(R,R) - solve(L-1,R) + MOD) % MOD << '\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
def f_coincidence(MOD=10 ** 9 + 7): # 参考: https://atcoder.jp/contests/abc138/submissions/7013926 from itertools import product L, R = [int(i) for i in input().split()] dp = [[[[0 for s in range(2)] for k in range(2)] for j in range(2)] for i in range(61)] dp[60][0][0][0] = 1 for i in range(59, -1, -1): lb = (L >> i) & 1 # 'b' はビット値であることを示す(以下も同じ) rb = (R >> i) & 1 r2 = range(2) for j, k, s in product(r2, r2, r2): pre = dp[i + 1][j][k][s] # ビットごとに値を試す for xb, yb in product(r2, r2): # editorial の通り、このビットの組は元の条件を満たさない if xb == 1 and yb == 0: continue # editorial の通り、これ以降は x <= y が満たされる nj, nk, ns = j, k, s # 今注目している桁でMSBが立つか? if s == 0 and xb != yb: continue # どちらかが立っていない if s == 0 and xb == 1 and yb == 1: ns = 1 # ここで初めてMSBが立った # L <= x を満たすか? if j == 0 and xb == 0 and lb == 1: # jが0のまま xb<lb となれば、L<=xになりようがない # jが1なら、xbとlbがどうなっていようがL<=xである continue if j == 0 and xb == 1 and lb == 0: nj = 1 # この桁で L<=x が確定する # y <= R を満たすか? if k == 0 and yb == 1 and rb == 0: continue # L<=x の場合分けと同じこと if k == 0 and yb == 0 and rb == 1: nk = 1 # 反映 dp[i][nj][nk][ns] += pre dp[i][nj][nk][ns] %= MOD ans = sum([dp[0][j][k][s] for j in range(2) for k in range(2) for s in range(2)]) % MOD return ans print(f_coincidence())
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
def examA(): N = DI()/dec(7) ans = N print(N) return def examB(): ans = 0 print(ans) return def examC(): ans = 0 print(ans) return def examD(): ans = 0 print(ans) return def examE(): ans = 0 print(ans) return def examF(): def bitdp(l, r): n = r.bit_length() dp = defaultdict(int) dp[0, 0, 0, 0] = 1 for i, less, greater, start in itertools.product(range(n), (0, 1), (0, 1), (0, 1)): R_ = 1 if less else (r>>(n-i-1))&1 L_ = 0 if greater else (l>>(n-i-1))&1 for y in range(R_ + 1): for x in range(L_, y + 1): less_ = less or y < R_ greater_ = greater or L_ < x start_ = start or (y==1 and x==1) if not start and (y==1 and x==0): # xorがあまりを確実に上回る continue dp[i + 1, less_, greater_, start_] += dp[i, less, greater, start] dp[i + 1, less_, greater_, start_] %= mod #print(dp[i + 1, less_, greater_, start_],i+1,less_,greater,start_,start) #for i in range(n): # print(dp[i,0,0,0],dp[i,1,0,0],dp[i,0,1,0],dp[i,1,1,0]) res = sum(dp[n, less, greater, 1] for less, greater in itertools.product((0, 1), (0, 1))) return res L, R = LI() ans = bitdp(L,R) % mod print(ans) return from decimal import getcontext,Decimal as dec import sys,bisect,itertools,heapq,math,random from copy import deepcopy from heapq import heappop,heappush,heapify from collections import Counter,defaultdict,deque read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines def I(): return int(input()) def LI(): return list(map(int,sys.stdin.readline().split())) def DI(): return dec(input()) def LDI(): return list(map(dec,sys.stdin.readline().split())) def LSI(): return list(map(str,sys.stdin.readline().split())) def LS(): return sys.stdin.readline().split() def SI(): return sys.stdin.readline().strip() global mod,mod2,inf,alphabet,_ep mod = 10**9 + 7 mod2 = 998244353 inf = 10**18 _ep = dec("0.000000000001") alphabet = [chr(ord('a') + i) for i in range(26)] alphabet_convert = {chr(ord('a') + i): i for i in range(26)} getcontext().prec = 28 sys.setrecursionlimit(10**7) if __name__ == '__main__': examF() """ 142 12 9 1445 0 1 asd dfg hj o o aidn """
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
from functools import lru_cache import sys sys.setrecursionlimit(1000000) P = 10**9+7 @lru_cache(maxsize=None) def subcalc(l, r): if l < 0 or r < 0: print("ERROR") print("l, r =", l, r) print(1//0) if l > r: return 0 if r == 0: return 1 aa, bb = l.bit_length(), r.bit_length() if aa == bb: return subcalc(l-(1<<aa-1), r-(1<<bb-1)) if (r & (r+1) == 0) and (l == 0): return pow(3, r.bit_length(), P) t = (subcalc(l, r-(1<<bb-1)) + subcalc(l, (1<<bb-1)-1) + subcalc(0, r-(1<<bb-1))) % P # print("subcalc", l, r, t) return t @lru_cache(maxsize=None) def calc(L, R): if L < 0 or R < 0: print("ERROR") print("l, r =", l, r) print(1//0) if L > R: return 0 a = L.bit_length() b = R.bit_length() if b > a: t = (calc(L, (1<<b-1)-1) + calc(1<<b-1, R)) % P # print("calc", L, R, t) return t a = 1 << L.bit_length() - 1 if L else 0 t = subcalc(L-a, R-a) # print("calc", L, R, t) return t L, R = map(int, input().split()) print(calc(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(ll (i) = (0);(i) < (n);++i) #define MOD 1000000007 typedef long long ll; ll dp[63][1<<3]; void mod_add(ll &a, ll b){ a += b; a %= MOD; } int main(){ ll l, r;cin >> l >> r; dp[0][0] = 1; REP(i, 62){ REP(j, (1LL<<3)){ REP(x, 2)REP(y, 2){ if(x == 1 && y == 0)continue; int nj = j; if(!(nj & 1)){ int tmp = ((l & (1LL << (61-i))) != 0); if(x < tmp)continue; else if(x > tmp)nj |= 1; } if(!(nj & 2)){ int tmp = ((r & (1LL << (61-i))) != 0); if(y > tmp)continue; else if(y < tmp)nj |= 2; } if(!(nj & 4)){ if(x + y != 0 && x + y != 2)continue; if(x + y == 2)nj |= 4; } mod_add(dp[i+1][nj], dp[i][j]); } } } ll ans = 0; REP(i, 8)mod_add(ans, dp[62][i]); 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
def p_f(): from itertools import product L, R = map(int, input().split()) mod = 10 ** 9 + 7 # dp[i][j][k][l]: iは桁, jはL<=xか, kはy<=Rか, lは桁数が同じか dp = [[[[0] * 2 for _ in range(2)] for _ in range(2)] for _ in range(61)] dp[60][0][0][0] = 1 for i in reversed(range(60)): # LとRのiビット目を取り出す lb = L >> i & 1 rb = R >> i & 1 for j, k, l in product(range(2), repeat=3): # 前の状態 pre = dp[i + 1][j][k][l] for x, y in product(range(2), repeat=2): nj, nk, nl = j, k, l if x and (not y): # xが1でyが0 continue if (not l) and x != y: continue if x and y: nl = 1 # j: L <= x if (not j) and (not x) and lb: continue if x and (not lb): nj = 1 # k: y <= R if (not k) and y and (not rb): continue if (not y) and rb: nk = 1 dp[i][nj][nk][nl] += pre ans = 0 for j, k, l in product(range(2), repeat=3): ans += dp[0][j][k][l] ans %= mod print(ans) if __name__ == '__main__': p_f()
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 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=R.bit_length() 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> using namespace std; using lint = long long int; using P = pair<int, int>; using PL = pair<lint, lint>; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++) #define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--) #define REP(i, n) FOR(i,0,n) #define IREP(i, n) IFOR(i,0,n) #define ALL(a) (a).begin(),(a).end() constexpr int MOD = 1000000007; constexpr int INF = 2147483647; void yes(bool expr) {cout << (expr ? "Yes" : "No") << "\n";} int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); lint L, R; cin >> L >> R; lint ans = 0; REP(x, 60) { vector<vector<vector<lint>>> dp(61, vector<vector<lint>>(2, vector<lint>(2))); dp[60][0][0] = 1; IREP(k, 60) { lint l = L>>k&1; lint r = R>>k&1; REP(i, 2) REP(j, 2) { //00 if(k != x) { if(l == 0 || i == 1) { if(r == 0) dp[k][i][j] = (dp[k][i][j] + dp[k+1][i][j]) % MOD; else dp[k][i][min(1, j+1)] = (dp[k][i][min(1, j+1)] + dp[k+1][i][j]) % MOD; } } if(k > x) continue; //10 if(k != x) { if((l == 0 || i == 1) && (r == 1 || j == 1)) { dp[k][i][j] = (dp[k][i][j] + dp[k+1][i][j]) % MOD; } } //11 if(r == 1 || j == 1) { if(l == 1) dp[k][i][j] = (dp[k][i][j] + dp[k+1][i][j]) % MOD; else dp[k][min(1, i+1)][j] = (dp[k][min(1, i+1)][j] + dp[k+1][i][j]) % MOD; } } } REP(i, 2) REP(j, 2) ans = (ans + dp[0][i][j]) % 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
#!/usr/bin/env python from collections import deque, defaultdict import itertools as ite import sys import math import decimal sys.setrecursionlimit(1000000) INF = 10 ** 18 MOD = 10 ** 9 + 7 L_, R_ = map(int, raw_input().split()) ans = 0 for MSB in range(70): S = 2 ** MSB if S * 2 <= L_ or R_ < S: continue L, R = L_, R_ if L <= S: L = S if 2 * S - 1 <= R: R = 2 * S - 1 n1 = nL = nR = 0 nLR = 1 SL = SR = 1 for i in range(MSB)[::-1]: flag1 = ((1 << i) & L != 0) flag2 = ((1 << i) & R != 0) SL *= 2 SR *= 2 SL += flag1 SR += flag2 n1 *= 3 if flag1 and not flag2: nLR = 0 if SR - SL >= 2: n1 += SR / 2 - SL / 2 - 1 if not flag1: n1 += nL if flag2: n1 += nR if not flag1: nL *= 2 nL += 1 if flag2: nR *= 2 nR += 1 if not flag1 and flag2: nL += nLR nR += nLR if L == R: ans += 1 else: ans += n1 + (SR - SL + 1) + nL + nR + nLR print ans % MOD
PYTHON
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 class mint: def __init__(self, i): self.i = i def __add__(self, m): t = self.i + (m.i if isinstance(m, mint) else m) if t > MOD: t -= MOD return mint(t) def __radd__(self, m): t = self.i + (m.i if isinstance(m, mint) else m) if t > MOD: t -= MOD return mint(t) def __mul__(self, m): return mint(self.i * (m.i if isinstance(m, mint) else m) % MOD) def __sub__(self, m): t = self.i - m.i if t < 0: t += MOD return mint(t) def __pow__(self, m): i = self.i res = 1 while(m > 0): if m & 1: res = res * i % MOD i = i * i % MOD m >>= 1 return mint(res) def __truediv__(self, m): return mint(self.i * (m ** (MOD - 2)).i % MOD) def __repr__(self): return repr(self.i) L, R = map(int, input().split()) dp = [[mint(0) for _ in range(4)] for _ in range(61)] for d in range(60, 0, -1): l = L >> d - 1 & 1 r = R >> d - 1 & 1 if (L >> d - 1) == 0: if (R >> d - 1) > 1: dp[d-1][3] += 1 elif (R >> d - 1) == 1: dp[d-1][2] += 1 elif (L >> d - 1) == 1: if (R >> d - 1) > 1: dp[d-1][1] += 1 else: dp[d-1][0] += 1 # x bound, y bound if l == r: dp[d-1][0] += dp[d][0] elif l < r: dp[d-1][0] += dp[d][0] dp[d-1][1] += dp[d][0] dp[d-1][2] += dp[d][0] # x bound, y free if l == 0: dp[d-1][1] += dp[d][1] * 2 dp[d-1][3] += dp[d][1] else: dp[d-1][1] += dp[d][1] # x free, y bound if r == 1: dp[d-1][2] += dp[d][2] * 2 dp[d-1][3] += dp[d][2] else: dp[d-1][2] += dp[d][2] # x free, y free dp[d-1][3] += dp[d][3] * 3 print(sum(dp[0]))
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; typedef long long ll; ll mod = 1e9+7; ll esq, dir; ll dp[65][5][5][5]; ll solve(int pos, int biggerMin, int smallerMax, int k){ // cout << pos << " " << biggerMin << " " << smallerMax << endl; if(pos < 0) return 1; if(dp[pos][biggerMin][smallerMax][k] != -1) return dp[pos][biggerMin][smallerMax][k]; int a = (dir >> pos)&1; int b = (esq >> pos)&1; // cout << a << " " << b << endl; ll res = 0; if( b == 0 || biggerMin) // placing (0,0) in both numbers res += solve(pos-1, biggerMin, smallerMax || (a == 1), k); if( (b == 0 || biggerMin) && (a == 1 || smallerMax) && k) res += solve(pos-1, biggerMin, smallerMax, k); if( a == 1 || smallerMax) res += solve(pos-1, biggerMin || (b == 0), smallerMax, 1); res %= mod; return dp[pos][biggerMin][smallerMax][k] = res; } int main(){ memset(dp, -1, sizeof dp); cin >> esq >> dir; cout << solve(60, 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
L,R = map(int,input().split()) mod = 10**9+7 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 j==0 : L2 = L elif L[j] == "1" : continue else : L2 = L[:j] + "1" + "?"*(N-j-1) tmp = 1 for r,l in zip(R2,L2): if r=="0" and l=="1" : tmp *= 0 ; break elif r=="?" and l=="?" : tmp = tmp*3%mod elif r=="?" and l=="0" : tmp = tmp*2%mod elif r=="1" and l=="?" : tmp = tmp*2%mod ret += tmp ret %= mod return ret 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 <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++) { for (int cl = 0; cl <= 1; cl++) { for (int cr = 0; cr <= 1; cr++) { if (cl <= cr && !(sb && (cl < cr)) && !(fl && (cl < bl)) && !(fr && (cr > br))) { addm(dp[i][fl][fr][sb], dp[i-1][fl&&cl==bl][fr&&cr==br][sb&&cl!=1]); } } } } } } } 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 <iostream> #include <vector> using namespace std; typedef long long ll; const ll MOD = 1000000007; vector<ll> lbits, rbits; ll dp[60][2][2][2]; void get_bits(ll n, vector<ll>& out){ out.clear(); for(int i = 0; n > 0; ++i){ out.push_back(n % 2); n /= 2; } } ll calc(ll pos, bool x_gt_l, bool y_lt_r, bool has_top){ if(pos < 0){ return 1; } if(dp[pos][x_gt_l][y_lt_r][has_top] != -1){ return dp[pos][x_gt_l][y_lt_r][has_top]; } ll ret = 0; // x = 0, y = 0 if((x_gt_l || lbits.at(pos) == 0)){ ret += calc(pos - 1, x_gt_l, y_lt_r || rbits.at(pos) == 1, has_top); } // x = 0, y = 1 if(has_top && (x_gt_l || lbits.at(pos) == 0) && (y_lt_r || rbits.at(pos) == 1)){ ret += calc(pos - 1, x_gt_l, y_lt_r, has_top); } // x = 1, y = 1 if(y_lt_r || rbits.at(pos) == 1){ ret += calc(pos - 1, x_gt_l || lbits.at(pos) == 0, y_lt_r, true); } ret %= MOD; dp[pos][x_gt_l][y_lt_r][has_top] = ret; return ret; } int main(){ ll l, r; cin >> l >> r; get_bits(l, lbits); get_bits(r, rbits); lbits.resize(60, 0); rbits.resize(60, 0); fill( &dp[0][0][0][0], &dp[0][0][0][0] + sizeof(dp) / sizeof(dp[0][0][0][0]), -1 ); ll ans = calc(59, 0, 0, 0); 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
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { long mod = 1_000_000_007; long[][][][] dp; 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(); 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++) { if(x == 1 && y == 0) { continue; } int nj = j; int nk = k; int ns = s; if(s == 0 && x != y) { continue; } if(x == 1 && y == 1) { ns = 1; } if(j == 0 && lb == 1 && x == 0) { continue; } if(lb == 0 && x == 1) { nj = 1; } if(k == 0 && rb == 0 && y == 1) { continue; } 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
#include <bits/stdc++.h> using i64 = long long; constexpr i64 mod = 1000000007; std::string l, r; i64 dp[61][2][2][2]; // l <= x, y <= r, MSB is fixed i64 rec(int p, int a, int b, int c) { if (!p) return 1; if (dp[p][a][b][c] >= 0) return dp[p][a][b][c]; i64 ret = 0; if (a || l[60 - p] == '0') { ret = (ret + rec(p - 1, a, r[60 - p] == '1' || b, c)) % mod; } if (b || r[60 - p] == '1') { ret = (ret + rec(p - 1, l[60 - p] == '0' || a, b, 1)) % mod; } if (c && (l[60 - p] == '0' || a) && (r[60 - p] == '1' || b)) { ret = (ret + rec(p - 1, a, b, c)) % mod; } return dp[p][a][b][c] = ret; } int main() { i64 ll, rr; std::cin >> ll >> rr; for (i64 i = 59; i >= 0; --i) l += '0' + ((ll >> i) & 1); for (i64 i = 59; i >= 0; --i) r += '0' + ((rr >> i) & 1); for (auto &w : dp) for (auto &x : w) for (auto &y : x) for (auto &z : y) z = -1; std::cout << rec(60, 0, 0, 0) << std::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
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <array> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> #include <iterator> #include <memory> #include <regex> using namespace std; const int MOD = 1000000007; int main() { long long l, r; cin >> l >> r; vector<int> dp(8, 0); dp[0] = 1; for(int i=0; i<63; ++i){ int l2 = (l >> i) & 1; int r2 = (r >> i) & 1; vector<int> nextDp(8, 0); for(int j=0; j<8; ++j){ for(int a=0; a<2; ++a){ for(int b=0; b<2; ++b){ if(a < b) continue; int k = j; if(a > r2) k |= 1; else if(a < r2) k &= ~1; if(b < l2) k |= 2; else if(b > l2) k &= ~2; if(a == 1 && b == 1) k |= 4; else if(a != b) k &= ~4; nextDp[k] += dp[j]; nextDp[k] %= MOD; } } } dp = move(nextDp); } cout << dp[4] << 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 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 tmp = 1 for r,l in zip(R2,L2): if r=="0" and l=="1" : tmp *= 0 ; break elif r=="?" and l=="?" : tmp = tmp*3%mod elif r=="?" and l=="0" : tmp = tmp*2%mod elif r=="1" and l=="?" : tmp = tmp*2%mod ret += tmp ret %= mod return ret 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
import sys stdin = sys.stdin ni = lambda: int(ns()) na = lambda: list(map(int, stdin.readline().split())) ns = lambda: stdin.readline().rstrip() # ignore trailing spaces # 最高次は等しくないといけないので、 # y % x = y-xになる # yがxを包含していればok mod = 1000000007 def ca(x, y): ct = 0 for i in range(x, y+1): for j in range(i, y+1): if j % i == (j^i): ct += 1 return ct ans = 0 l, r = na() dp = [0] * 4 for i in range(60, -1, -1): ndp = [0] * 4 base = 1<<i if base <= r and l < base*2: ptn = 0 if base+1 <= l: ptn += 1 if r < base*2-1: ptn += 2 ndp[ptn] += 1 if l>>i != r>>i: if (l>>i&1) == 0 and (r>>i&1) == 1: ndp[1] += dp[3] ndp[2] += dp[3] if ((l>>i)&(r>>i)) == (l>>i): ndp[3] += dp[3] if (l >> i & 1) == 0: ndp[0] += dp[1] ndp[1] += dp[1] ndp[1] += dp[1] if (r >> i & 1) == 1: ndp[0] += dp[2] ndp[2] += dp[2] ndp[2] += dp[2] ndp[0] += dp[0] * 3 for k in range(4): ndp[k] %= mod dp = ndp print(sum(dp) % 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> using namespace std; #define ll long long #define mod 1000000007 #define BIT_COUNT 70 int add(int x,int y) { int ret = x+y; if(ret>=mod) { ret -= mod; } if(ret<0) { ret += mod; } return ret; } int dp[BIT_COUNT][2][2][2]; ll L,R; int solveDp(int pos,int mbit,int LeqX,int ReqY) { if(pos<0) { return mbit==1; } int &ret = dp[pos][mbit][LeqX][ReqY]; if(ret!=-1) { return ret; } ret = 0; for(int a=0;a<2;++a) { for(int b=0;b<2;++b) { if(a==1 && b==0) continue; if(LeqX && a==0 && (L&(1LL<<pos))>0 ) continue; if(ReqY && b==1 && (R&(1LL<<pos) )==0)continue; if(mbit==0 && (a!=b)) continue; int nmbit = mbit | (a==1 && b==1); int nLeqX = LeqX && ( ((L>>pos)&1) == a); int nReqY = ReqY && ( ((R>>pos)&1) ==b); ret = add(ret, solveDp(pos-1,nmbit,nLeqX,nReqY)); } } return ret; } void solve() { scanf("%lld %lld",&L,&R); memset(dp,-1,sizeof(dp)); cout<<solveDp(62,0,1,1)<<endl; } int main() { 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 ll long long const int mod=1e9+7; ll l,r; ll f[64][2][2][2][2]; int L[100],R[100]; ll dfs(int pos,int fl,int fr,int s,int ht) { if(pos<0)return 1; ll &ans=f[pos][fl][fr][s][ht]; if(ans)return ans; for(int i=0;i<2;i++)//y { for(int j=0;j<2;j++)//x { int tl=fl,tr=fr,ts=s,tt=ht; if(!tt&&i)if(i!=j)continue; if((i&j)!=j)continue; if(!tl&&j<L[pos])continue; if(!tr&&i>R[pos])continue; if(!ts&&j>i)continue; if(j>L[pos])tl=1;if(i<R[pos])tr=1;if(j<i)ts=1;if(j)tt=1; ans+=dfs(pos-1,tl,tr,ts,tt); } } ans%=mod; return ans; } int main() { scanf("%lld%lld",&l,&r); for(int i=63;i>=0;i--) { L[i]=(int)((l>>i)&1),R[i]=(int)((r>>i)&1); } printf("%lld\n",dfs(63,0,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
def count(lb, rb): assert lb[0] == '1' assert rb[0] == '1' assert len(lb) == len(rb) dp = [1, 0, 0, 0] for lc, rc in zip(lb[1:], rb[1:]): ndp = [dp[0], 0, 0, 0] if rc == '0': ndp[1] += dp[1] if lc == '1': ndp[0] = 0 else: ndp[1] += dp[1] * 2 ndp[2] += dp[1] if lc == '0': ndp[1] += dp[0] ndp[3] += dp[0] if lc == '0': ndp[2] += dp[3] ndp[3] += dp[3] * 2 else: ndp[3] += dp[3] ndp[2] += dp[2] * 3 dp = ndp return sum(dp) l, r = map(int, input().split()) lb = bin(l)[2:] rb = bin(r)[2:] ld = len(lb) rd = len(rb) ans = 0 MOD = 10 ** 9 + 7 for d in range(ld, rd + 1): tlb = lb if d == ld else '1' + '0' * (d - 1) trb = rb if d == rd else '1' * d ans = (ans + count(tlb, trb)) % 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
from itertools import product MOD = 10**9 + 7 L, R = map(int, input().split()) binR = bin(R+1)[2:] maxD = len(binR) binL = bin(L-1)[2:].zfill(maxD) dp = [[[[0]*(2) for k in range(2)] for j in range(2)] for i in range(maxD+1)] dp[0][0][0][0] = 1 for d, (Ld, Rd) in enumerate(zip(binL, binR)): Ld, Rd = int(Ld), int(Rd) for isLleX, isYleR, isNum in product(range(2), repeat=3): for x, y in [(0,0), (0,1), (1,1)]: if not isNum and (x, y) == (0, 1): continue if not isLleX and x < Ld: continue if not isYleR and y > Rd: continue isLleX2 = isLleX or x > Ld isYleR2 = isYleR or y < Rd isNum2 = isNum or y == 1 dp[d+1][isLleX2][isYleR2][isNum2] += dp[d][isLleX][isYleR][isNum] dp[d+1][isLleX2][isYleR2][isNum2] %= MOD print(dp[-1][1][1][1])
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, raw_input().split()) """ R ????????1?????? y 0001????0?????? ^d ^rd x 0001???????1?? ^ld L ???????????0??? """ ans = 0 for d in range(70): if d<len(bin(L))-3: LD = [] elif d==len(bin(L))-3: LD = [i for i in range(-1,d) if i==-1 or (L>>i&1)==0] else: LD = [d] if d<len(bin(R))-3: RD = [d] elif d==len(bin(R))-3: RD = [i for i in range(-1,d) if i==-1 or (R>>i&1)==1] else: RD = [] for ld in LD: for rd in RD: a = 1 for i in range(d): if i<ld: xc = [0, 1] elif i==ld: xc = [1] else: xc = [L>>i&1] if i<rd: yc = [0, 1] elif i==rd: yc = [0] else: yc = [R>>i&1] c = 0 for x in xc: for y in yc: if y>=x: c += 1 a *= c ans += a print ans%(10**9+7)
PYTHON
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 = input().split() l = int(l) r = int(r) dp = [[[-1 for k in range(2)] for j in range(2)] for i in range(64)] def bbin(num): res = [] while num: res.append(num % 2) num //= 2 return res numr = bbin(r) numl = bbin(l) while len(numl) < len(numr): numl.append(0) def rec(pos, brr, brl): global numl, numr, dp if pos < 0: return 1 if dp[pos][brr][brl] != -1: return dp[pos][brr][brl] res = 0 if brr: if brl: res = 3 * rec(pos - 1, brr, brl) else: if numl[pos] == 1: res = rec(pos - 1, brr, brl) else: res += (2 * rec(pos - 1, brr, 0)) res += rec(pos - 1, brr, 1) else: if numr[pos] == 1: if brl: res += (2 * rec(pos - 1, 0, brl)) res += rec(pos - 1, 1, brl) else: if numl[pos] == 1: res += rec(pos - 1, 0, brl) else: res += rec(pos - 1, 0, 0) res += rec(pos - 1, 0, 1) res += rec(pos - 1, 1, 0) else: if brl: res += rec(pos - 1, 0, brl) elif numl[pos] == 0: res += rec(pos - 1, 0, 0) dp[pos][brr][brl] = res % (10**9 + 7) return dp[pos][brr][brl] c = 0 for i in range(len(numl) - 1, -1, -1): if numl[i] == 1: c += rec(i - 1, 1 if i < len(numl) - 1 else 0, 0) break else: c += rec(i - 1, 1 if i < len(numl) - 1 else 0, 1) print(c % (10**9 + 7))
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; 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; // (x の i 桁目) ≦ (y の i 桁目) でなければ無視 if (!j && (xi^yi)) continue; // (1, 1) が現れる前に (0, 1) か (1, 0) が現れたら無視 if (xi&yi) j2 = 1; // (1, 1) が現れたら j2 を更新 if (!l && (xi < Li)) continue; if (xi > Li) l2 = 1; 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 <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #define mkp make_pair #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long ll; const ll MOD=1e9+7; ll L,R; ll dp[61][2][2][2]; void add(ll &a,ll b){ a=(a+b)%MOD; } void mul(ll &a,ll b){ a=a*b%MOD; } int main(){ 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; rep(j,2)rep(k,2)rep(s,2){ rep(y,2)rep(x,2){ if(y==0&&x==1) continue; int nj=j,nk=k,ns=s; ll cost=1; if(lb&&j==0&&x==0) continue; if(rb==0&&k==0&&y==1) continue; if(s==0&&(y==1&&x==0)) continue; if(y==1&&x==1){ if(s==0) ns=1; if(lb==0&&j==0) nj=1; } if(y==1&&x==0){ } if(y==0&&x==0){ if(rb&&k==0) nk=1; } ll val=cost; mul(val,dp[i+1][j][k][s]); add(dp[i][nj][nk][ns],val); } } } ll ans=0; rep(j,2)rep(k,2) add(ans,dp[0][j][k][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<cstdio> #include<algorithm> #include<vector> using namespace std; typedef long long ll; #define rep(i,n) for(int i=0;i<n;i++) const ll mod=1e9+7; ll dp[65][2][2][2]; int main() { ll l,r; scanf("%lld%lld",&l,&r); int n=64; vector<ll> bl(n),br(n); for(int i=0;i<n;i++) { bl[i]=l%2; br[i]=r%2; l/=2; r/=2; } reverse(begin(bl),end(bl)); reverse(begin(br),end(br)); dp[0][0][0][0]=1; rep(i,n)rep(fl,2)rep(fr,2)rep(g,2) { if(dp[i][fl][fr][g]==0)continue; rep(bx,2)rep(by,2) { if(bx==1&&by==0)continue; if(!g&&bx+by==1)continue; if(!fl&&bl[i]&&!bx)continue; if(!fr&&!br[i]&&by)continue; dp[i+1][fl | (bx > bl[i])][fr | (by < br[i])][g | bx | by] += dp[i][fl][fr][g]%=mod; } } ll ans=0; rep(fl,2)rep(fr,2)rep(g,2)ans=(ans+dp[n][fl][fr][g])%mod; 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; typedef long long ll; const ll MOD = 1e9 + 7; const ll INF = 1LL << 60; ll dp[61][2][2]; // 何ケタ目か,L=x or L<x,R=y or y<R int main() { ll L, R; cin >> L >> R; ll ans = 0; for (int n = 0; n <= 60; n++) { ll p = 1LL << n; ll mask = ~(p - 1); if ((L&mask) > p || p > (R&mask)) continue; memset(dp, 0, sizeof(dp)); dp[n][(L&mask) < p][p < (R&mask)] = 1; for (int i = n - 1; i >= 0; i--) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { // 0 0 if (j || ((L >> i) & 1) == 0) { (dp[i][j][k | (((R >> i) & 1) == 1)] += dp[i + 1][j][k]) %= MOD; } // 1 0 if ((j || ((L >> i) & 1) == 0) && (k || ((R >> i) & 1) == 1)) { (dp[i][j][k] += dp[i + 1][j][k]) %= MOD; } // 1 1 if (k || ((R >> i) & 1) == 1) { (dp[i][j | (((L >> i) & 1) == 0)][k] += dp[i + 1][j][k]) %= MOD; } } } } for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) (ans += dp[0][i][j]) %= 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 <bits/stdc++.h> using namespace std; typedef long long ll; ll l, r; const int MOD = 1000000007; bool seen[2][2][2][100]; int cache[2][2][2][100]; //vector<pair<ll, ll>> sols[2][2][2][100]; int dp(bool top, bool bottom, bool same, int bit) { if (bit < 0) { //if (!same) sols[top][bottom][same][(bit + 100)%100].emplace_back(0, 0); return !same; } if (seen[top][bottom][same][bit]) return cache[top][bottom][same][bit]; seen[top][bottom][same][bit] = true; int &ans = cache[top][bottom][same][bit]; //auto &v = sols[top][bottom][same][bit]; ans = 0; const ll mask = 1ll<<bit; // try 1 if (!top || (mask & r)) { ans += dp(top, bottom && (l & mask), false, bit-1); ans %= MOD; //for (auto p : sols[top][bottom && (l & mask)][0][(bit+99)%100]) // v.emplace_back(p.first | mask, p.second); } // try 0 if (!bottom || !(mask & l)) { ans += dp(top && !(r & mask), bottom, same, bit-1); ans %= MOD; //for (auto p : sols[top && !(r & mask)][bottom][same][(bit+99)%100]) { // v.emplace_back(p.first, p.second); //} if (!same && (!top || (mask & r))) { ans += dp(top, bottom, same, bit-1); //for (auto p : sols[top][bottom][same][(bit+99)%100]) { // v.emplace_back(p.first, p.second); //} } ans %= MOD; } return ans; } int main() { cin >> l >> r; cout << dp(true, true, true, 62) << '\n'; //for (auto p : sols[1][1][1][62]) { // ll x = p.first, y = p.first + p.second, z = p.second; // cout << x << ' ' << y << ' ' << z << '\n'; // assert(l <= x && x <= y && y <= r); // assert((y % x) == (y ^ x)); //} 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 FOR(i, a, n) for(ll i = (ll)a; i < (ll)n; i++) #define FORR(i, n) for(ll i = (ll)n - 1LL; i >= 0LL; i--) #define rep(i, n) FOR(i, 0, n) #define ALL(x) (x).begin(), (x).end() using namespace std; using ll = long long; template <typename T> using V = vector<T>; constexpr int Mod = 998244353; constexpr int mod = 1e9 + 7; constexpr ll inf = 1LL << 60; template <typename T> constexpr bool chmax(T &a, const T &b) { if(a >= b) return false; a = b; return true; } template <typename T> constexpr bool chmin(T &a, const T &b) { if(a <= b) return false; a = b; return true; } /*-------------------------------------------*/ ll L, R; ll dp[2][2][2][65]; ll f(int d, int S, int l, int r) { if(d == -1) return S; if(dp[S][l][r][d] >= 0) return dp[S][l][r][d]; ll ret = 0; rep(a, 2) rep(b, 2) { if(!a && l && (L >> d & 1)) continue; if(b && r && !(R >> d & 1)) continue; if(a > b) continue; if(!S && a != b) continue; int Nl = l && (L >> d & 1) == a; int Nr = r && (R >> d & 1) == b; int NS = S || a == 1; ret += f(d - 1, NS, Nl, Nr); } return dp[S][l][r][d] = ret % mod; } int main() { cin.tie(0); ios::sync_with_stdio(0); cin >> L >> R; memset(dp, -1, sizeof(dp)); cout << f(61, 0, 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
/* 如何动态维护第k大元素? */ #include<bits/stdc++.h> #define ll long long #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; const ll MAXN=2e5+5; const ll INF=1e18; const ll MOD=1e9+7; ll l,r,numx[60],numy[60],cntx,cnty; int dp[60][2][2][2]; ll add(ll x,ll y) { ll ret=x+y; if(ret>=MOD) { return ret-MOD; } return ret; } void getnum() { while(l) { numx[cntx++]=l%2; l/=2; } while(r) { numy[cnty++]=r%2; r/=2; } } ll dfs(ll step,ll fx,ll fy,ll fz) { if(step==-1) { return 1; } if(dp[step][fx][fy][fz]!=-1) { return dp[step][fx][fy][fz]; } ll ret=0; //0 0 if(fx||numx[step]==0) { ret=add(ret,dfs(step-1,fx,numy[step]==1?1:fy,fz)); } //0 1 if((fx||numx[step]==0)&&(fy||numy[step]==1)&&(fz)) { ret=add(ret,dfs(step-1,fx,fy,fz)); } if(fy||numy[step]==1) { ret=add(ret,dfs(step-1,numx[step]==0?1:fx,fy,1)); } dp[step][fx][fy][fz]=ret; return ret; } void solve() { memset(dp,-1,sizeof(dp)); getnum(); printf("%lld\n",dfs(59,0,0,0)); } int main() { while(~scanf("%lld %lld",&l,&r)) { solve(); } }
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; const int M = 1000000007; long long modexp(int x, long long e, int m) { long long ans = 1, p = x % m; while (e > 0) { if (e % 2 != 0) ans = (ans * p) % m; p = (p * p) % m; e >>= 1; } return ans; } long long L, R; long long calc(long long lx, long long ly, int i, int j) { long long rx = lx + (1LL<<i), ry = ly + (1LL<<j); if (ry <= lx || rx <= L || ry <= L || R <= lx || R <= ly) return 0; if (L <= lx && rx <= R) if (L <= ly && ry <= R) { if (i >= j) { if (((lx >> i) & (ly >> i)) == (lx >> i)) { int p = __builtin_popcountll((ly>>j)&((1LL<<(i-j))-1)); return modexp(3, j, M) * modexp(2, p, M) % M; } else return 0; } else { if (((lx >> j) & (ly >> j)) == (lx >> j)) { int p = __builtin_popcountll((lx>>i)&((1LL<<(j-i))-1)); return modexp(3, i, M) * modexp(2, j-i-p, M) % M; } else return 0; } } else { long long my = ly + (1LL<<(j-1)); return (calc(lx, ly, i, j-1) + calc(lx, my, i, j-1)) % M; } else { long long mx = lx + (1LL<<(i-1)); return (calc(lx, ly, i-1, j) + calc(mx, ly, i-1, j)) % M; } } int main() { cin >> L >> R; R++; long long ans = 0; for (int k = 0; k < 60; k++) ans += calc(1LL<<k, 1LL<<k, k, k); cout << ans % M << 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> #include <vector> #include <map> #include <algorithm> #include <queue> #include <string> #define REP(i,a,b) for (int i = a; i < (b); i++) #define sz(t) int(t.size()) #define INF 1000000000 const int DIV = 1000000000 + 7; using namespace std; typedef long long ll; int dp[62][2][2][2]; int main(void) { 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; // cout << lb << " " << rb << endl; // s means most significant bit is the same or unknown // j means x is greater than l // k means y is less than r REP(j, 0, 2) REP(k, 0, 2) REP(s, 0, 2) { int pre = dp[i + 1][j][k][s]; REP(x, 0, 2) REP(y, 0, 2) { int ni = i, nj = j, nk = k, ns = s; if (x && !y) continue; if (!s && x != y) continue; if (!s && x&y) ns = 1; if (!j && lb && !x) continue; if (!j && !lb && x) nj = 1; if (!k && !rb && y) continue; if (!k && rb && !y) nk = 1; dp[ni][nj][nk][ns] += pre; dp[ni][nj][nk][ns] %= DIV; } } } ll ans = 0; REP(j, 0, 2) REP(k, 0, 2) REP(s, 0, 2) { ans += dp[0][j][k][s]; ans %= DIV; } 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
from functools import * MOD=10**9+7 @lru_cache(maxsize=None) def solve(L,R): if L > R: return 0 if L == 1: return (1 + solve(2, R)) % MOD return (solve(L//2,(R-1)//2) + solve((L+1)//2,R//2) + solve((L+1)//2,(R-1)//2)) % MOD print(solve(*map(int,input().split())))
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; 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; int Ri = R>>(M-1-i)&1; 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; if (!j && (xi^yi)) continue; if (xi&yi) j2 = 1; if (!l && (xi < Li)) continue; if (xi > Li) l2 = 1; 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 <iostream> #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; typedef long long LL; const int MO=1e9+7; const int MB=65; // x<=y x^y==y%x // highbit() Most Significant Bit // highbit(x) != highbit(y) // x^y>x && x%y<x no solution // so highbit(x) == highbit(y) // so floor(y/x) == 1 // x^y==y%x -> x^y == y-x // for y-x // i-th bit // 1-1 0-0 = 0 -> xor same // 1-0 = 1 -> xor same // 0-1 change higher bit! impossible! void add(int &x,int y) { x+=y; if(x>=MO) x-=MO; } int dp[MB][2][2][2]; // dp[i-th digit][upperbound][lowerbound][leading zero] LL L,R; int getdp(int d,int llim,int rlim,int lead) { if(d==-1) return 1; int &ans=dp[d][llim][rlim][lead]; if(ans!=-1) return ans; ans=0; for(int i=0;i<2;i++) for(int j=0;j<2;j++) { if(i==0&&j==1) continue; if(lead&&(i!=j)) continue; int l=((L>>d)&1); int r=((R>>d)&1); if(llim&&j<l) continue; if(rlim&&i>r) continue; add(ans,getdp(d-1,llim&&l==j,rlim&&r==i,lead&&!i&&!j)); } return ans; } int main() { cin>>L>>R; memset(dp,-1,sizeof(dp)); cout<<getdp(61,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; #define ll long long namespace io{ const int l=1<<20; char buf[l],*s,*t; inline char gc(){ if(s==t){ t=(s=buf)+fread(buf,1,l,stdin); return s==t?EOF:*s++; } return *s++; } char c; template<class IT>inline void gi(IT &x){ x=0;c=gc();while(c<'0'||c>'9')c=gc(); while('0'<=c&&c<='9'){x=(x<<1)+(x<<3)+(c^'0');c=gc();} } }; using io::gi; template<class IT>inline void cmin(IT &a,IT b){if(b<a)a=b;} template<class IT>inline void cmax(IT &a,IT b){if(a<b)a=b;} const int p=1000000007; const int N=200005; int dp[64][2][2]; inline void upd(int &a,int b){a=a+b<p?a+b:a+b-p;} inline int solve(ll l,ll r){ int s=0,t=0,n,i,a,b,c,d,x,y,ans=0; while((1ll<<s)<=l)++s; while((1ll<<t)<=r)++t; // printf("l=%lld r=%lld s=%d t=%d\n",l,r,s,t); for(n=1;n<=s;++n){ memset(dp,0,sizeof(dp)); dp[n-1][n!=s][n!=t]=1; for(i=n-2;~i;--i){ x=l>>i&1ll; y=r>>i&1ll; for(a=0;a<2;++a)for(b=0;b<2;++b)if(dp[i+1][a][b]){ for(c=0;c<2;++c)for(d=c;d<2;++d){ if((a||c<=x)&&(b||d<=y))upd(dp[i][a|(c<x)][b|(d<y)],dp[i+1][a][b]); } } } // printf("%d\n",n); // for(i=n-1;~i;--i)printf("%d %d %d %d\n",dp[i][0][0],dp[i][0][1],dp[i][1][0],dp[i][1][1]); for(a=0;a<2;++a)for(b=0;b<2;++b)upd(ans,dp[0][a][b]); } // printf("%d\n",ans); return ans; } int main(){ ll l,r; scanf("%lld%lld",&l,&r); printf("%d",(solve(r,r)-solve(l-1ll,r)+p)%p); 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 <algorithm> using namespace std; const int maxd = 64; const int mod = 1e9+7; int a[maxd],b[maxd]; long long dp[maxd][2][2][2][2],n,m; long long dfs(int p,int l,int r,int f,int t) { //位数,是否填过小于m的值,是否填过大于n的值,是否填过a<b,最高位是否都为1 if(p < 0) return 1; long long &ans = dp[p][l][r][f][t]; // 记忆化搜索 if(ans) return ans; for(int i=0;i<2;i++) // y for(int j=0;j<2;j++) // x { int nl = l,nr = r,nf = f,nt = t; if(!nt && i && i!=j) continue; if((j&i)!=j) continue; if(!nl && j < a[p]) continue; // 如果值小于n了 if(!nr && i > b[p]) continue; // 如果值大于m了 if(!nf && j > i) continue; if(j > a[p]) nl = 1; if(i < b[p]) nr = 1; if(j) nt = 1; if(i > j) nf = 1; ans += dfs(p-1,nl,nr,nf,nt); ans %= mod; } return ans; } int main() { // freopen("a.in","r",stdin); // freopen("k.out","w",stdout); scanf("%lld %lld",&n,&m); for(int i=0;i<=63;i++) a[i] = (n>>i)&1 , b[i] = (m>>i)&1; long long ans = dfs(63,0,0,0,0); printf("%lld",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()) ans = 0 mod = 10 ** 9 + 7 for d in range(62): # 最上位 bit # dp[n] := n 桁目まで見た時に条件を満たすものが何個あるか # dp_small[n] := n 桁目まで見た時に、l と x が n 桁目まで一致していて条件を満たすものが何個あるか l = max(1<<d, L) r = min((2<<d)-1, R) if l > r: continue dp_small = [0] * (d+2) dp_ok = [0] * (d+2) dp_large = [0] * (d+2) dp_small_large = [0] * (d+2) dp_small_large[0] = 1 for n in range(1, d+2): i = d-n+1 dp_ok[n] = dp_ok[n-1] * 3 \ + (dp_small[n-1] if l>>i&1==0 else 0) \ + (dp_large[n-1] if r>>i&1 else 0) dp_small[n] = dp_small[n-1] * (1 if l>>i&1 else 2) \ + (dp_small_large[n-1] if l>>i&1 < r>>i&1 else 0) dp_large[n] = dp_large[n-1] * (2 if r>>i&1 else 1) \ + (dp_small_large[n-1] if l>>i&1 < r>>i&1 else 0) dp_small_large[n] = dp_small_large[n-1] if l>>i&1 <= r>>i&1 else 0 a = dp_ok[d+1] + dp_large[d+1] + dp_small[d+1] + dp_small_large[d+1] ans += a # print(f"d={d},a={a},l={l},r={r}") # print(f"dp_ok={dp_ok}") # print(f"dp_small={dp_small}") # print(f"dp_large={dp_large}") # print(f"dp_small_large={dp_small_large}") # print() 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
L, R = map(int, input().split()) dp = [[[[0]*2 for _ in range(2)] for _ in range(2)] for _ in range(65)] dp[0][0][0][0] = 1 MOD = 10**9+7 for i in range(64): Li = (L>>(63-i))&1 Ri = (R>>(63-i))&1 for j in range(2): for k in range(2): for l in range(2): for x in (range(2) if j else range(Li, 2)): for y in range(2 if k else Ri+1): if x==1 and y==0: continue if l==0 and x==0 and y==1: continue dp[i+1][j|(x>Li)][k|(y<Ri)][l|(x==1 and y==1)] += dp[i][j][k][l] dp[i+1][j|(x>Li)][k|(y<Ri)][l|(x==1 and y==1)] %= MOD ans = 0 for i in range(2): for j in range(2): for k in range(2): ans += dp[-1][i][j][k] 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; typedef long long ll; const int mod=1e9+7; ll L,R; ll f[64][2][2][2][2]; int l[100],r[100]; ll dfs(int x,int fl,int fr,int s,int ht) { if(x<0)return 1; ll &ans=f[x][fl][fr][s][ht]; if(ans)return ans; for(int i=0;i<2;i++) { for(int j=0;j<2;j++){ int tl=fl,tr=fr,ts=s,tt=ht; if(!tt&&i)if(i!=j)continue; if((j&i)!=j)continue; if(!tl&&j<l[x])continue; if(!tr&&i>r[x])continue; if(j>l[x])tl=1;if(i<r[x])tr=1;if(j<i)ts=1;if(j)tt=1; ans+=dfs(x-1,tl,tr,ts,tt); } } ans%=mod; return ans; } int main(){ ios::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>L>>R; for(int i=63;i>=0;i--) { l[i]=(bool)((1ll<<i)&L);r[i]=(bool((1ll<<i)&R)); } cout<<dfs(63,0,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
from functools import lru_cache P = 10**9+7 @lru_cache(maxsize=None) def subcalc(l, r): if l > r: return 0 if r == 0: return 1 aa, bb = l.bit_length(), r.bit_length() if aa == bb: return subcalc(l-(1<<aa-1), r-(1<<bb-1)) if (r & (r+1) == 0) and (l == 0): return pow(3, r.bit_length(), P) return (subcalc(l, r-(1<<bb-1)) + subcalc(l, (1<<bb-1)-1) + subcalc(0, r-(1<<bb-1))) % P @lru_cache(maxsize=None) def calc(L, R): if L > R: return 0 a = L.bit_length() b = R.bit_length() if b > a: return (calc(L, (1<<b-1)-1) + calc(1<<b-1, R)) % P a = 1 << L.bit_length() - 1 if L else 0 return subcalc(L-a, R-a) L, R = map(int, input().split()) print(calc(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 <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 < 2; fl++) { for (int fr = 0; fr < 2; fr++) { for (int sb = 0; sb < 2; sb++) { for (int cl = 0; cl < 2; cl++) { for (int cr = 0; cr < 2; cr++) { if (cl <= cr && !(sb && (cl < cr)) && !(fl && (cl < bl)) && !(fr && (cr > br))) { addm(dp[i][fl][fr][sb], dp[i-1][fl&&cl==bl][fr&&cr==br][sb&&cl!=1]); } } } } } } } 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
/*Author: Satyajeet Singh, Delhi Technological University*/ import java.io.*; import java.util.*; import java.text.*; import java.lang.*; import java.math.*; public class Main{ /*********************************************Constants******************************************/ static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static long mod=(long)1e9+7; static long mod1=998244353; static boolean sieve[]; static ArrayList<Integer> primes; static ArrayList<Long> factorial; static HashSet<Pair> graph[]; static boolean oj = System.getProperty("ONLINE_JUDGE") != null; /****************************************Solutions Begins***************************************/ static ArrayList<Long> list,ls; static long dp[][][][][][]; static long dfs(int i,int x,int y,int f,int e,int f2){ if(i==list.size()){ if(f<=1&&e==0&&f2<=1){ return 1; } else{ return 0; } } if(dp[i][x][y][f][e][f2]!=-1){ return dp[i][x][y][f][e][f2]; } long ans=0; //0,0 int aa=0; if(ls.get(i)==1)aa=2; else aa=f2; if(list.get(i)==1){ ans=(ans+dfs(i+1,0,0,0,e,aa))%mod; } else{ ans=(ans+dfs(i+1,0,0,f,e,aa))%mod; } //1,0 if(list.get(i)==1){ ans=(ans+dfs(i+1,1,0,f,1,aa))%mod; } else{ ans=(ans+dfs(i+1,1,0,2,1,aa))%mod; } //1,1 if(ls.get(i)==1)aa=f2; else aa=0; if(list.get(i)==1){ ans=(ans+dfs(i+1,1,1,f,0,aa))%mod; } else{ ans=(ans+dfs(i+1,1,1,2,0,aa))%mod; } dp[i][x][y][f][e][f2]=ans; return ans; } static long fn(long a,long b){ list=new ArrayList<>(); long k=a; while(k!=0){ list.add(k%2); k/=2; } ls=new ArrayList<>(); k=b; while(k!=0){ ls.add(k%2); k/=2; } while(ls.size()!=list.size())ls.add(0l); // debug(list); dp=new long[list.size()][2][2][3][2][3]; for(long arr[][][][][]:dp) for(long ar1[][][][]:arr) for(long ar2[][][]:ar1) for(long aa[][]:ar2) for(long hh[]:aa) Arrays.fill(hh,-1); long ans=dfs(0,0,0,1,0,1); return ans; } public static void main (String[] args) throws Exception { String st[]=br.readLine().split(" "); long a=pl(st[0]); long b=pl(st[1]); // debug(b,a); // debug(fn(4)); //debug(fn(b),fn(a-1)); long ans=fn(b,a); out.println(ans); /****************************************Solutions Ends**************************************************/ out.flush(); out.close(); } /****************************************Template Begins************************************************/ static String[] nl() throws Exception{ return br.readLine().split(" "); } static String[] nls() throws Exception{ return br.readLine().split(""); } static int pi(String str) { return Integer.parseInt(str); } static long pl(String str){ return Long.parseLong(str); } static double pd(String str){ return Double.parseDouble(str); } /***************************************Precision Printing**********************************************/ static void printPrecision(double d){ DecimalFormat ft = new DecimalFormat("0.00000000000000000"); out.println(ft.format(d)); } /**************************************Bit Manipulation**************************************************/ static void printMask(long mask){ System.out.println(Long.toBinaryString(mask)); } static int countBit(int mask){ int ans=0; while(mask!=0){ if(mask%2==1){ ans++; } mask/=2; } return ans; } /******************************************Graph*********************************************************/ static void Makegraph(int n){ graph=new HashSet[n]; for(int i=0;i<n;i++){ graph[i]=new HashSet<>(); } } static void addEdge(int a,int b,int c){ graph[a].add(new Pair(b,c)); } /*********************************************PAIR********************************************************/ static class PairComp implements Comparator<Pair>{ public int compare(Pair p1,Pair p2){ if(p1.u!=p2.u){ return p1.u-p2.u; } else{ return p1.u-p2.u; } } } static class Pair implements Comparable<Pair> { int u; int v; int index=-1; public Pair(int u, int v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pair other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /******************************************Long Pair*************************************************/ static class PairCompL implements Comparator<Pairl>{ public int compare(Pairl p1,Pairl p2){ long aa=p2.v-p1.v; if(aa<0){ return -1; } else if(aa>0){ return 1; } else{ return 0; } } } static class Pairl implements Comparable<Pairl> { long u; long v; int index=-1; public Pairl(long u, long v) { this.u = u; this.v = v; } public int hashCode() { int hu = (int) (u ^ (u >>> 32)); int hv = (int) (v ^ (v >>> 32)); return 31 * hu + hv; } public boolean equals(Object o) { Pair other = (Pair) o; return u == other.u && v == other.v; } public int compareTo(Pairl other) { if(index!=other.index) return Long.compare(index, other.index); return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } /*****************************************DEBUG***********************************************************/ public static void debug(Object... o) { if(!oj) System.out.println(Arrays.deepToString(o)); } /************************************MODULAR EXPONENTIATION***********************************************/ static long modulo(long a,long b,long c) { long x=1; long y=a; while(b > 0){ if(b%2 == 1){ x=(x*y)%c; } y = (y*y)%c; // squaring the base b /= 2; } return x%c; } /********************************************GCD**********************************************************/ static long gcd(long x, long y) { if(x==0) return y; if(y==0) return x; long r=0, a, b; a = (x > y) ? x : y; // a is greater number b = (x < y) ? x : y; // b is smaller number r = b; while(a % b != 0) { r = a % b; a = b; b = r; } return r; } /******************************************SIEVE**********************************************************/ static void sieveMake(int n){ sieve=new boolean[n]; Arrays.fill(sieve,true); sieve[0]=false; sieve[1]=false; for(int i=2;i*i<n;i++){ if(sieve[i]){ for(int j=i*i;j<n;j+=i){ sieve[j]=false; } } } primes=new ArrayList<Integer>(); for(int i=0;i<n;i++){ if(sieve[i]){ primes.add(i); } } } /********************************************End***********************************************************/ }
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
MOD = 10**9 + 7 l, r = map(int, input().split()) def func(x, y): if y == 0: return 1 dp = [[0 for _ in range(6)] for _ in range(61)] dp[60][0] = 1 for i in range(59, -1, -1): if (y>>i) & 1 == 0 and (x>>i) & 1 == 0: dp[i][0] = dp[i+1][0] dp[i][1] = dp[i+1][1] dp[i][2] = dp[i+1][2] dp[i][3] = (dp[i+1][3]*2) % MOD dp[i][4] = dp[i+1][4] dp[i][5] = (dp[i+1][1] + dp[i+1][3] + dp[i+1][5]*3) % MOD elif (y>>i) & 1 == 1 and (x>>i) & 1 == 1: dp[i][0] = 0 dp[i][1] = 0 dp[i][2] = (dp[i+1][0] + dp[i+1][2]) % MOD dp[i][3] = (dp[i+1][1] + dp[i+1][3]) % MOD dp[i][4] = (dp[i+1][4]*2) % MOD dp[i][5] = (dp[i+1][4] + dp[i+1][5]*3) % MOD elif (y>>i) & 1 == 1 and (x>>i) & 1 == 0: dp[i][0] = 0 dp[i][1] = (dp[i+1][0] + dp[i+1][1]) % MOD dp[i][2] = dp[i+1][2] dp[i][3] = (dp[i+1][2] + dp[i+1][3]*2) % MOD dp[i][4] = (dp[i+1][0] + dp[i+1][2] + dp[i+1][4]*2) % MOD dp[i][5] = (dp[i+1][1] + dp[i+1][3] + dp[i+1][4] + dp[i+1][5]*3) % MOD elif (y>>i) & 1 == 0 and (x>>i) & 1 == 1: dp[i][0] = 0 dp[i][1] = 0 dp[i][2] = 0 dp[i][3] = (dp[i+1][1] + dp[i+1][3]) % MOD dp[i][4] = dp[i+1][4] dp[i][5] = (dp[i+1][5]*3) % MOD return (sum(dp[0]))%MOD print(func(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; typedef long long ll; const int mod=1e9+7; ll L,R; ll f[64][2][2][2][2]; int l[100],r[100]; ll dfs(int x,int fl,int fr,int s,int ht) { if(x<0)return 1; // x:第x位 (二进制转十进制下权值是1的为第0位) // fl: y之前是否有填过小于 R的某一位的数字 例如:R:11000 y:10??? fl即为1 表示后面数字可以往大了填 // fr: x之前是否有填过大于 L的某一位的数字 类似fl // s: 之前x是否有某一位 < y的某一位 如果有,后面的位数就没有相对大小限制了 (确保 x<=y) //ht:确保 x,y 最高位一样都为1 (满足推导的要求) ll &ans=f[x][fl][fr][s][ht]; if(ans)return ans; for(int i=0;i<2;i++)//y { for(int j=0;j<2;j++)//x { int tl=fl,tr=fr,ts=s,tt=ht; if(!tt&&i)if(i!=j)continue; if((j&i)!=j)continue; if(!tl&&j<l[x])continue; if(!tr&&i>r[x])continue; if(!ts&&j>i)continue; if(j>l[x])tl=1;if(i<r[x])tr=1;if(j<i)ts=1;if(j)tt=1; ans+=dfs(x-1,tl,tr,ts,tt); } } ans%=mod; return ans; } int main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>L>>R; for(int i=63;i>=0;i--) { l[i]=(bool)((1ll<<i)&L);r[i]=(bool((1ll<<i)&R)); } cout<<dfs(63,0,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 int long long using namespace std; const int MOD=1e9+7; int L,R,Mx[101],Mi[101],dp[101][2][2][2]; int DFS(int pos,bool limit,bool lead,bool is) { if(pos==0)return !lead; if(dp[pos][limit][lead][is]!=-1)return dp[pos][limit][lead][is]; int up=limit?Mx[pos]:1; int tmp=0; if(is||Mi[pos]==0)tmp+=DFS(pos-1,limit&&Mx[pos]==0,lead,is); if(up){ tmp+=DFS(pos-1,limit,false,is||(Mi[pos]==0)); if(!lead&&(is||Mi[pos]==0))tmp+=DFS(pos-1,limit&&Mx[pos]==1,false,is); } tmp%=MOD; dp[pos][limit][lead][is]=tmp; return tmp; } int solve() { while(R)Mx[++Mx[0]]=R&1,R=R>>1; while(L)Mi[++Mi[0]]=L&1,L=L>>1; // for(int i=1; i<=Mx[0]; i++)cout<<Mx[i]<<" "; // cout<<endl; // for(int i=1; i<=Mi[0]; i++)cout<<Mi[i]<<" "; // cout<<endl; return DFS(Mx[0],true,true,false); } signed main() { memset(dp,-1,sizeof(dp)); scanf("%lld %lld",&L,&R); cout<<solve(); // for(int i=1; i<=Mx[0]; i++)cout<<dp[i][0]<<" "<<dp[i][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
import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) from functools import lru_cache MOD = 10 ** 9 + 7 """ 桁数が異なると矛盾 したがって商が1であることが必要 すると、xがyのsubsetであることが必要 一番上の桁を含むsubsetがx """ @lru_cache() def F_naive(L,R): # ret = 0 ret += F((L+1)//2,(R+1)//2) # 2x,2y answer = 0 for x in range(L,R+1): for y in range(x,R+1): if x.bit_length() == y.bit_length() and (x^y)+x == y: answer += 1 return answer @lru_cache() def F(L,R): # x subset y かつ 2x > y if L < 0: L = 0 if R < L: return 0 if R == 0: return 0 ret = 0 # 下一桁が0,0ととる場合 ret += F((L+1)//2, R//2) # 下一桁が0,1ととる場合 # 2(2x) > (2y+1) iff 2x > y ret += F((L+1)//2,(R-1)//2) # 下一桁が1,0ととる場合 # これはsubsetにならない # 下一桁が1,1ととる場合 # 2(2x+1) > 2y+1 iff 2x >= y ret += G(L//2,(R-1)//2) return ret @lru_cache() def G(L,R): # x subset y かつ 2x >= y if L < 0: L = 0 if R < L: return 0 if R == 0: return 1 ret = 0 # 下一桁が0,0ととる場合 ret += G((L+1)//2, R//2) # 下一桁が0,1ととる場合 # 2(2x) >= (2y+1) iff 2x > y ret += F((L+1)//2,(R-1)//2) # 下一桁が1,0ととる場合 # これはsubsetにならない # 下一桁が1,1ととる場合 # 2(2x+1) >= 2y+1 iff 2x >= y ret += G(L//2,(R-1)//2) return ret L,R = map(int,input().split()) answer = F(L,R) % MOD print(answer)
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; typedef long long LL; const int OO = 1e9; const int MOD = 1e9 + 7; LL l, r; LL dp[64][2][2][2]; LL dfs(int t, int x, int y, int z) { if (t == -1) return 1; LL& ans = dp[t][x][y][z]; if (ans != -1) return ans; ans = 0; for (int a = 0; a < 2; ++a) { for (int b = 0; b <= a; ++b) { int t1 = x, t2 = y, t3 = z; if (x == 1 && (1LL << t) * a > ((1LL << t) & r)) continue; if (y == 1 && (1LL << t) * b < ((1LL << t) & l)) continue; if (z == 0 && a != b) continue; if ((1LL << t) * a < ((1LL << t) & r)) x = 0; if ((1LL << t) * b > ((1LL << t) & l)) y = 0; if (z == 0 && a == b && a == 1) z = 1; ans += dfs(t - 1, x, y, z); ans %= MOD; x = t1, y = t2, z = t3; } } return ans; } void solve() { scanf("%lld%lld", &l, &r); memset(dp, -1, sizeof(dp)); printf("%lld\n", dfs(63, 1, 1, 0)); } int32_t main() { // #ifndef ONLINE_JUDGE // freopen("input.txt", "r", stdin); // clock_t begin = clock(); // #endif solve(); // #ifndef ONLINE_JUDGE // clock_t end = clock(); // printf("ELAPSED TIME: %f\n", (double) (end - begin) / CLOCKS_PER_SEC); // #endif 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 pb push_back #define ll long long #define ii pair<int,int> #define st first #define nd second #define N 200005 #define inf 1000000000 #define MOD 1000000007 #define sz(x) ((int)x.size()) #define umax(x,y) x=max(x,y) #define umin(x,y) x=min(x,y) using namespace std; int dp[62][2][2][2]; int mul(int x,int y) { return (ll)x*y%MOD; } int add(int x,int y) { x+=y; if(x>=MOD) x-=MOD; if(x<0) x+=MOD; return x; } int solve(ll l,ll r) { dp[61][0][0][0]=1; for(int i=61;i>0;i--) { for(int a=0;a<2;a++) { for(int b=0;b<2;b++) { for(int c=0;c<2;c++) { // put 1,1 if(a || (r>>(i-1))&1) dp[i-1][a][b || !((l>>(i-1))&1)][1]=add(dp[i-1][a][b || !((l>>(i-1))&1)][1],dp[i][a][b][c]); // put 1,0 if(c && (b || !((l>>(i-1))&1)) && (a || (r>>(i-1))&1)) dp[i-1][a][b][c]=add(dp[i-1][a][b][c],dp[i][a][b][c]); // put 0,0 if((b || !((l>>(i-1))&1))) dp[i-1][(a || (r>>(i-1))&1)][b][c]=add(dp[i-1][(a || (r>>(i-1))&1)][b][c],dp[i][a][b][c]); } } } } int res=0; for(int a=0;a<2;a++) { for(int b=0;b<2;b++) { res=add(res,dp[0][a][b][1]); } } return res; } int main(int arg,char* argv[]) { ll l,r; cin>>l>>r; cout<<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
#include <bits/stdc++.h> using namespace std; #define finish(x) return cout << x << endl, 0 #define ll long long ll l, r; int dp[61][2][2][2], mod = 1e9 + 7; int solve(int bit, bool a, bool b, bool s){ if(bit == -1) return 1; int &ret = dp[bit][a][b][s]; if(ret != -1) return ret; ret = 0; // 0 0 if(a || ((l >> bit) & 1) == 0) ret += solve(bit - 1, a, b || ((r >> bit) & 1), s); if(ret >= mod) ret -= mod; // 0 1 if(s && (a || ((l >> bit) & 1) == 0) && (b || ((r >> bit) & 1) == 1)) ret += solve(bit - 1, a, b, s); if(ret >= mod) ret -= mod; // 1 1 if(b || ((r >> bit) & 1) == 1) ret += solve(bit - 1, a || ((l >> bit) & 1) == 0, b, 1); if(ret >= mod) ret -= mod; return ret; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); memset(dp, -1, sizeof dp); cin >> l >> r; cout << solve(60, 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> #include <iostream> using namespace std; typedef long long ll; ll L, R; ll mod = 1000000007; ll dp[63][2][2][2]; //digit, deviated from l, deviated from r, initial bit has been set ll solve(int dig, int x, int y, int firstBit){ if (dig == -1) return 1LL; if (dp[dig][x][y][firstBit] != -1) return dp[dig][x][y][firstBit]; dp[dig][x][y][firstBit] = 0; if (x == 1 || !(1LL<<dig & L)){ // x 0, y 0 dp[dig][x][y][firstBit] += (1LL<<dig & R) ? solve(dig-1, x, 1, firstBit) : solve(dig-1, x, y, firstBit); } if ((x == 1 || !(1LL<<dig & L)) && (y == 1 || (1LL<<dig & R)) && firstBit){ // x 0, y 1 dp[dig][x][y][firstBit] += solve(dig-1, x, y, firstBit); } if (y == 1 || (1LL<<dig & R)){ // x 1, y 1 dp[dig][x][y][firstBit] += !(1LL<<dig & L) ? solve(dig-1, 1, y, 1) : solve(dig-1, x, y, 1); } dp[dig][x][y][firstBit] %= mod; return dp[dig][x][y][firstBit]; } int main() { cin >> L >> R; memset(dp, -1, sizeof(dp)); cout << solve(62, 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 <bits/stdc++.h> using namespace std; using ll = long long; const int mod = 1000*1000*1000 + 7; int memo[60][2][2][2]; ll L, R; void add_self(int& a, int b) { a += b; if(a >= mod) a -= mod; } int dp(int pos, int flagX, int flagY, int flagZ) { if(pos == -1) { return 1; } int& ans = memo[pos][flagX][flagY][flagZ]; if(ans != -1) { return ans; } ans = 0; // x 0, y 0 if(flagX || (L>>pos & 1) == 0) { add_self(ans, dp(pos-1, flagX, (R>>pos & 1) ? 1 : flagY, flagZ)); } // x 0, y 1 if((flagX || (L>>pos & 1) == 0) && (flagY || (R>>pos & 1)) && flagZ) { add_self(ans, dp(pos-1, flagX, flagY, flagZ)); } // x 1, y 1 if(flagY || (R>>pos & 1)) { add_self(ans, dp(pos-1, (L>>pos & 1) == 0 ? 1 : flagX, flagY, 1)); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); memset(memo, -1, sizeof memo); cin >> L >> R; cout << dp(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 f first #define s second #define pb push_back #define mp make_pair using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<long long, long long> pll; const int N = (int)2e5 + 123, inf = 1e9, mod = 1e9 + 7; const ll INF = 1e18; ll l, r, p3[100], ans; ll solvex(ll bit, ll x, ll l){ if(x >= l) return p3[bit + 1]; if(x + (1ll << (bit + 1)) - 1 < l) return 0; return (solvex(bit - 1, x, l) * 2 + solvex(bit - 1, x | (1ll << bit), l)) % mod; } ll solvey(ll bit, ll y, ll r){ if(y + (1ll << (bit + 1)) - 1 <= r) return p3[bit + 1]; if(y > r) return 0; return (solvey(bit - 1, y, r) + solvey(bit - 1, y | (1ll << bit), r) * 2) % mod; } ll solve(ll bit, ll x, ll y, ll l, ll r){ if(x + (1ll << (bit + 1)) - 1 < l) return 0; if(y > r) return 0; if(y + (1ll << (bit + 1)) - 1 <= r) return solvex(bit, x, l); if(x >= l) return solvey(bit, y, r); return (solve(bit - 1, x, y, l, r) + solve(bit - 1, x , y | (1ll << bit), l, r) + solve(bit - 1, x | (1ll << bit), y | (1ll << bit), l, r)) % mod; } int main() { p3[0] = 1; for(int i = 1; i < 100; i++) p3[i] = p3[i - 1] * 3 % mod; cin >> l >> r; for(int i = 0; i <= 60; i++) ans = (ans + solve(i - 1, (1ll << i), (1ll << i), l, r)) % 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
L,R = map(int,input().split()) dp = [] for i in range(62): nowA = [] for i in range(2): nowB = [] for i in range(2): nowC = [] for i in range(2): nowC.append(0) nowB.append(nowC) nowA.append(nowB) dp.append(nowA) dp[0][0][0][0] = 1 #print (dp[3][0][0][0]) for i in range(61): i += 1 keta = 62-i lb = (L >> (keta-1)) % 2 rb = (R >> (keta-1)) % 2 for j in range(2): for k in range(2): for m in range(2): pre = dp[i-1][j][k][m] for x in range(2): for y in range(2): nj = j nk = k nm = m if x == 1 and y == 0: continue; if x != y and m == 0: continue; if j == 0 and lb == 1 and x == 0: continue; if k == 0 and rb == 0 and y == 1: continue; if j == 0 and lb == 0 and x == 1: nj = 1 if k == 0 and rb == 1 and y == 0: nk = 1 if m == 0 and x == 1 and y == 1: nm = 1 dp[i][nj][nk][nm] += dp[i-1][j][k][m] dp[i][nj][nk][nm] %= 10 ** 9 +7 ans = 0 for j in range(2): for k in range(2): ans += dp[-1][j][k][1] print (ans % (10 ** 9 + 7))
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 INF 2e9 #define ll long long #define MINUS(a) memset(a,0xff,sizeof(a)) ll L, R; ll mo = 1000000007; ll dp[2][2][2][61]; /* dp(d, msb, l, r)内のret: x, yのd桁目のより下の値の組み合わせ数 msb: d桁目より上ですでにMSBとなる桁があったかどうか l : d桁目より上を決めた段階で、L<=xか r : d桁目より上を決めた段階で、y<=Rか 上を前提にcom()でx, yのd桁目として0, 1を取りうるか見ていく。 */ ll com(int d, int S, int LT, int RT){ if(d == -1){ return S == 1; } /* メモ */ if(dp[S][LT][RT][d]>=0){ return dp[S][LT][RT][d]; } ll ret = 0; /* aがxのd桁目、bがyのd桁目 */ int a, b; for(int a=0; a<2; a++){ for(int b=0; b<2; b++){ /* x>=Lになっているか */ if(a==0 && LT && (L&(1LL<<d))>0){ continue; } /* y<=Rになっているか */ if(b==1 && RT && (R&(1LL<<d))==0){ continue; } /* y-xで繰り下がりは起こさせない */ if(a>b){ continue; } /* MSBがなくてa==1かつb==1はだめ */ if(S==0 && a != b){ continue; } int NLT = LT && (((L>>d)&1)==a); int NRT = RT && (((R>>d)&1)==b); int NS = S || (a==1 && b==1); ret += com(d-1, NS, NLT, NRT); } } return dp[S][LT][RT][d] = ret % mo; } int main(){ cin>>L>>R; MINUS(dp); cout<<com(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 <cstdio> #include <algorithm> #include <cstdlib> #include <cmath> #include <climits> #include <cstring> #include <string> #include <vector> #include <queue> #include <numeric> #include <functional> #include <set> #include <map> #include <unordered_map> #include <unordered_set> #include <memory> #include <thread> #include <tuple> #include <limits> using namespace std; constexpr int mod = 1000000007; map<tuple<int, long long, long long>, int> result; long long solve(int sz, long long s, long long e) { if (sz == 1) return 1; if (sz == 2) return s == e ? 1 : 3; auto ccheck = result.emplace(make_tuple(sz, s, e), 0); if (!ccheck.second) { return ccheck.first->second; } long long len = (1ull << (sz-1)); long long sublen = len / 2; long long ans = 0; if (e - s >= sublen) ans += solve(sz - 1, s, e - sublen); if (s < sublen) ans += solve(sz - 1, s, min(sublen-1, e)); if (e >= sublen) ans += solve(sz - 1, max(0ll, s - sublen), e - sublen); ccheck.first->second = ans % mod; return ans % mod; } int main() { long long L, R; scanf("%lld%lld", &L, &R); long long ans = 0; for (int sz = 1; sz <= 63; sz++) { long long beg = (1ull << (sz - 1)); long long end = (1ull << sz) - 1; if (beg > R) continue; if (end < L) continue; ans += solve(sz, max(0ll, L-beg), min(end - beg, R - beg)); ans %= mod; } 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
import sys sys.setrecursionlimit(10**9) from functools import lru_cache mod=1000000007 @lru_cache() def calc(l,r): if l>r: return 0 if r==0: return 1 b_l,b_r=l.bit_length(),r.bit_length() if b_l==b_r: return calc(l-(1<<(b_l-1)),r-(1<<(b_r-1))) if r==(1<<(b_r-1))-1 and l==0: return pow(3,b_r,mod) return (calc(l,r-(1<<(b_r-1)))+calc(l,(1<<(b_r-1))-1)+calc(0,r-(1<<b_r-1)))%mod @lru_cache() def solve(l,r): if l>r: return 0 b_l,b_r=l.bit_length(),r.bit_length() if b_r>b_l: return (solve(l,(1<<b_r-1)-1)+calc(0,r-(1<<(b_r-1))))%mod a=1<<(b_l-1) if l else 0 return calc(l-a,r-a) l,r=map(int,input().split()) print(solve(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
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 tmp %= mod 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 <iostream> using namespace std; typedef long long ll; const int LGN = 63; const int Z = 1e9+7; int main() { ios::sync_with_stdio(false); ll l, r; cin >> l >> r; int p3[LGN]; for (int i = 0; i < LGN; i++) { p3[i] = i == 0 ? 1 : 3ll * p3[i-1] % Z; } int dp[LGN+1][5]; dp[0][0] = dp[0][1] = dp[0][2] = dp[0][3] = dp[0][4] = 1; for (int i = 1; i <= LGN; i++) { bool bl = l >> (i - 1) & 1, br = r >> (i - 1) & 1; if (!bl && !br) { dp[i][0] = dp[i-1][0]; dp[i][1] = dp[i-1][1]; } else if (!bl && br) { dp[i][0] = (dp[i-1][2] + dp[i-1][4]) % Z; dp[i][1] = (0ll + dp[i-1][3] + dp[i-1][4] + dp[i-1][1]) % Z; } else if (bl && !br) { dp[i][0] = dp[i][1] = 0; } else { dp[i][0] = dp[i][1] = dp[i-1][1]; } if (!bl) { dp[i][2] = (dp[i-1][2] + p3[i-1]) % Z; dp[i][3] = (2ll * dp[i-1][3] + p3[i-1]) % Z; } else { dp[i][2] = dp[i][3] = dp[i-1][3]; } if (!br) { dp[i][4] = dp[i-1][4]; } else { dp[i][4] = (2ll * dp[i-1][4] + p3[i-1]) % Z; } } cout << dp[LGN][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
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); FCoincidence solver = new FCoincidence(); solver.solve(1, in, out); out.close(); } static class FCoincidence { long l; long r; long[][][][] dp = new long[2][2][2][60]; public void solve(int testNumber, InputReader in, OutputWriter out) { l = in.readLong(); r = in.readLong(); ArrayUtils.fill(dp, -1); out.printLine(rec(0, 0, 0, 59)); } long rec(int x, int y, int z, int i) { if (i == -1) return 1; if (dp[x][y][z][i] >= 0) return dp[x][y][z][i]; long ret = 0; boolean flg1 = (l >> i & 1) == 0; boolean flg2 = (r >> i & 1) == 1; if (x == 1 || flg1) ret += rec(x, flg2 ? 1 : y, z, i - 1); if ((x == 1 || flg1) && (y == 1 || flg2) && z == 1) ret += rec(x, y, z, i - 1); if (y == 1 || flg2) ret += rec(flg1 ? 1 : x, y, 1, i - 1); ret %= MiscUtils.MOD7; return dp[x][y][z][i] = ret; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public long readLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class ArrayUtils { public static void fill(long[][] array, long value) { for (long[] row : array) { Arrays.fill(row, value); } } public static void fill(long[][][] array, long value) { for (long[][] row : array) { fill(row, value); } } public static void fill(long[][][][] array, long value) { for (long[][][] row : array) { fill(row, value); } } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } static class MiscUtils { public static final int MOD7 = (int) (1e9 + 7); } }
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
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; ll dp [61][2][2][2]; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << setprecision(20); ll l, r; cin >> l >> r; constexpr ll mod = 1e9 + 7; dp[60][0][0][0] = 1; for (int i = 59; i >= 0; --i) { int lb = (l >> i) & 1; int rb = (r >> i) & 1; rep(j, 2) { rep(k, 2) { rep(s, 2) { rep(x, 2) { ll pre = dp[i+1][j][k][s]; rep(y, 2) { 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; dp[i][nj][nk][ns] %= mod; } } } } } } ll ans = 0; rep(j, 2) { rep(k, 2) { rep(s, 2) { ans += dp[0][j][k][s]; 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
L, R = map(int, input().split()) MOD = 10 ** 9 + 7 l = '{:060b}'.format(L)[::-1] r = '{:060b}'.format(R)[::-1] memo = [[[[-1 for l in range(2)] for k in range(2)] for j in range(2)] for i in range(60)] 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; 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][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][1][0][1] = 1ll; for(i=0; i<60; ++i){ for(j=0; j<128; ++j){ for(k=0; k<7; ++k){ f[k] = ((j&(1<<k))?1:0); } if(f[5] == 0 && f[6] == 1){ continue; } if(f[0] && f[5]<f[6]){ continue; } if(f[1] && f[5]>a[i]){ continue; } if(f[2] && f[6]<b[i]){ continue; } if(f[4] && f[3]>f[6]){ continue; } add_mod(dp[i+1][((f[0]&&(f[5]==f[6]))?1:0)][((f[1]&&(f[5]==a[i]))?1:0)][((f[2]&&(f[6]==b[i]))?1:0)][f[5]][((f[4] && (f[3]==f[6]))?1:0)],dp[i][f[0]][f[1]][f[2]][f[3]][f[4]]); } } for(j=0; j<32; ++j){ for(k=0; k<5; ++k){ f[k] = (j&(1<<k))?1:0; } if(f[4]){ continue; } add_mod(ans,dp[60][f[0]][f[1]][f[2]][f[3]][f[4]]); } 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
import java.io.IOException; import java.io.InputStream; import java.util.NoSuchElementException; public class Main { static long[][][][] dp = new long[64][2][2][2]; static long l, r; public static void main(String[] args) { FastScanner fsc = new FastScanner(); l = fsc.nextLong(); r = fsc.nextLong(); for (int i = 0; i < 64; i++) { for (int gt = 0; gt < 2; gt++) { for (int lt = 0; lt < 2; lt++) { for (int msb = 0; msb < 2; msb++) { dp[i][gt][lt][msb] = -1; } } } } System.out.println(dp(0, 0, 0, 0)); } public static long dp(int i, int gt, int lt, int msb) { if (i >= 64) { return 1; } if (dp[i][gt][lt][msb] >= 0) { return dp[i][gt][lt][msb]; } long ret = 0; boolean x0 = gt == 1 || ((1l << (63 - i)) & l) == 0; boolean y1 = lt == 1 || ((1l << (63 - i)) & r) != 0; // x: 0, y: 0 if (x0) { ret += dp(i + 1, gt, ((1l << (63 - i)) & r) != 0 ? 1 : lt, msb); } // x: 0, y: 1 if (msb == 1 && x0 && y1) { ret += dp(i + 1, gt, lt, msb); } // x: 1, y: 1 if (y1) { ret += dp(i + 1, ((1l << (63 - i)) & l) == 0 ? 1 : gt, lt, 1); } return dp[i][gt][lt][msb] = ret % Const.MOD; } } class FastScanner { private final InputStream in = System.in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) { return buffer[ptr++]; } else { return -1; } } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) { ptr++; } return hasNextByte(); } public String next() { if (!hasNext()) { throw new NoSuchElementException(); } StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) { throw new NoSuchElementException(); } long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { n *= 10; n += b - '0'; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) { throw new NumberFormatException(); } return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } } class Const { public static final long MOD7 = 1_000_000_007; public static final long MOD9 = 1_000_000_009; public static final long MOD99 = 998_244_353; public static final long LINF = 1_000_000_000_000_000_000l; public static final int IINF = 1_000_000_000; public static final double DINF = 1e150; public static final double DELTA = 0.000_000_000_1; public static final double LDELTA = 0.000_001; public static final String YES = "YES"; public static final String NO = "NO"; public static final String Yes = "Yes"; public static final String No = "No"; public static final String POSSIBLE = "POSSIBLE"; public static final String IMPOSSIBLE = "IMPOSSIBLE"; public static final String Possible = "Possible"; public static final String Impossible = "Impossible"; public static final int[] dx8 = {1, 0, -1, 0, 1, -1, -1, 1}; public static final int[] dy8 = {0, 1, 0, -1, 1, 1, -1, -1}; public static final int[] dx = {1, 0, -1, 0}; public static final int[] dy = {0, 1, 0, -1}; public static long MOD = MOD7; public static void setMod(long mod) { MOD = mod; } private Const(){} }
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
mod = 10 ** 9 + 7 memo = {} def solve(L, R): if (L, R) in memo: return memo[(L, R)] if L > R: res = 0 elif L == 1: res = 1 + solve(2, R) else: res = ( solve(L // 2, (R - 1) // 2) + solve((L + 1) // 2, R // 2) + solve((L + 1) // 2, (R - 1) // 2) ) res %= mod memo[(L, R)] = res return res 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
L, R = map(int, raw_input().split()) """ R ????????1?????? y 0001????0?????? ^d ^rd x 0001???????1?? ^ld L ???????????0??? """ ans = 0 for d in range(70): if d<len(bin(L))-3: LD = [] elif d==len(bin(L))-3: LD = [i for i in range(-1,d) if i==-1 or (L>>i&1)==0] else: LD = [d] if d<len(bin(R))-3: RD = [d] elif d==len(bin(R))-3: RD = [i for i in range(-1,d) if i==-1 or (R>>i&1)==1] else: RD = [] for ld in LD: for rd in RD: a = 1 for i in range(d): # i<ld: x==0 or 1 # i==ld: x==1 # i>ld: x==L # i<rd: y==0 or 1 # i==rd: y==0 # i>rd: y==R # y>=x if i<ld and i<rd: a *= 3 if i<ld and i==rd: a *= 1 if i<ld and i>rd: a *= (R>>i&1)+1 if i==ld and i<rd: a *= 1 if i==ld and i==rd: a *= 0 if i==ld and i>rd: a *= (R>>i&1) if i>ld and i<rd: a *= 2-(L>>i&1) if i>ld and i==rd: if (L>>i&1)==0: a *= 1 else: a *= 0 if i>ld and i>rd: if (L>>i&1)==(R>>i&1): a *= 1 if (L>>i&1)==0 and (R>>i&1)==1: a *= 1 if (L>>i&1)==1 and (R>>i&1)==0: a *= 0 ans += a print ans%(10**9+7)
PYTHON
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 modulo 1000000007 #define mod(mod_x) ((((long long)mod_x+modulo))%modulo) #define Inf 1000000000 pair<long long,long long> get(long long Lx,long long Rx,int x,long long d){ long long lx,rx; if(x==0){ if((Lx>>d)&1LL)return {-1,-1}; else lx = Lx; if((Rx>>d)&1LL)rx = (1LL<<d)-1LL; else rx=Rx; } else{ if((Lx>>d)&1LL)lx=Lx^(1LL<<d); else lx=0LL; if((Rx>>d)&1LL)rx=Rx^(1LL<<d); else return {-1,-1}; } return {lx,rx}; } int Get(long long Lx,long long Rx,long long Ly,long long Ry,long long d,int f){ if(d<0){ if(Lx==0&&Rx==0&&Ly==0&&Ry==0)return 1; else return 0; } static map<vector<long long>,int> mp; vector<long long> args = {Lx,Rx,Ly,Ry,d,f}; if(mp.count(args))return mp[args]; int ret = 0; for(int y=0;y<2;y++){ for(int x=0;x<2;x++){ if(y==0&&x==1)continue; if((f==0)&&x!=y)continue; auto [lx,rx] = get(Lx,Rx,x,d); auto [ly,ry] = get(Ly,Ry,y,d); if(lx==-1||rx==-1)continue; if(x==1&&y==1)ret = mod(ret +Get(lx,rx,ly,ry,d-1,1)); else ret = mod(ret + Get(lx,rx,ly,ry,d-1,f)); } } mp[args] = ret; return ret; } int main(){ long long L,R; cin>>L>>R; int ans = Get(L,R,L,R,60,0); 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 LL long long #define mo 1000000007 LL L,R;int f[61][2][2][2]; int cal(int x,int a,int b,int c){ /* a±íÊ¾Ç°ÃæÊÇ·ñÓÐijλx>L b±íʾÊÇ·ñÓÐijλy<R c±íÊ¾Ç°ÃæÊÇ·ñÓÐ1 */ if(x<0)return 1; if(f[x][a][b][c])return f[x][a][b][c]; int l=(L>>x)&1,r=(R>>x)&1,&ans=f[x][a][b][c]; if(a)l=0;if(b)r=1; for(int i=l;i<2;i++)for(int j=i;j<=r;j++){ if(c)(ans+=cal(x-1,a|(i>l),b|(j<r),c))%=mo; else if((i^j)==0)(ans+=cal(x-1,a|(i>l),b|(j<r),(i==1)))%=mo; } return ans; } int main(){ scanf("%lld%lld",&L,&R); printf("%d",cal(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> using namespace std; const long mod = 1000000007; int main() { long l, r; cin >> l >> r; long dp[61][2][2][2] = { }; // 上からi桁確定、L<=x確定?、y<=R確定?、桁数同じ? dp[0][0][0][0] = 1; for (int i = 1; i <= 60; i++) { // 上からi桁確定 for (int j = 0; j < 2; j++) { // L <= x ? for (int k = 0; k < 2; k++) { // y <= R ? for (int s = 0; s < 2; s++) { // 桁数同じ? for (int x = 0; x < 2; x++) { // xの上からi桁目 for (int y = 0; y < 2; y++) { // yの上からi桁目 if (x == 1 && y == 0) continue; if (s == 0 && x != y) continue; // 桁数が合わない if (j == 0 && (l >> (60 - i)) % 2 == 1 && x == 0) continue; // L > x if (k == 0 && (r >> (60 - i)) % 2 == 0 && y == 1) continue; // y > R int jnew = j, knew = k, snew = s; if ((l >> (60 - i)) % 2 == 0 && x == 1) jnew = 1; if ((r >> (60 - i)) % 2 == 1 && y == 0) knew = 1; if (x == 1 && y == 1) snew = 1; dp[i][jnew][knew][snew] += dp[i - 1][j][k][s]; dp[i][jnew][knew][snew] %= 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[60][j][k][s]) % 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
# coding: utf-8 # Your code here! import sys sys.setrecursionlimit(10**6) readline = sys.stdin.readline #文字列入力のときは注意 #n = int(input()) l,r = [int(i) for i in readline().split()] def make_dp(init, size): res = "[{}]*{}".format(init,size[-1]) for i in reversed(size[:-1]): res = "[{} for _ in [0]*{}]".format(res,i) return eval(res) MOD = 10**9 + 7 R = bin(r)[2:] L = bin(l)[2:] L = "0"*(len(R)-len(L)) + L dp = make_dp(0,(len(R)+1,2,2,2)) dp[0][0][0][0] = 1 for i, (r,l) in enumerate(zip(R,L)): #i桁目からi+1桁目に遷移 ri = int(r) li = int(l) for is_less in range(2): for is_more in range(2): for is_nonzero in range(2): for dl in range(0 if is_more else li,2): for dr in range(dl, 2 if is_less else ri+1): # d: i+1桁目の数字 dp[i+1][is_nonzero or dr != 0][is_more or li < dl][is_less or dr < ri] += dp[i][is_nonzero][is_more][is_less]*(dr==dl or is_nonzero) dp[i+1][is_nonzero or dr != 0][is_more or li < dl][is_less or dr < ri] %= MOD #print(dp[-1]) ans = 0 for i in range(2): for j in range(2): for k in range(2): ans += dp[-1][i][j][k] 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
import sys sys.setrecursionlimit(2147483647) INF=float("inf") MOD=10**9+7 input=lambda:sys.stdin.readline().rstrip() from itertools import product def resolve(): L,R=map(int,input().split()) L=map(int,bin(L)[2:].zfill(60)) R=map(int,bin(R)[2:].zfill(60)) dp=[[[0]*2 for _ in range(2)] for _ in range(2)] dp[0][0][0]=1 # dp[msb][mt][lt] for l,r in zip(L,R): ndp=[[[0]*2 for _ in range(2)] for _ in range(2)] for msb,mt,lt,x,y in product(range(2),repeat=5): if((not msb) and (x!=y)): continue if((not mt) and l>x): continue if((not lt) and y>r): continue if(x>y): continue ndp[max(msb,x*y==1)][max(mt,l<x)][max(lt,y<r)]+=dp[msb][mt][lt] ndp[max(msb,x*y==1)][max(mt,l<x)][max(lt,y<r)]%=MOD dp=ndp print(sum(dp[msb][mt][lt] for msb,mt,lt in product(range(2),repeat=3))%MOD) 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 mod 1000000007 #define mod998 998244353 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define mkp make_pair typedef long long ll; using namespace std; ll L, R, res, pow3[60]; int main() { cin >> L >> R; pow3[0] = 1; for (int i = 1; i < 60; ++i) { pow3[i] = pow3[i - 1] * 3 % mod; } for (ll i = 0; i < 60; ++i) { if (L <= (1ll << i + 1) - 1 && (1ll << i) <= R) { ll l = max(L, (1ll << i)); ll r = min(R, (1ll << i + 1) - 1); ll dp[3]; dp[0] = dp[2] = 0; dp[1] = 1; for (ll j = i - 1; j >= 0; --j) { if (r&(1ll << j)) { if (l&(1ll << j)) { res += dp[0] * pow3[j]; dp[0] *= 2; dp[0] %= mod; } else { res += dp[0] * pow3[j]; res += dp[2] * pow3[j]; dp[0] *= 2; dp[2] *= 2; dp[0] += dp[1]; dp[2] += dp[1]; dp[0] %= mod; dp[2] %= mod; } } else { if (l&(1ll << j)) { dp[1] = 0; } else { res += dp[2] * pow3[j]; dp[2] *= 2; dp[2] %= mod; } } res %= mod; } res += dp[0] + dp[1] + dp[2]; } } cout << res % mod << 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 int ll #define inf 0x3f3f3f3f #define mod 1000000007 #define pb push_back #define mp make_pair #define ll long long #define vi vector <int> #define pii pair <int, int> #define eprintf(...) fprintf(stderr, __VA_ARGS__) #define rep(i,n) for (int i = 0; i < (int) (n); ++ i) #define foreach(it,c) for (__typeof(c.begin()) it = c.begin(); it != c.end(); ++ it) inline int read() { int x = 0, f = 1, c = getchar(); for (;!isdigit(c);c = getchar()) if (c == '-') f ^= 1; for (; isdigit(c);c = getchar()) x = x * 10 + c - '0'; return f ? x : -x; } string s, t; string to2(int x) {string str; rep(i, 60) str = char('0' + (x >> i & 1)) + str; return str;} int dp[60][2][2][2]; int DP(int id, int sml, int lgr, int fst) { if (id == 60) return 1; int & rem = dp[id][sml][lgr][fst]; if (~rem) return rem; int res = 0; for (int a = lgr ? 0 : s[id] - '0'; a <= (sml ? 1 : t[id] - '0'); ++ a) { for (int b = lgr ? 0 : s[id] - '0'; b <= (sml ? 1 : t[id] - '0'); ++ b) { if (!fst && b && !a) continue; if (a > b) continue; res += DP(id + 1, sml | b < t[id] - '0', lgr | a > s[id] - '0', fst | b); if (res >= mod) res -= mod; } } // printf("%d %d %d %d = %d\n", id, sml, lgr, fst, res); return rem = res; } signed main() { int l, r; scanf("%lld %lld", &l, &r); s = to2(l), t = to2(r); memset(dp, -1, sizeof dp); printf("%lld\n", DP(0, 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 PII pair<int, int> #define LL long long using namespace std; const int LOG = 65; const LL MOD = 1000000007; const LL INF = (LL) 1e9 + 5; LL L, R, dp[LOG][2][2][2]; bool vis[LOG][2][2][2]; vector<int> d1, d2; void trans(LL x, vector<int> &d) { d.clear(); for (int i = 0; i < LOG; i++) { d.push_back(x % 2); x /= 2; } // cout << "Get:"; for (int x : d) cout << x; cout << '\n'; } LL dfs(int pos, bool lim1, bool lim2, bool first) { if (pos < 0) return 1; if (vis[pos][lim1][lim2][first]) return dp[pos][lim1][lim2][first]; vis[pos][lim1][lim2][first] = true; int up1 = lim1 ? d1[pos] : 1; LL res = 0; for (int i = 0; i <= up1; i++) { int up2 = lim2 ? min(d2[pos], i) : i; for (int j = 0; j <= up2; j++) { if (first && i == 1 && j == 0) continue; res = (res + dfs(pos - 1, lim1 && i == d1[pos], lim2 && j == d2[pos], first && i == 0)) % MOD; } } return dp[pos][lim1][lim2][first] = res; } LL solve(LL r1, LL r2) { trans(r1, d1); trans(r2, d2); memset(vis, 0, sizeof(vis)); return dfs((int) d1.size() - 1, 1, 1, 1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> L >> R; cout << (solve(R, R) - solve(R, L - 1) + MOD) % MOD << '\n'; // LL bf = 0; // for (LL i = L; i <= R; i++) { // for (LL j = L; j <= R; j++) { // int m1 = 0, m2 = 0, t1 = i, t2 = j; // while (t1) t1 >>= 1, m1++; // while (t2) t2 >>= 1, m2++; // if (m1 != m2) continue; // if ((i | j) == i) bf++; // } // } // cout << "BF say " << bf << '\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> #define ll long long #define fornum(A,B,C) for(A=B;A<C;A++) #define mp make_pair #define pii pair<int,int> #define pll pair<ll,ll> using namespace std; ///////////////////////////////////////////////////// #define MOD (ll)(1e9+7) ll L, R; ll bL[90], bR[90]; ll mk[90][2][2][2]; ll i, j, k,l, ans; ll solve(ll p,ll a,ll b,ll c){ if(p==-1){ return 1; } if(mk[p][a][b][c]!=-1) return mk[p][a][b][c]; ll ret = 0; // 0 0 if(a==1||bL[p]==0){ ret += solve(p - 1, a, bR[p] == 1 ? 1 : b, c); // 0 1 if(c==1&&(b==1||bR[p]==1)){ ret += solve(p - 1, a, b, c); } } // 1 1 if(b==1||bR[p]==1){ ret += solve(p - 1, bL[p] == 0 ? 1 : a, b, 1); } mk[p][a][b][c] = ret % MOD; return ret % MOD; } int main(){ scanf("%lld%lld", &L,&R); fornum(i,0,70){ fornum(j,0,2){ fornum(k,0,2){ fornum(l,0,2){ mk[i][j][k][l] = -1; } } } } fornum(i,0,62){ if(R&(1ll<<i)){ bR[i] = 1; } if(L&(1ll<<i)){ bL[i] = 1; } } printf("%lld", 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
L, R = map(int, input().split()) MOD = 10 ** 9 + 7 l = '{:060b}'.format(L)[::-1] r = '{:060b}'.format(R)[::-1] memo = [[[[-1 for l in range(2)] for k in range(2)] for j in range(2)] for i in range(60)] 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 l[pos] == '0' or flagX: ret += f(pos - 1, flagX, 1 if flagY or r[pos] == '1' else 0, flagZ) # x 0 y 1 if (l[pos] == '0' or flagX) and (r[pos] == '1' or flagY) and flagZ: ret += f(pos - 1, flagX, flagY, flagZ) # x 1 y 1 if r[pos] == '1' or flagY: ret += f(pos - 1, 1 if l[pos] == '0' or flagX else 0, 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; int mod = 1e9 + 7; long long dp[61][2][2][2]; int main(){ long long l, r; cin >> l >> r; //dp[桁(i)][L(j)][R(k)][最上位(s)] 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++){ if (x && !y) continue; int nj = j; int nk = k; int ns = s; if (!s && x != y) continue; if (x && y) ns = 1; if (!j && lb && !x) continue; if (!lb && x) nj = 1; if (!k && !rb && y) continue; if (rb && !y) nk = 1; dp[i][nj][nk][ns] += dp[i + 1][j][k][s]; dp[i][nj][nk][ns] %= mod; } } } } } } long 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 += 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 <iostream> #define ll long long using namespace std; ll mod=1000000007, dp[62][2][4][4]; int main() { ll L, R; cin >> L >> R; dp[61][0][0][0]=1; for(int i=60; i>=0; --i){ for(int j=0; j<32; ++j){ int a=(j/4)%4, b=j%4, t=j/16; if(((a&1) || !(L&((ll) 1<<i))) && ((b&1) || !(L&((ll) 1<<i)))){ int c=a, d=b; if((R&((ll) 1<<i))){ c |= 2; d |= 2; } dp[i][t][c][d]=(dp[i][t][c][d]+dp[i+1][t][a][b])%mod; } if(((a&2) || (R&((ll) 1<<i))) && ((b&2) || (R&((ll) 1<<i)))){ int e=a, f=b; if(!(L&((ll) 1<<i))){ e |= 1; f |= 1; } dp[i][1][e][f]=(dp[i][1][e][f]+dp[i+1][t][a][b])%mod; } if((((a&1) || !(L&((ll) 1<<i))) && ((b&2) || (R&((ll) 1<<i)))) && t){ int g=a, h=b; if((R&((ll) 1<<i))) g |= 2; if(!(L&((ll) 1<<i))) h |= 1; dp[i][t][g][h]=(dp[i][t][g][h]+dp[i+1][t][a][b])%mod; } } } ll ans=0; for(int i=0; i<16; ++i){ ans=(ans+dp[0][1][i/4][i%4])%mod; } cout << ans << endl; return 0; }
CPP