分析
通过观察惊奇的发现:
n=100时,
而100的二进制表示为:
所以推断答案为n的二进制上为1的a的次幂之和。
C++ 代码
#include<bits/stdc++.h>
using namespace std;
int a,n;
long long ans;
long long calc(int x) //计算a的x次方
{
long long res=1;
while(x)
{
res=res*a;
x--;
}
return res;
}
int main()
{
cin>>a>>n;
for(int i=10;i>=0;i--)
{
int x=(n>>i)&1; //n的第i位为1,就要加上a^i
if(x)
{
ans+=calc(i);
}
}
cout<<ans;
return 0;
}