P1635 jump

Portal

Observed \ (4x + 3 = 2 ( 2x + 1) +1 \) and \ (8x + 7 = 2 ( 2 (2x + 1) +1) +1 \)

It can be the \ (xx-> 2x + 12x + 1 \) as a fundamental change in

The \ (xx-> 4x + 3 \ ) are the two fundamental changes, \ (xx-> + 8x. 7 \) are the three fundamental changes

It is possible to simulate a fundamental change

When the number of basic changes by more than 300,000 is the end of the iteration

Because the sum of the minimum to make the two changes, so try to use xx-> 8x + 78x + 7

When the basic frequency variation% 3 == 0, are used \ (xx-> + 8x. 7 \) , the total number of basic frequency variation = / 3

When the basic frequency variation% 3 == 1, with two \ (xx-> 4x + 3 \) , with the remaining \ (xx-> +. 7 \ 8x) , the total number of basic frequency variation = / 3 + 1

When the basic frequency variation% 3 == 2, with a (xx-> 4x + 3 \) \ , with the remaining (+. 7 \ xx-> 8x) \ , = total number of basic frequency change / 3 + 1

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1000000007;
ll x,ans;
int main()
{
	cin>>x;
	ll i;
	for(i=0;i<=300000;i++)
	{
		if(x==0)	break;
		x=(x*2+1)%mod;
	}
//其实这里有个小bug,i==300000时已经迭代了300001次
//如果在这个时候刚好等于0,其实还是不符合条件的
//不过我,懒得改了(●'?'●) 
	if(x!=0)	cout<<-1;
	else if(i%3==0)	cout<<i/3;
	else if(i%3==1)	cout<<i/3-1+2;
	else if(i%3==2)	cout<<i/3+1;
	return 0;
}

Guess you like

Origin www.cnblogs.com/iss-ue/p/12636168.html