洛谷P1062 數列
思路:
挺有意思的一個題目。剛開始的想法是枚舉ki,然後把ki和數列前面的數相加。但是後來注意到每個數列的序號的二進制,可以發現:
1 | 3 | 4 | 9 | 10 | 12 | 13 |
---|---|---|---|---|---|---|
30 | 31 | 30+31 | 32 | 30+32 | 31+32 | 30+31+32 |
1 | 10 | 11 | 100 | 101 | 110 | 111 |
數列的值就是數列編號的二進制數位i的值*ki-1。
代碼:
#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
const int N=1e6+10;
const int mod=1e7+9;
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
ll k,n,ans=0;
cin>>k>>n;
ll m=1;
for(;n;n>>=1)
{
if(n&1)
ans+=m;
m*=k;
}
cout<<ans<<endl;
return 0;
}