题解 | #小q的数列#

小q的数列

https://ac.nowcoder.com/acm/problem/15979

第一个卡取模运算,对2取模求fn就是对二取模的结果

根据规律求解任意一个fn,发现fn表示的是n二进制数值到底有几个1,最小的n那么就是所有一挨在一起,也就是比如f(5)=3,最小的n就是111

注意对1左移的时候会超过int 的二进制位数

//#include "bits_stdc++.h"
#include <functional>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <queue>
#include <vector>
#include <iterator>
#include <map>
#include <bitset>
#include <stack>
#include <deque>
#include <cmath>
#include <iomanip>
#include <forward_list>
#include <set>
#include <cmath>
#include <cctype>
#include <sstream>
#include <climits>
#include <list>
#include <typeinfo>

//INT_MAX 32 bit int
//LLONG_MAX 64 bit int
//LONG_MAX 64 bit int

using namespace std;

//#pragma comment(linker, "/STACK:3073741824")
#pragma warning(disable : 4996)
#define memmin(a) memset(a,0,sizeof(a))
#define memmax(a) memset(a,0x3f,sizeof(a));
#define fspr(n) fixed << setprecision(n)
#define spr(n) setprecision(n)
#define sci setiosflags(ios::scientific)
#define siosf setiosflags
#define endll '\n'
#define ifor(i, l, r) for (long long (i) = (l); (i) <= (r); ++(i))
#define rfor(i, r, l) for (long long (i) = (r); (i) >= (l); --(i))
#define fori(i,l,r) for(auto i=p;i!=p1;++i)
#define rfori(i,rl,rr) for(auto i=rl;i!=rr;++i)
#define coute cout<<'\n'
typedef long long ll;
typedef pair<int, int> P;
typedef unsigned long long ull;

template <class T, class Container = vector<T>,
	class Compare = less<typename Container::value_type> >
using priqueue = priority_queue<T, Container, Compare>;

#define start1
#ifndef start1
const int IINF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1.0e-9;
const long long MOD = 1e9 + 7;
const int MAX1 = 1e5 + 100;
const int MAX2 = 1e7 + 100;

#endif

#define start2
#ifndef start2

//__int128读取
void  read(ll& w) {
	w = 0;
	ll f = 1;
	char ch = getchar();
	while (ch < '0' || ch > '9') {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (ch <= '9' && ch >= '0') {
		w = w * 10 + ch - '0';
		ch = getchar();
	}
}

void print128(ll x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9)print128(x / 10);
	putchar(x % 10 + '0');
}

#endif

#define start
#ifndef start3
template<typename container>
void _print(container& a2) {
	for (auto i = a2.begin(); i != a2.end(); i++) {
		if (i == a2.begin()) cout << *i;
		else
			cout << " " << *i;
	}
}

template<typename T1>
auto _print(T1 a1, ll l, ll r) ->void {
	for (ll i = l; i <= r; ++i) {
		if (i == l) cout << a1[i];
		else cout << " " << a1[i];
	}
}
#endif

//core code 

int f(ll n)
{
	if (n == 1) return 1;
	if (n == 0) return 0;
	return f(n / 2) + n % 2;
}
void solved()
{
	int t;
	cin >> t;
	while (t--)
	{
		ll x;
		cin >> x;
		ll res = f(x);
		printf("%lld %lld\n", res, (1LL << res) - 1);
	}
	return;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin.tie(nullptr);
	solved();
	return 0;	
}
全部评论

相关推荐

10-09 22:05
666 C++
找到工作就狠狠玩CSGO:报联合国演讲,报电子烟设计与制造
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务