题意:给定n,让你构造一个数组,数组中的数字只有两种情况,是1或者存在其他数字是其他数字的两倍,数组中数字的种类数不能大于r,也不能小于l,输出数组中和的最小值与最大值。
思路:最小值:
最大值:
AC代码:
#include<bits/stdc++.h> // n-l + 2的l加1次方减1 // n-r乘2的r减1次方+2的r次方加1 using namespace std; int main(void){ int n,l,r; cin>>n>>l>>r; int ans = 1; int res = 1; for(int i = 1; i <= l; i++){ ans *= 2; } ans += n-l-1; for(int i = 1; i <= r; i++){ res *= 2; } res += (n-r)*res/2-1; cout<<ans<<" "<<res<<endl; return 0; }