189. 方幂数列

Money4

数值只有1和0 类似于2进制表示 但底数为K

#include<bits/stdc++.h>
using namespace std;
int main(){
    int k,n;
    cin>>k>>n;
    long long i = 0,ans=0;
    while(n){
        int left = n % 2;
        ans += left*pow(k,i);
        n = n/2;
        i++;
    }
    cout<<ans<<endl;
}
lnu_cxn

typedef long long ll;

int main() {
ll s[1010] = { 0 };
int k, n;
cin >> k >> n;
s[1] = 1;
s[2] = k;
s[3] = s[1] + s[2];
s[4] = k * k;
int num = 5;
int p = 3;
for (int i = 4;;) {
for (int j = 1; j < i; j++) {
s[num] = s[j] + s[i];
if (num == n) {
cout << s[num] << endl;
return 0;
}
num++;
}
s[num] = pow(k, p++);
i = num++;
}
return 0;
}

cd106224
//DFS
#include <iostream>
#include <vector>
#include <set>
using namespace std;
using LL = long long;
set<LL> si;
vector<LL> vi;

void DFS(int index, LL sum, int n) {
    if (index == n) {
        si.insert(sum);
        return;
    }
    DFS(index + 1, sum, n);
    DFS(index + 1, sum + vi[index], n);
}

int main() {
    int k, N;
    cin >> k >> N;
    LL ans = 1;
    for (int i = 1; i <= 15; ++i) {
        si.insert(ans);
        vi.push_back(ans);
        ans = ans * k;
    }

    DFS(0, 0, 15);

    int count = -1;
    for (set<LL>::iterator it = si.begin(); it != si.end(); ++it) {
        count++;
        if (count == N) {
            cout << *it << endl;
            break;
        }
    }
    return 0;
}
wty24601

类似于二进制,并以此为思路解决
而且第N个数就是N的二进制表示(底数为k)
def p(a,b):
x = 1
while b > 0:
if b % 2 == 1:
x = (x * a)
b = b // 2
a = a * a
else:
a = a * a
b = b / 2
return x
k,N=map(int,input().split())
L=[]
L1=[]
for i in range(11):
L.append(p(k,i))
t = -1
a = 0
while N > 0:
if N % 2 == 0:
t = t + 1
N = N / 2
else:
t = t + 1
N = N // 2
a = a + L[t]
print(a)

你当前正在回复 博客/题目
存在问题!