スキップ

1000 ワード

Problem 2ジャンプ(jump.cpp/c/pas)
【タイトル説明】
最初は数軸上のx位置に位置し、1回のジャンプでxから(9 x+4)mod 1000000007または(27 x+13)mod 1000000007にジャンプできます.原点に戻る最低ジャンプ回数を求めます.
限られたジャンプで原点に戻ることを保証します.
【入力形式】
1行1つの整数xはあなたの初期座標を表し、0<=x<100000007を保証します.
【出力形式】
1行1個の整数は最小ジャンプ回数を表す.
【サンプル入力】
555555559
【サンプル出力】
1
【データ範囲】
40%のデータに対して、答え<=20
100%のデータに対して、答え<=10000000
神の結論:
9 x+4は2回の3 x+1に等しい
27 x+13は3回の3 x+1に等しい
#include<cstdio>
#include<iostream>
#include<functional>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
#define MAXN (100000)
#define F (1000000007)
long long x;
int main()
{
	freopen("jump.in","r",stdin);
	freopen("jump.out","w",stdout);
	while (scanf("%d",&x)==1)
	{
		int ans=0;
		for (;!((!x)&&ans!=1);x=(3*x+1)%F) ans++;
		printf("%d
",ans/3+bool(ans%3)); } return 0; }