Submission #39648391


Source Code Expand

// हर हर महादेव
using namespace std;
#include <bits/stdc++.h>
#define int long long int

const int lim = 1e3 + 5;
const int dig = 7;
const int N = (int) 1e18 + 50;
const int inf = (int) 1e18 + 50;

bool check(int n,int b){
	while(n){
		if(abs(n % b) > 1){
			return false;
		}
		n /= b;
	}
	return true;
}

int interpret_in_base_b(int mask,int b){
	int ans = 0;
	__int128 pw = 1;
	bool flag = false;
	for(int i = 0; i < dig; i++){
		if(1 << i & mask){
			if(flag){
				return inf;
			}
			ans += pw;
		}
		pw *= b;
		if(pw > N){
			flag = true;
		}
	}
	return ans;
}

int binary_search_base(int n,int mask){ // value of b for which mask interpreted in base b is equal to n. Returns -1 if there is no such b
	int l = 0,r = N,ans = -1;
	while(l <= r){
		int m = (l + r) >> 1;
		if(interpret_in_base_b(mask,m) <= n){
			ans = m;
			l = m + 1;
		}
		else{
			r = m - 1;
		}
	}
	
	return interpret_in_base_b(mask,ans) == n ? ans : -1;
}

void testcase(){
	
	int n; cin >> n;
	
	// checking for b in [2,1000)
	int ans = 0;
	for(int i = 2; i < lim; i++){
		ans += check(n,i);
	}
	
	
	// bitmasking + binary search
	for(int mask = 1;mask < (1 << dig);mask++){
		int here = binary_search_base(n,mask);
		if(here >= lim){
			ans++;
		}
	}
	cout << ans << '\n';
}

int32_t main(){
	ios::sync_with_stdio(false);cin.tie(0);
	
	int tt; cin >> tt;
	
	while(tt--){
		testcase();
	}
	return 0;
}

Submission Info

Submission Time
Task F - Zero or One
User shadow9
Language C++ (GCC 9.2.1)
Score 500
Code Size 1487 Byte
Status AC
Exec Time 165 ms
Memory 3664 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 35
Set Name Test Cases
Sample example0.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, example0.txt
Case Name Status Exec Time Memory
000.txt AC 140 ms 3620 KiB
001.txt AC 142 ms 3560 KiB
002.txt AC 148 ms 3592 KiB
003.txt AC 153 ms 3552 KiB
004.txt AC 152 ms 3552 KiB
005.txt AC 152 ms 3624 KiB
006.txt AC 150 ms 3484 KiB
007.txt AC 152 ms 3592 KiB
008.txt AC 148 ms 3660 KiB
009.txt AC 146 ms 3664 KiB
010.txt AC 147 ms 3664 KiB
011.txt AC 150 ms 3664 KiB
012.txt AC 150 ms 3548 KiB
013.txt AC 153 ms 3548 KiB
014.txt AC 150 ms 3500 KiB
015.txt AC 152 ms 3540 KiB
016.txt AC 151 ms 3620 KiB
017.txt AC 151 ms 3616 KiB
018.txt AC 155 ms 3500 KiB
019.txt AC 152 ms 3484 KiB
020.txt AC 153 ms 3500 KiB
021.txt AC 144 ms 3484 KiB
022.txt AC 145 ms 3548 KiB
023.txt AC 145 ms 3488 KiB
024.txt AC 147 ms 3620 KiB
025.txt AC 145 ms 3548 KiB
026.txt AC 145 ms 3552 KiB
027.txt AC 146 ms 3584 KiB
028.txt AC 146 ms 3484 KiB
029.txt AC 148 ms 3596 KiB
030.txt AC 145 ms 3660 KiB
031.txt AC 165 ms 3488 KiB
032.txt AC 164 ms 3624 KiB
033.txt AC 154 ms 3592 KiB
example0.txt AC 3 ms 3584 KiB