数列(codevs 1141)

题目描述 Description

给定一个正整数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。

输入描述 Input Description

只有1行,为2个正整数,用一个空格隔开:

k N(k、N的含义与上述的问题描述一致,且3≤k≤15,10≤N≤1000)

输出描述 Output Description

为计算结果,是一个正整数(可能较大你懂的)。(整数前不要有空格和其他符号)

样例输入 Sample Input

3 100

样例输出 Sample Output

981

//递推公式研究了20分钟,题解说是普通的进制转换~~~~(>_<)~~~~ 
#include<cstdio>
#include<iostream>
#include<cmath>
#define M 1010
#define LL long long
using namespace std;
LL a[M];
int num[M];
LL poww(LL x,int n)
{
    if(n==0)return 1;
    else
    {
        while((n&1)==0)
        {
            n>>=1;
            x*=x;
        }
    }
    LL result=x;
    n>>=1;
    while(n!=0)
    {
        x*=x;
        if((n&1)!=0)
          result*=x;
        n>>=1;
    }
    return result;
}
void init()
{
    num[0]=1;
    for(int i=1;i<=10;i++)
      num[i]=num[i-1]*2;
    for(int i=1;i<=10;i++)
      num[i]+=num[i-1];
}
int main()
{
    LL k;int n;
    init();
    scanf("%lld%d",&k,&n);
    a[1]=1;
    int p=1;
    for(int i=2;i<=n;i++)
    {
        if(i>num[p])p++;
        int pos=pow(2,p);
        a[i]=a[i-pos]+poww(k,p);
    }
    printf("%lld",a[n]);
    return 0;
} 
View Code
原文地址:https://www.cnblogs.com/harden/p/5640516.html