zoukankan      html  css  js  c++  java
  • 【JZOJ1273】袁绍的刁难【数论,数学】

    题目大意:

    题目链接:https://jzoj.net/senior/#main/show/1273
    给出一个数列a[]=30,31,32...3Infa[]={3^0,3^1,3^2...3^{Inf}}。求在这个数列中选择任意数字,和是第kk大的数字。


    思路:

    很明显,有a1+a2+a3+...ai1<aia_1+a_2+a_3+...a_i-1<a_i,所以拆分成三进制,然后暴力求解即可。


    代码:

    #include <cstdio>
    #include <iostream>
    using namespace std;
    typedef long long ll;
    
    ll k,power[40],ans;
    int T;
    
    int main()
    {
    	freopen("recruitment.in","r",stdin);
    	freopen("recruitment.out","w",stdout);
    	cin>>T;
    	power[0]=1;
    	for (int i=1;i<=35;i++)
    		power[i]=power[i-1]*3;  //预处理3^n
    	while (T--)
    	{
    		cin>>k;
    		ans=0;
    		for (ll i=0;i<=35;i++)
    			if ((k&((ll)1<<i))==((ll)1<<i)) ans+=power[i];
    		cout<<ans<<endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    C#
    C#
    C#
    创建一个ROS包
    创建一个工作空间
    ROS的文件系统
    单一职责原因
    策略模式
    UML类图
    简单工厂模式
  • 原文地址:https://www.cnblogs.com/hello-tomorrow/p/11998346.html
Copyright © 2011-2022 走看看