题解:P15131 [ROIR 2026] 长方体的染色
思路
涂色面数的本质是单位立方体的涂色面数,等于其在 \(x\)、\(y\)、\(z\) 三个维度上是否处于长方体边界的贡献值之和:
- 若某维度长度 \(s>1\):该维度的边界位置贡献 \(1\) 个涂色面,非边界位置贡献 \(0\) 个;
- 若某维度长度 \(s=1\):该维度只有 \(1\) 个位置,这个位置会暴露在长方体的两个对立面上,因此贡献 \(2\) 个涂色面。
对每个维度 \(s\),定义三个特征值:
- \(s_0\):该维度内贡献 \(0\) 个涂色面位置数量;
- \(s_1\):该维度内贡献 \(1\) 个涂色面位置数量;
- \(s_2\):该维度内贡献 \(2\) 个涂色面位置数量。
若 \(s=1\):\(s_0=0\),\(s_1=0\),\(s_2=1\)(仅 \(1\) 个位置,贡献 \(2\))。若 \(s>1\):\(s_0=s-2\)(非边界位置),\(s_1=2\)(两个边界),\(s_2=0\)。
然后枚举所有的组合,统计所有和为 \(k\) 的组合对应的立方体数量(各维度特征值的乘积),求和就为答案。
注意
不开 long long 见祖宗。
代码
#include<bits/stdc++.h>
#define lcm(x,y) x/__gcd(x,y)*y
#define lb(x) (x&-x)
#define str to_string
#define endl '\n'
#define endr '\r'
#define emp_ '\0'
using namespace std;
#define int long long
using ll=long long;
namespace Fast{
namespace Fread{const int SZ=1<<20;char buf[SZ],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SZ,stdin);if(S==TT)return endl;}return *S++;}}
namespace Fwrite{const int SZ=1<<20;char buf[SZ],*S=buf,*TT=buf+SZ;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
using namespace Fread;
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace FastIO{
struct R{
template<typename T>
R&operator>>(T&from){from=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')from=(from<<3)+(from<<1)+(c^48),c=getchar();from*=f;return*this;}
R&operator>>(double&from){from=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(long double&from){from=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(__float128&from){from=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')from=from*10+(c^48),c=getchar();if(c=='.')c=getchar();else{from*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;from=(from+t)*f;return*this;}
R&operator>>(char&c){c=getchar();while(c==' '||c==endl||c==endr)c=getchar();return*this;}
R&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c==endl||c==endr)c=getchar();while(c!=' '&&c!=endl&&c!=endr)str[len++]=c,c=getchar();str[len]=emp_;return*this;}
R(){}
}cin;
struct W{
const int S=6;
typedef int M;
template<typename T>
W&operator<<(T from){if(from==0){putchar('0');return*this;}if(from<0)putchar('-'),from=-from;static short sta[40];short top=0;while(from>0)sta[++top]=from%10,from/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(long double from){if(from<0)putchar('-'),from=-from;M _=from;from-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(__float128 from){if(from<0)putchar('-'),from=-from;M _=from;from-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<S;i++)from*=10;_=from;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<S-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}
W&operator<<(char c){putchar(c);return*this;}
W&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
W&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}
W(){}
}cout;
}
int max(register const int e,register const int b){register const int from(e),to(b);return(from>to?from:to);}
int min(register const int e,register const int b){register const int from(e),to(b);return(from>to?to:from);}
}
using namespace Fast;
#define cin FastIO::cin
#define cout FastIO::cout
const double EPS=1e-6,PAI=acos(-1.0);
const int MAX=5e5+5,mod=1e9+7,MOD=998244353;
void f(int n,int&x0,int&x1,int&x2){// 计算单个维度n的特征值x0(贡献0)、x1(贡献1)、x2(贡献2)
if(n==1){
x0=0;
x1=0;
x2=1;
}else{
x0=n-2;
x1=2;
x2=0;
}
}
int a,b,c,k,ans=0;
void solve(){
cin>>a>>b>>c>>k;
int a0,a1,a2,b0,b1,b2,c0,c1,c2;
f(a,a0,a1,a2);
f(b,b0,b1,b2);
f(c,c0,c1,c2);
// 枚举所有维度贡献组合
for(int i=0;i<=2;i++){ // 维度a的
for(int j=0;j<=2;j++){ // 维度b的
for(int l=0;l<=2;l++){ // 维度c的
if(i+j+l==k){ // 贡献和等于k时统计数量
int x=(i==0)?a0:(i==1)?a1:a2;
int y=(j==0)?b0:(j==1)?b1:b2;
int z=(l==0)?c0:(l==1)?c1:c2;
ans+=x*y*z;
}
}
}
}
cout<<ans<<endl;
}
signed main(){
int t=1;
// cin>>t;
while(t--)solve();
return 0;
}

浙公网安备 33010602011771号