首页 > 其他分享 >P9838 挑战 NPC IV

P9838 挑战 NPC IV

时间:2023-11-12 09:23:10浏览次数:25  
标签:cnt cur int ll NPC IV P9838 calc r1

差点就场切了。


按 \(f\) 的值分类。令 \(n'=n\),对于 \(i=1,2,\dots\),\(cnt_i=\lfloor\frac{n'+1}{2}\rfloor\),\(n'\leftarrow \lfloor\frac{n'}{2}\rfloor\)。

注意到数值相同的可以随意交换,这就是说在无标号情况下的一种本质不同方案对应有标号情况下的 \(\prod cnt_i!\) 种方案。

由于 \(k\le 10^{18}\),可以得到 \(n\ge 30\) 时只需考虑最优解。模拟最优解过程即可。


此时把情况转化为无标号情况,即令 \(\displaystyle k\leftarrow \lfloor\frac{k-1}{\prod cnt_i}\rfloor+1\)。

当 \(n=29\) 时 \(cnt\) 数组上界为 \(\{15,7,4,2,1\}\),设 \(f_{a,b,c,d,e,v}\) 表示现在放置了 \(a\) 个 \(1\),\(b\) 个 \(2\),以此类推,总和为 \(v\) 的方案数,可以 \(O(1)\) 转移。

然后就做完了。\(V\) 要开到 \(10^4\) 左右。


只开了 \(5400\)。难受。

#include<bits/stdc++.h>
#define ll long long
#define N 64
#define P 998244353
using namespace std;
ll read(){
	ll x=0,w=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
	while(isdigit(ch))x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*w;
}
//计算 f 数组。每次 cnt_i <- (n+1)/2,n <- n/2。
//注意到 20! > 10^{18},当 n>=30 时直接输出最优解。
//除此之外答案的上界是 O(n^3) 的。
ll cnt[N];int tot;
ll n,k,fac[20];
ll s1(ll x){
	ll r1=x,r2=x+1;
	(r1%2==0)?(r1/=2):(r2/=2);
	r1%=P,r2%=P;
	return r1*r2%P;
}
ll s2(ll x){
	ll r1=x,r2=x+1,r3=2*x+1;
	(r1%2==0)?(r1/=2):(r2/=2);
	(r1%3==0)?(r1/=3):(r2%3==0?r2/=3:r3/=3);
	r1%=P,r2%=P,r3%=P;
	return r1*r2%P*r3%P;
}
ll calc(ll n,ll l,ll r){
	n=(n+1)%P;
	return ((s1(r)-s1(l-1)+P)%P*n-s2(r)+s2(l-1)+P)%P;
}
void solvecase(ll n){
	ll L=0,R=0,ans=0;
	for(int i=tot;i;i--){
		ll cur=0;
		if(cnt[i]%2==0){
			cur=(calc(n,L+1,L+cnt[i]/2)+calc(n,R+1,R+cnt[i]/2))%P;
			L+=cnt[i]/2,R+=cnt[i]/2;
		}
		else{
			if(L>R){
				cur=(calc(n,L+1,L+cnt[i]/2)+calc(n,R+1,R+cnt[i]/2+1))%P;
				L+=cnt[i]/2,R=L;
			}
			else{
				cur=(calc(n,L+1,L+cnt[i]/2+1)+calc(n,R+1,R+cnt[i]/2))%P;
				R+=cnt[i]/2,L=R+1;
			}
		}
		ans=(ans+1ll*i*cur)%P;
	}
	printf("%lld\n",ans);
}
int getval(int x){
	return x*(n-x+1);
}
ll f[16][8][5][3][2][10000];
void solve(){
	n=read(),k=read();
	ll tp=n;tot=0;
	while(tp)cnt[++tot]=(tp+1)/2,tp/=2;
	bool gocase=false;
	ll cur=1;
	if(n>29)gocase=true;
	else{
		for(int i=1;i<=tot;i++){
			if(cur>k/fac[cnt[i]]){gocase=true;break;}
			cur*=fac[cnt[i]];
		}
		gocase|=(cur>=k);
	}
	if(gocase){solvecase(n);return;}
	for(int i=tot+1;i<=5;i++)cnt[i]=0;
	k=(k-1)/cur+1;
	int V=9999,val;
	memset(f,0,sizeof(f)),f[0][0][0][0][0][0]=1;
	for(int a=0;a<=cnt[1];a++)for(int b=0;b<=cnt[2];b++)for(int c=0;c<=cnt[3];c++)for(int d=0;d<=cnt[4];d++)for(int e=0;e<=cnt[5];e++)for(int v=0;v<=V;v++){
		if(!f[a][b][c][d][e][v])continue;
		val=getval(a+b+c+d+e+1);
		if(a!=cnt[1]&&v+val<=V)f[a+1][b][c][d][e][v+val]+=f[a][b][c][d][e][v];
		if(b!=cnt[2]&&v+val*2<=V)f[a][b+1][c][d][e][v+val*2]+=f[a][b][c][d][e][v];
		if(c!=cnt[3]&&v+val*3<=V)f[a][b][c+1][d][e][v+val*3]+=f[a][b][c][d][e][v];
		if(d!=cnt[4]&&v+val*4<=V)f[a][b][c][d+1][e][v+val*4]+=f[a][b][c][d][e][v];
		if(e!=cnt[5]&&v+val*5<=V)f[a][b][c][d][e+1][v+val*5]+=f[a][b][c][d][e][v];
	}
	for(int i=1;i<=V;i++){
		k-=f[cnt[1]][cnt[2]][cnt[3]][cnt[4]][cnt[5]][i];
		if(k<=0){
			printf("%d\n",i);
			return;
		}
	}
	puts("-1");
}
int main(){
	fac[0]=1;
	for(int i=1;i<20;i++)fac[i]=fac[i-1]*i;
	int T=read();
	while(T--)solve();
	
	return 0;
}

标签:cnt,cur,int,ll,NPC,IV,P9838,calc,r1
From: https://www.cnblogs.com/SError0819/p/17826754.html

相关文章

  • USB 3.0 Type-C PD(Power Delivery)
    www.usb.org:USB.orgDocumentLibraryUSBCharger(USBPowerDelivery)|USB-IFType-CPD(PowerDelivery)USBPowerDelivery......
  • Fight Hard for Ecological Protection and Governance of the Yellow River to Addre
    1.Effectivemeasureaimedataddressingthewatercontamination:Wewill fight hard for ecological protection and governance of the Yellow River. We will fully implement the requirements of determining the city, the land, the people,......
  • 如何使用透明的div实现页面背景模糊效果
    要在页面背景上实现模糊效果,并使内容区域(<div>)保持半透明,你可以使用CSS的backdrop-filter属性。这个属性可以用于设置页面背景的滤镜效果,而不影响内部内容的模糊。下面是一个示例的代码片段,展示如何实现这个效果:<!DOCTYPEhtml><html><head><title>背景模糊效果</title>......
  • Performance Improvements in .NET 8 -- Native AOT & VM & GC & Mono
    原生AOT原生AOT在.NET7中发布。它使.NET程序在构建时被编译成一个完全由原生代码组成的自包含可执行文件或库:在执行时不需要JIT来编译任何东西,实际上,编译的程序中没有包含JIT。结果是一个可以有非常小的磁盘占用,小的内存占用,和非常快的启动时间的应用程序。在.NET7......
  • Performance Improvements in .NET 8 -- Native AOT & VM & GC & Mono
    原生AOT原生AOT在.NET7中发布。它使.NET程序在构建时被编译成一个完全由原生代码组成的自包含可执行文件或库:在执行时不需要JIT来编译任何东西,实际上,编译的程序中没有包含JIT。结果是一个可以有非常小的磁盘占用,小的内存占用,和非常快的启动时间的应用程序。在.NET7......
  • ReactNative进阶(十):WebView 应用详解
    (文章目录)一、WebView组件介绍使用WebView组件可通过url来加载显示一个网页,也可以传入一段html代码来显示。下面对其主要属性和方法进行介绍。1.主要属性source:在WebView中载入一段静态的html代码或是一个url(还可以附带一些header选项);automaticallyAdjustCon......
  • TLOP is Implemented Effectively in China
    TheNationalFive-YearWaterEcologicalFunctionPlan(NFWEFP)thatcoversallofChina,hasbeeniteratedsixtimessinceitsimplementationin1995.The13thNFWEFPincludes10first-levelzones,338s-levelzonesand1840third-levelzones.Itspractic......
  • ../include/types.hh:16:43: fatal error: boost/archive/text_oarchive.hpp: No such
     001、make编译报错如下:../include/types.hh:16:43:fatalerror:boost/archive/text_oarchive.hpp:Nosuchfileordirectory 002、 ......
  • 论文阅读:Adaptive Hierarchical Down-Sampling for Point Cloud Classification
    AdaptiveHierarchicalDown-SamplingforPointCloudClassification用于点云分类的自适应分层下采样法摘要深度神经网络中无序点云的确定性下采样到目前为止还没有得到严格的研究。现有的方法对点进行下采样,而不考虑它们对网络输出的重要性,并且经常在处理前对原始点云进行下采样......
  • Codeforces Round 908 (Div. 2) A-D
    SecretSport观察到数据不大,直接摁住x和y枚举即可,方案合法当且仅当刚好打若干局,且赢最后一局的人是赢家#include<bits/stdc++.h>usingnamespacestd;voidsolve(){intn;cin>>n;strings;cin>>s;//xintwina=0,winb=0;for(inti=1;i<=n......