http://codeforces.com/contest/1204/problem/B
B. Mislove Has Lost an Array
Mislove had an array a1a1, a2a2, ⋯⋯, anan of nn positive integers, but he has lost it. He only remembers the following facts about it:
- The number of different numbers in the array is not less than ll and is not greater than rr;
- For each array's element aiai either ai=1ai=1 or aiai is even and there is a number ai2ai2 in the array.
For example, if n=5n=5, l=2l=2, r=3r=3 then an array could be [1,2,2,4,4][1,2,2,4,4] or [1,1,1,1,2][1,1,1,1,2]; but it couldn't be [1,2,2,4,8][1,2,2,4,8] because this array contains 44 different numbers; it couldn't be [1,2,2,3,3][1,2,2,3,3] because 33 is odd and isn't equal to 11; and it couldn't be [1,1,2,2,16][1,1,2,2,16]because there is a number 1616 in the array but there isn't a number 162=8162=8.
According to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array.
Input
The only input line contains three integers nn, ll and rr (1≤n≤10001≤n≤1000, 1≤l≤r≤min(n,20)1≤l≤r≤min(n,20)) — an array's size, the minimal number and the maximal number of distinct elements in an array.
Output
Output two numbers — the minimal and the maximal possible sums of all elements in an array.
Examples
input
Copy
4 2 2
output
Copy
5 7
其实这道题就是思维题,所以一定要多想,通过观察我们可以发现最小值和最大值都是有规律可循的。
意思就是说最小值minsum=(2^0--->2^(l-1))+(n-l),最大值就是maxsum=(2^0--->2^(r-1))+(n-r)*2^(r-1).所以代码就很简单了。
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
int n,l,r;
int minsum,maxsum,temp1,temp2;
cin>>n>>l>>r;
temp1=0;temp2=0;
for(int i=0;i<=(l-1);i++)
{
temp1+=pow(2,i);
}
minsum=(n-l)+temp1;
for(int i=0;i<=(r-1);i++)
{
temp2+=pow(2,i);
}
maxsum=(n-r)*pow(2,r-1)+temp2;
cout<<minsum<<" "<<maxsum<<endl;
return 0;
}