/* -*- coding: utf-8 -*- * * 822.cc: No.822 Bitwise AND - yukicoder */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; /* constant */ const int MAX_N = 100; const int BN = 24; const int INF = 1 << 30; const long long LINF = 1LL << 60; /* typedef */ typedef long long ll; typedef vector vi; typedef queue qi; typedef pair pii; /* global variables */ ll es[BN]; /* subroutines */ ll rec(int x, int i, int msk, int k) { while (i >= 0 && ! ((msk >> i) & 1)) i--; if (i < 0) return (x <= k) ? 1 : 0; if (x + msk <= k) return es[i + 1]; if (x - msk > k) return 0; int bi = 1 << i; msk ^= bi; i--; ll sum = 0; sum += rec(x + bi, i, msk, k); sum += rec(x, i, msk, k); sum += rec(x - bi, i, msk, k); return sum; } /* main */ int main() { es[0] = 1; for (int i = 1; i < BN; i++) es[i] = es[i - 1] * 3; int n, k; scanf("%d%d", &n, &k); int msb = 1; for (; msb <= n; msb <<= 1); //printf("msb=%x\n", msb); if (msb - ((msb - 1) ^ n) <= k) { puts("INF"); return 0; } ll cnt = 1; int msk = 0; for (int i = 0, bi = 1; bi < msb; i++, bi <<= 1) if (! (n & bi)) { if (bi - msk > k) break; ll c = rec(bi, i - 1, msk, k); //printf("rec(%d, %d, %d, %d) = %lld\n", bi, i - 1, msk, k, c); cnt += c; msk |= bi; } printf("%lld\n", cnt); return 0; }