zoukankan      html  css  js  c++  java
  • 蓝桥---数列(进制思维)

    问题描述

    给定一个正整数k(3≤k≤15),把所有k的方幂及所有有限个互不相等的k的方幂之和构成一个递增的序列。

    例如,当k=3时,这个序列是:1,3,4,9,10,12,13,…(该序列实际上就是:30,31,30+31,32,30+32,31+32,30+31+32,…)

    请你求出这个序列的第N项的值(用10进制数表示)。

    例如,对于k=3,N=100,正确答案应该是981。

    输入格式

    只有1行,为2个正整数,用一个空格隔开:k N(k、N的含义与上述的问题描述一致,且3≤k≤15,10≤N≤1000)。

    输出格式

    计算结果,是一个正整数(在所有的测试数据中,结果均不超过2.1*109)。(整数前不要有空格和其他符号)。

    样例输入

    3 100

    样例输出

    981

    思路:
                               30,31,30+31,32,30+32,31+32,30+31+32

    分别对应3进制的1, 10,   11,   100,101,   110,     111,

    而这些3进制数又对应2进制数的1,2,3,4,5,6,7(相当于是下标)

    因此,我们可以逆向来求,比如要求p的第n个数,我们把n转换成2进制数,再按位加pi即可。

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <iostream>
     4 #include <string>
     5 #include <math.h>
     6 #include <algorithm>
     7 #include <vector>
     8 #include <stack>
     9 #include <queue>
    10 #include <set>
    11 #include <map>
    12 #include <sstream>
    13 const int INF=0x3f3f3f3f;
    14 typedef long long LL;
    15 const int mod=1e9+7;
    16 const int maxn=1e4+10;
    17 using namespace std;
    18 
    19 int main()
    20 {
    21     #ifdef DEBUG
    22     freopen("sample.txt","r",stdin);
    23     #endif
    24     
    25     int n,p;
    26     scanf("%d %d",&p,&n);
    27     int sum=0;
    28     int temp=1;
    29     while(n)
    30     {
    31         if(n%2) sum+=temp;
    32         n/=2;
    33         temp*=p;
    34     }
    35     printf("%d
    ",sum);
    36     
    37     return 0;
    38 }

    -

  • 相关阅读:
    CentOS查看CPU信息、位数、多核信息
    Linux常用命令大全
    chmod命令详细用法
    tar命令的详细解释
    yum和rpm命令详解
    LeetCode 241. Different Ways to Add Parentheses
    LeetCode 139. Word Break
    LeetCode 201. Bitwise AND of Numbers Range
    LeetCode 486. Predict the Winner
    LeetCode 17. Letter Combinations of a Phone Number
  • 原文地址:https://www.cnblogs.com/jiamian/p/12285746.html
Copyright © 2011-2022 走看看