数列(洛谷1062)——做过,当时也是灵光一现
之前看到的是基数仅为三的版本,只有一个输入的。我看数据范围就知道暴力出屎已成必然,而且难敲。然后我拿笔稍微划几笔,突然想到这他妈的每一个幂前边的系数都是一,不就可以把二进制一转完事吗——如下。
1 #include<iostream> 2 #include<cstdio> 3 using namespace std; 4 const int N=16; 5 int n,m,mark; 6 long long res,pow[N]={1}; 7 bool val[N]; 8 int main(){ 9 cin>>m>>n; 10 for(int i=1;i<=10;i++)pow[i]=pow[i-1]*m; 11 12 while(n){ 13 val[mark++]=n%2; 14 n/=2; 15 } 16 for(int i=0;i<mark;i++)res+=val[i]*pow[i]; 17 cout<<res<<endl; 18 return 0; 19 }
洛谷 Aqours 0ms

浙公网安备 33010602011771号