【题解】Special Numbers
原题链接:
Problem - 1594B - Codeforces
题目描述
Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers.
Let's call a positive number special if it can be written as a sum of different non-negative powers of
Theofanis asks you to help him find the
k ">10 9 + 7 ">输入
The first line contains a single integer
The first and only line of each test case contains two integers
n ">k ">2 ≤ n ≤ 10 9 ">1 ≤ k ≤ 10 9 ">输出
n ">k ">2 ≤ n ≤ 10 9 ">1 ≤ k ≤ 10 9 ">k ">10 9 + 7 ">输入样例
3
3 4
2 12
105 564
n ">k ">2 ≤ n ≤ 10 9 ">1 ≤ k ≤ 10 9 ">k ">10 9 + 7 ">输出样例
9
12
3595374
n ">k ">2 ≤ n ≤ 10 9 ">1 ≤ k ≤ 10 9 ">k ">10 9 + 7 ">分析及代码实现
n ">k ">2 ≤ n ≤ ">10 9 1 ≤ k ≤ ">10 9 k ">10 9 + 7 ">比如n为3,k为13,此时k的二进制为1101。n ">k ">2 ≤ n ≤ ">10 9 1 ≤ k ≤ ">10 9 k ">10 9 + 7 ">那么第k个以n为底的幂底数的累加就为3^0 + 3^2 + 3^3 = 3
那么我们就可以写出n = 3时的情况
c++代码实现
#includeusing namespace std; typedef long long ll; const ll mod = 1e9 + 7; int main(){ int t; cin >> t; while(t--){ ll n, k, ret = 0, deal = 1; scanf("%lld%lld", &n, &k); while(k){ if (k & 1)//如果当前最后一位是1,则加上deal ret = (ret + deal) % mod; deal = deal * n % mod;//处理deal k >>= 1;//右移一位,看k的下一个二进制位 } printf("%lld\n", ret); } return 0; }