Kill a water question qwq [monkey eat peach question]

Subject:
A little monkey bought some peaches. He happened to eat half of these peaches on the first day, and ate one more peach; every day after that, he would eat half of the remaining peaches plus one more. When I got up in the morning on the n(n\le20)n(n≤20) day, there was only 1 peach left. How many peaches did the little monkey buy?

This is the first time this Konjac has written a problem solution qwq
nonsense, less talk and paste code:

#include<bits/stdc++.h>  
using namespace std;

int main()
{
    
    
	int n;
	cin>>n;
	long long t=3*pow(2.0,n-1)-2;
	if(n!=0)cout<<t;
	else if(n==0)cout<<1;
}

The principle is very simple.
We will mark the last day as a1=1

Then we have a recurrence relationship an = 2* ( an-1 +1)

dalao can be directly obtained by the fixed point method

Of course, after the identity transformation

We can get an+2=2*( an-1 +2) Let bn=an+2

There are b1=3 and bn=2bn-1 bn is a geometric sequence with a common ratio of 2 and the first term of 3!

The late bn = 3 2 ^ (n-1) an = 3 2 ^ (n-1) -2;

So we can directly output! (Don’t forget to discuss the n==0 situation)

Note that when n is large, long long is required to ensure the range


I didn’t have AC for the first time because of this

Guess you like

Origin blog.csdn.net/melon_sama/article/details/107860085