有一个由
n
n
n 个非负整数组成的序列
a
1
a_1
a1 到
a
n
a_n
an,这个序列保证单调不降。接着,小华将上述序列作为2的次幂,写下了另一个序列:2的
a
1
a_1
a1 次幂到2的
a
n
a_n
an 次幂。
现在他想知道,最少要在这个序列中添加多少个形式为
2
x
2^x
2x 的数(
x
x
x 为非负整数),才能使这个序列所有整数的和为
2
v
−
1
2^v-1
2v−1,其中
v
v
v 为某个非负整数。
Ivan has got an array of n n n non-negative integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an .
Ivan knows that the array is sorted in the non-decreasing order.
Ivan wrote out integers 2 a 1 , 2 a 2 , . . . , 2 a n 2^{a_{1}},2^{a_{2}},...,2^{a_{n}} 2a1,2a2,...,2an on a piece of paper.
Now he wonders, what minimum number of integers of form 2 b 2^{b} 2b ( b > = 0 ) (b>=0) (b>=0) need to be added to the piece of paper so that the sum of all integers written on the paper equalled 2 v − 1 2^{v}-1 2v−1 for some integer v v v ( v > = 0 ) (v>=0) (v>=0) .
Help Ivan, find the required quantity of numbers.
The first line contains integer n n n ( 1 < = n < = 1 0 5 1<=n<=10^{5} 1<=n<=105 ).
The second input line contains n n n space-separated integers a 1 , a 2 , . . . , a n a_{1},a_{2},...,a_{n} a1,a2,...,an ( 0 < = a i < = 2 ⋅ 1 0 9 ) (0<=a_{i}<=2·10^{9}) (0<=ai<=2⋅109) .
It is guaranteed that a 1 < = a 2 < = . . . < = a n a_{1}<=a_{2}<=...<=a_{n} a1<=a2<=...<=an .
Print a single integer — the answer to the problem.
4
0 1 1 1
0
1
3
3
In the first sample you do not need to add anything, the sum of numbers already equals 2 3 − 1 = 7 2^{3}-1=7 23−1=7 .
In the second sample you need to add numbers 2 0 , 2 1 , 2 2 2^{0},2^{1},2^{2} 20,21,22 .
#include
using namespace std;
const int N=2e6+10;
typedef long long ll;
int n,v,ans,x;
map<int,int>sum;
int main(){
cin>>n;
for(int i=1;i<=n;++i){
cin>>x;
while(sum.count(x))
sum.erase(x++);
++sum[x];
v=max(v,x);
}
cout<<v+1-sum.size()<<endl;
return 0;
}
题目大意:定义一对数(x,y),若x、y中至少有一个大于等于m,则称(x,y)对m完美。
现在给定一对数(x,y),允许把其中的一个数换成x+y,问把(x,y)变成对m完美的数对至少需要几次操作。
Let us call a pair of integer numbers m m m -perfect, if at least one number in the pair is greater than or equal to m m m .
Thus, the pairs (3, 3) and (0, 2) are 2-perfect while the pair (-1, 1) is not.
Two integers x x x , y y y are written on the blackboard.
It is allowed to erase one of them and replace it with the sum of the numbers, ( x + y ) (x+y) (x+y) .
What is the minimum number of such operations one has to perform in order to make the given pair of integers m m m -perfect?
Single line of the input contains three integers x x x , y y y and m m m ( − 1 0 18 < = x -10^{18}<=x −1018<=x , y y y , m < = 1 0 18 m<=10^{18} m<=1018 ).
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preffered to use the cin, cout streams or the %I64d specifier.
Print the minimum number of operations or “-1” (without quotes), if it is impossible to transform the given pair to the m m m -perfect one.
1 2 5
2
-1 4 15
4
0 -1 5
-1
In the first sample the following sequence of operations is suitable: (1, 2) (3, 2) (5, 2).
In the second sample: (-1, 4) (3, 4) (7, 4) (11, 4) (15, 4).
Finally, in the third sample x x x , y y y cannot be made positive, hence there is no proper sequence of operations.
#include
using namespace std;
const int N=2e6+10;
typedef long long ll;
ll x,y,m,s=0;
int main(){
cin>>x>>y>>m;
if(x>=m||y>=m){
puts("0");
return 0;
}
else if(x<=0&&y<=0){
puts("-1");
return 0;
}
else if(x*y<0){
if(x<y){
s+=(-x)/y;
x=x%y;
}
else{
s+=(-y)/x;
y=y%x;
}
}
while(x<m&&y<m){
if(x>y)swap(x,y);
x+=y;
s++;
}
cout<<s;
return 0;
}
Fox Ciel有r朵红花,g朵绿花和b朵蓝花。她想用这些花做几个花束。
有四种类型的花束:
输出可以制作的最大数量的花束。
Fox Ciel has some flowers: r r r red flowers, g g g green flowers and b b b blue flowers.
She wants to use these flowers to make several bouquets.
There are 4 types of bouquets:
Help Fox Ciel to find the maximal number of bouquets she can make.
The first line contains three integers r r r , g g g and b b b ( 0 < = r , g , b < = 1 0 9 0<=r,g,b<=10^{9} 0<=r,g,b<=109 ) — the number of red, green and blue flowers.
Print the maximal number of bouquets Fox Ciel can make.
3 6 9
6
4 4 4
4
0 0 0
0
In test case 1, we can make 1 red bouquet, 2 green bouquets and 3 blue bouquets.
In test case 2, we can make 1 red, 1 green, 1 blue and 1 mixing bouquet.
#include
using namespace std;
const int N=2e6+10;
typedef long long ll;
ll a,b,c,ans;
int main(){
cin>>a>>b>>c;
ans=a/3+b/3+c/3+min(a%3,min(b%3,c%3));
if(a>=1&&b>=1&&c>=1) ans=max(ans,(a-1)/3+(b-1)/3+(c-1)/3+1+min((a+2)%3,min((b+2)%3,(c+2)%3)));
if(a>=2&&b>=2&&c>=2) ans=max(ans,(a-2)/3+(b-2)/3+(c-2)/3+2+min((a+1)%3,min((b+1)%3,(c+1)%3)));
printf("%d\n",ans);
return 0;
}
这个世界上只有这样面值的硬币:1,3,9,27,81,…有一个商人,某一天遇到了一个顾客,他购买了价值n的商品,发现用自己的硬币无法付给商人刚好n的钱。那个顾客会给商人大于等于n的钱且使得给商人的硬币数量最少。
在这个顾客有的硬币可能的各种情况下,请问这个商人最多可能收到多少硬币?
Gerald has been selling state secrets at leisure.
All the secrets cost the same: n n n marks.
The state which secrets Gerald is selling, has no paper money, only coins.
But there are coins of all positive integer denominations that are powers of three: 1 mark, 3 marks, 9 marks, 27 marks and so on.
There are no coins of other denominations.
Of course, Gerald likes it when he gets money without the change.
And all buyers respect him and try to give the desired sum without change, if possible.
But this does not always happen.
One day an unlucky buyer came.
He did not have the desired sum without change.
Then he took out all his coins and tried to give Gerald a larger than necessary sum with as few coins as possible.
What is the maximum number of coins he could get?
The formal explanation of the previous paragraph: we consider all the possible combinations of coins for which the buyer can not give Gerald the sum of n n n marks without change.
For each such combination calculate the minimum number of coins that can bring the buyer at least n n n marks.
Among all combinations choose the maximum of the minimum number of coins. This is the number we want.
The single line contains a single integer n n n ( 1 < = n < = 1 0 17 1<=n<=10^{17} 1<=n<=1017 ).
Please, do not use the %lld specifier to read or write 64 bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
In a single line print an integer: the maximum number of coins the unlucky buyer could have paid with.
1
1
4
2
In the first test case, if a buyer has exactly one coin of at least 3 marks, then, to give Gerald one mark, he will have to give this coin.
In this sample, the customer can not have a coin of one mark, as in this case, he will be able to give the money to Gerald without any change.
In the second test case, if the buyer had exactly three coins of 3 marks, then, to give Gerald 4 marks, he will have to give two of these coins.
The buyer cannot give three coins as he wants to minimize the number of coins that he gives.
#include
using namespace std;
const int N=2e6+10;
typedef long long ll;
ll n,s=1;
int main(){
cin>>n;
while(n%s==0) s*=3;
cout<<n/s+1;
return 0;
}
迈克有许多阻值为 1 Ω 1Ω 1Ω 的电阻,通过串联、并联的方式组装为一个电阻为 a b \frac{a}{b} ba 的元件,求所需的最少电阻数量。
1 < = a , b < = 1 0 18 1<=a,b<=10^{18} 1<=a,b<=1018,保证 a b \frac{a}{b} ba 已经最简,保证有解。
Mad scientist Mike is building a time machine in his spare time.
To finish the work, he needs a resistor with a certain resistance value.
However, all Mike has is lots of identical resistors with unit resistance R 0 = 1 R_{0}=1 R0=1 .
Elements with other resistance can be constructed from these resistors.
In this problem, we will consider the following as elements:
With the consecutive connection the resistance of the new element equals
R
=
R
e
+
R
0
R=R_{e}+R_{0}
R=Re+R0 .
With the parallel connection the resistance of the new element equals . In this case
R
e
R_{e}
Re equals the resistance of the element being connected.
Mike needs to assemble an element with a resistance equal to the fraction .
Determine the smallest possible number of resistors he needs to make such an element.
The single input line contains two space-separated integers a a a and b b b ( 1 < = a , b < = 1 0 18 1<=a,b<=10^{18} 1<=a,b<=1018 ).
It is guaranteed that the fraction is irreducible. It is guaranteed that a solution always exists.
Print a single number — the answer to the problem.
Please do not use the %lld specifier to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams or the %I64d specifier.
1 1
1
3 2
3
199 200
200
In the first sample, one resistor is enough.
In the second sample one can connect the resistors in parallel, take the resulting element and connect it to a third resistor consecutively.
Then, we get an element with resistance . We cannot make this element using two resistors.
#include
using namespace std;
const int N=2e6+10;
typedef long long ll;
ll a,b,c,s=0;
int main(){
cin>>a>>b;
while(b!=0){
s+=a/b;
c=a%b;
a=b,b=c;
}
cout<<s;
return 0;
}