結果

問題 No.3554 Rurumaru Function Problem 2
コンテスト
ユーザー askr58
提出日時 2026-05-23 00:10:21
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 989 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,215 ms
コンパイル使用メモリ 335,800 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-05-23 00:10:27
合計ジャッジ時間 3,092 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge3_1
純コード判定待ち
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	auto f=[](ll n,ll x)->ll{
		n--;
		vector<ll> dp(4);
		dp[0]=1;
		for(int i=29;i>=0;i--){
			vector<ll> ndp(4);
			for(int a=0;a<2;a++)for(int b=0;b<2;b++){
				if(i%2==0&&(a&b)!=(x>>i&1))continue;
				if(i%2==1&&(a|b)!=(x>>i&1))continue;
				for(int c=0;c<2;c++)for(int d=0;d<2;d++){
					if(c==0&&(n>>i&1)==0&&a==1)continue;
					if(d==0&&(n>>i&1)==0&&b==1)continue;
					int nc=c,nd=d;
					if((n>>i&1)==1&&a==0)nc=1;
					if((n>>i&1)==1&&b==0)nd=1;
					ndp[nc*2+nd]+=dp[2*c+d];
				}
			}
			dp=ndp;
		}
		return dp[0]+dp[1]+dp[2]+dp[3];
	};
	ll ans=0;
	ll p=0;
	ll cur=1;
	for(int i=0;i<=30;i+=2){
		ll nxt=p+(1<<(i+1));	
		if(f(n,p)>=f(n,nxt)){
			ans+=(n-cur+1)*p;
			break;
		}
		ll l=cur,r=n;
		while(l+1<r){
			ll c=(l+r)/2;
			if(f(c,p)>=f(c,nxt))l=c;
			else r=c;
		}
		ans+=(r-cur)*p;
		p=nxt;
		cur=r;
	}
	cout<<ans<<endl;
}
			

0