zoukankan      html  css  js  c++  java
  • 紫书 习题 10-22 UVa 10479 (找规律)

    自己一直在纠结这个串的构造方法

    而没有观察串本身的规律……

    2的63次方用 unsigned long long

    然后可以发现串是递归构造的。

    将串分成1,1,2,4,8,16, 然后会发现s串里面1个s-2串,2个s-3串,3个s-4串最后加上s

    如第三个串1003

    是由1个1串(1), 2个0串(0)最后加上自己3构成的

    因此根据这个性质递归求就好了

    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #define REP(i, a, b) for(int i = (a); i < (b); i++)
    using namespace std;
    
    typedef unsigned long long ull;
    ull n;
    
    ull dfs(int k)
    {
    	int p = 1;
    	for(int i = k - 2; i >= 0; i--)
    	{
    		ull t = i ? (1uLL << (i - 1)) : 1;
    		REP(j, 0, p)
    		{
    			if(n > t) n -= t;
    			else return dfs(i);
    		}
    		p++;
    	}
    	return k;
    }
    
    int main()
    {
    	while(~scanf("%llu", &n) && n)
    	{
    		if(n == 1) { puts("0"); continue; }
    		n--;
    		for(int i = 1; ; i++)
    		{
    			ull t = 1uLL << (i - 1);
    			if(n > t) n -= t;
    			else
    			{
    				printf("%llu
    ", dfs(i));
    				break;
    			}
    		}
    	}
    	return 0;
    }
  • 相关阅读:
    Win7 VS2015环境编译Libpng
    VS2013正确设置DLL环境变量目录的方法
    Win7 VS2013环境编译Squirrel 3.0.7
    docker-compose部署redis
    docker-compose部署nginx
    mysql备份
    docker 清理空间
    centos安装docker
    django整合vue
    部署3主3从redis伪集群
  • 原文地址:https://www.cnblogs.com/sugewud/p/9819467.html
Copyright © 2011-2022 走看看