类似于二进制,并以此为思路解决
而且第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)
数值只有1和0 类似于2进制表示 但底数为K
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;
}
类似于二进制,并以此为思路解决
而且第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)