結果

問題 No.916 Encounter On A Tree
ユーザー leaf_1415
提出日時 2019-10-25 22:16:33
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,498 bytes
コンパイル時間 508 ms
コンパイル使用メモリ 57,376 KB
実行使用メモリ 34,656 KB
最終ジャッジ日時 2024-09-13 04:13:26
合計ジャッジ時間 4,062 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 56
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#define llint long long
#define mod 1000000007

using namespace std;

llint d, l, r, k;

const int FACT_MAX = 2000005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < FACT_MAX; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2);
	for(int i = FACT_MAX-2; i >= 0; i--){
		fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
	}
}

llint get(llint x)
{
	llint ret = 0;
	for(;x;x/=2) ret++;
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> d >> l >> r >> k;
	make_fact();
	
	llint ans = 1;
	for(int i = 1; i <= d; i++) ans *= fact[1<<(i-1)], ans %= mod;
	//cout << ans << endl;
	
	if(l == r){
		if(k == 0) cout << ans << endl;
		else cout << 0 << endl;
		return 0;
	}
	
	llint dl = get(l), dr = get(r);
	if((dl+dr-k) % 2){
		cout << 0 << endl;
		return 0;
	}
	llint da = (dl+dr-k) / 2, f = dr - da;
	if(da > min(dl, dr) || da < 1){
		cout << 0 << endl;
		return 0;
	}
	//cout << dl << " " << dr << " " << da << " " << f << endl;
	
	if(da < min(dl, dr)) ans *= 1<<(f-1), ans %= mod;
	else ans *= 1<<f, ans %= mod;
	
	if(dl != dr) ans *= modpow(1<<(dr-1), mod-2), ans %= mod;
	else ans *= modpow((1<<(dr-1))-1, mod-2), ans %= mod;
	
	cout << ans << endl;
	
	return 0;
}
0