Topcoder SRM403 Division I Level Three
题解:
错误的解法:
dp
d
p
预处理
n≤106
n
≤
10
6
的答案,然后迭代加深爆搜,发现无论怎么加优化都会T掉。
正确的解法:
按位搜索,枚举每一位上有几个4和几个7,然后在确定了每一位上4和7的个数后,当前的最优解可以直接
O(92)
O
(
9
2
)
求得。加上一些可行性剪枝即可轻松通过。
#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define ULL ull
#define mp make_pair
#define pii pair<int,int>
#define piii pair<int, pii >
#define pll pair <ll,ll>
#define pb push_back
#define big 20160116
#define INF 2147483647
#define pq priority_queue
using namespace std;
inline int read(){
int x=0,f=1;
char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
namespace Mymath{
LL qp(LL x,LL p,LL mod){
LL ans=1;
while (p){
if (p&1) ans=ans*x%mod;
x=x*x%mod;
p>>=1;
}
return ans;
}
LL inv(LL x,LL mod){
return qp(x,mod-2,mod);
}
LL C(LL N,LL K,LL fact[],LL mod){
return fact[N]*inv(fact[K],mod)%mod*inv(fact[N-K],mod)%mod;
}
template <typename Tp> Tp gcd(Tp A,Tp B){
if (B==0) return A;
return gcd(B,A%B);
}
template <typename Tp> Tp lcm(Tp A,Tp B){
return A*B/gcd(A,B);
}
};
namespace fwt{
using namespace Mymath;
void FWT(int a[],int n,LL mod)
{
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=(x+y)%mod,a[i+j+d]=(x-y+mod)%mod;
//xor:a[i+j]=x+y,a[i+j+d]=x-y;
//and:a[i+j]=x+y;
//or:a[i+j+d]=x+y;
}
}
void UFWT(int a[],int n,LL mod)
{
LL rev=inv(2,mod);
for(int d=1;d<n;d<<=1)
for(int m=d<<1,i=0;i<n;i+=m)
for(int j=0;j<d;j++)
{
int x=a[i+j],y=a[i+j+d];
a[i+j]=1LL*(x+y)*rev%mod,a[i+j+d]=(1LL*(x-y)*rev%mod+mod)%mod;
//xor:a[i+j]=(x+y)/2,a[i+j+d]=(x-y)/2;
//and:a[i+j]=x-y;
//or:a[i+j+d]=y-x;
}
}
void solve(int a[],int b[],int n,LL mod)
{
FWT(a,n,mod);
FWT(b,n,mod);
for(int i=0;i<n;i++) a[i]=1LL*a[i]*b[i]%mod;
UFWT(a,n,mod);
}
};
const int Maxn=15;
int w[Maxn],n;
int c4[Maxn],c7[Maxn];
int tc4[Maxn],tc7[Maxn];
int p10[Maxn];
int curans;
vector<int> ans;
int Pc(int &a,int&b){
if(a){
a--;
return 4;
}
b--;//assert(b>=0);
return 7;
}
void check(){
int Mx=0;
for (int i=0;i<10;i++) Mx=max(Mx,c4[i]+c7[i]);
if (Mx>curans) return;
//cout<<c4[0]<<c7[0]<<endl;
vector<int> tmp;
for (int i=0;i<10;i++) tc4[i]=c4[i],tc7[i]=c7[i];
for (int i=0;i<9;i++){
int tx=c4[i]+c7[i]-c4[i+1]-c7[i+1];
for (int j=0;j<tx;j++){
int t=0;
for (int k=0;k<=i;k++){
t+=p10[k]*Pc(c4[k],c7[k]);
}
tmp.pb(t);
}
}
for (int i=0;i<10;i++) c4[i]=tc4[i],c7[i]=tc7[i];
//cout<<c4[0]<<' '<<c7[0]<<endl;
if (Mx<curans){
curans=Mx;
ans=tmp;
return;
}
for (int i=0;i<ans.size();i++){
if (tmp[i]<ans[i]){
break;
}
if (tmp[i]>ans[i]){
return;
}
}
ans=tmp;
return;
}
void Go(int lvl,int cnt){
//cout<<lvl<<cnt<<endl;
if (lvl==9){
if (w[lvl]==0){
//cout<<c4[0]<<' '<<c7[0]<<endl;
check();
}
return;
}
for (int i=0;i<=cnt;i++){
for (int j=0;j<=cnt-i;j++){
//if (!i && !j) continue;
int v=i*4+j*7-w[lvl];
if (v%10!=0) continue;
//if (lvl==0) cout<<i<<j<<endl;
int pv=w[lvl];
w[lvl]=0;
w[lvl+1]-=v/10;
c4[lvl]=i;c7[lvl]=j;
Go(lvl+1,i+j);
//c4[lvl]=c7[lvl]=0;
w[lvl]=pv;
w[lvl+1]+=v/10;
}
}
}
class TheLuckySum{
public:
vector<int> sum(int N){
n=N;curans=1e9;
p10[0]=1;
for (int i=1;i<10;i++) p10[i]=p10[i-1]*10;
for (int i=0;i<10;i++){
w[i]=N%10;
N/=10;
}
Go(0,14);
//cout<<curans<<endl;
return ans;
}
}tst;
/*
int main(){
vector<int> as=tst.sum(998244353);
for (int i=0;i<as.size();i++){
cout<<as[i]<<endl;
}
}*/