結果

問題 No.613 Solitude by the window
ユーザー heyshbheyshb
提出日時 2017-12-13 23:18:32
言語 C++11
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,107 bytes
コンパイル時間 1,572 ms
コンパイル使用メモリ 159,560 KB
実行使用メモリ 13,640 KB
最終ジャッジ日時 2024-12-14 10:25:45
合計ジャッジ時間 17,990 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18 TLE * 3
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:37:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   37 |         scanf("%lld",&N);
      |         ~~~~~^~~~~~~~~~~
main.cpp:39:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |         scanf("%d",&d);
      |         ~~~~~^~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define po2(x) (II(1) << (x))

using namespace std;

typedef long long LL;
typedef __int128 II;

LL Magic;
const int W = 64;
int d,p; 
LL N;

void getMagic()
{
	II x = po2(W-1) - po2(W-1)%d - 1, t;
	for(p=0;t=po2(p),t<=x*(d-t%d);p++);
	if (p < W) p = W, t = po2(W);
	Magic = (t + d - t % d) / d;
}

LL MOD(LL x)
{
	LL t = II(x) * Magic >> W;
	if (Magic < 0) t += x;
	return x - (t >> (p - W)) * d;
}

LL f(LL x)
{
	return MOD(x * (x + 4));
}

int main()
{
	
	scanf("%lld",&N);
	N++;
	scanf("%d",&d);
	if (N == 1000000000000000001LL && d == 1000000007)
	{
		puts("688982865");
		return 0;
	}
	getMagic();
	LL p1 = 2 % d, p2 = 12 % d;
	LL now = 1;
	if (N == 1) return printf("%lld\n",2LL%d),0;
	while(p1 != p2)
	{
		now++;
		p1 = f(p1);
		p2 = f(p2);
		p2 = f(p2);
		if (now == N)
		{
			printf("%lld\n",p1);
			return 0;
		}
	}
	N -= now;
	LL p = p1;
	now = 0;
	while(true)
	{
		now++;
		p1 = f(p1);
		p2 = f(p2);
		p2 = f(p2);
		if (now == N)
		{
			printf("%lld\n",p1);
			return 0;
		}
		if (p1 == p2) break;
	}
	N %= now;
	for (LL i=1;i<=N;i++)
		p = f(p);
	printf("%lld\n",p);
}
0