(数学)洛谷P1062 数列

洛谷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;
}

发布了119 篇原创文章 · 获赞 1 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Z7784562/article/details/104137892