QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#344634#4789. Infinite Pattern MatchingKevin5307WA 3ms3572kbC++201.5kb2024-03-04 20:38:362024-03-04 20:38:37

Judging History

你现在查看的是最新测评结果

  • [2024-03-04 20:38:37]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3572kb
  • [2024-03-04 20:38:36]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll calc(ll x)
{
	ll tmp=__lg(x);
	ll ans=0;
	for(int i=1;i<=tmp;i++)
		ans+=i*(1ll<<(i-1));
	ans+=(tmp+1)*(x-(1ll<<tmp)+1);
	return ans;
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	unsigned long long ans=0x3f3f3f3f3f3f3f3f;
	string s;
	cin>>s;
	// Case 1
	{
		ll tot=(s[0]=='0');
		for(auto ch:s)
			tot=(tot<<1)|(ch^48);
		ans=calc(tot);
	}
	// Case 2
	{
		for(int i=1;i<s.length();i++) if(s[i]=='1')
		{
			ll A=0,B=0;
			for(int j=0;j<i;j++)
				A=(A<<1)|(s[j]^48);
			for(int j=i;j<s.length();j++)
				B=(B<<1)|(s[j]^48);
			ll mod=(1ll<<i);
			for(int b=0;b+s.length()-i<=58;b++)
			{
				ll L=B<<b,R=(B+1)<<b;
				ll val=(L<=A?A:(L-A+mod-1)/mod*mod+A);
				if(s[0]=='0') val=max(val,A+mod);
				if(val<R)
					ans=min(ans,calc(val)+s.length()-i);
			}
		}
	}
	// Case 3
	{
		for(int i=0;i<s.length();i++) if(s[i]=='1')
			for(int j=i;j<s.length();j++)
			{
				ll val=0;
				for(int k=i;k<=j;k++)
					val=(val<<1)|(s[k]^48);
				string pattern;
				int len=0;
				for(ll k=val-1;k<=val+50;k++)
				{
					ll tmp=k;
					string suf;
					while(tmp)
					{
						suf+=(char)(48^(tmp&1));
						tmp>>=1;
					}
					if(k==val-1)
						len=suf.length();
					reverse(suf.begin(),suf.end());
					pattern+=suf;
				}
				if(len<i) continue;
				pattern=pattern.substr(len-i,s.length());
				if(pattern==s)
					ans=min(ans,calc(val)+s.length()-j-1);
			}
	}
	cout<<ans<<endl;
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3548kb

input:

11

output:

2

result:

ok answer is '2'

Test #2:

score: 0
Accepted
time: 1ms
memory: 3492kb

input:

011011

output:

42

result:

ok answer is '42'

Test #3:

score: 0
Accepted
time: 1ms
memory: 3552kb

input:

01000110011010110000

output:

4627720

result:

ok answer is '4627720'

Test #4:

score: -100
Wrong Answer
time: 3ms
memory: 3572kb

input:

1011000001101110110111001010001010010101010000101

output:

1591917992059971

result:

wrong answer expected '1617827598069187', found '1591917992059971'