http://www.lydsy.com/JudgeOnline/problem.php?id=1853
在我年轻的时候,我一直以为这是一道水数位DP
直到我今天搜了下题解。。。
知道是容斥原理就好办了,有个倍数关系的剪枝我没想到
在bzoj上实测是真真正正倒数第一卡过去2108ms。。。。可以说相当有水平。。。。。
理论上算法的时间复杂度是O(2^n)哈哈哈哈
但是这个n很小,理论n*10实际*2,但也是线性增长。。。。。
dfs搜索+强剪枝,于是快了许多
我搜索太弱了啊,不过枚举子集还是没问题的
先估计了下时限
//#define _TEST _TEST #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath> #include <algorithm> using namespace std; /************************************************ Code By willinglive Blog:http://willinglive.cf ************************************************/ #define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++) #define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--) #define MS(arr,x) memset(arr,x,sizeof(arr)) #define LL unsigned long long #define INE(i,u,e) for(int i=head[u];~i;i=e[i].next) inline const LL read() {LL r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1; for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;} ///////////////////////////////////////////////// LL a,b; LL c[2050],cnt; int clock; bool del[2050]; ///////////////////////////////////////////////// bool cmp(LL a,LL b){return a>b;} LL gcd(LL a,LL b){return b?gcd(b,a%b):a;} inline LL lcm(LL a,LL b){return a/gcd(a,b)*b;} void dfs1(LL n) { if(n>b) return; c[++cnt]=n; dfs1(n*10+6); dfs1(n*10+8); } void dfs2(LL n,int pos,int x) { clock++; if(n>b || pos>cnt) return; LL nn=lcm(n,c[pos]); if(nn<=b) dfs2(nn,pos+1,-x); dfs2(n,pos+1,x); } ///////////////////////////////////////////////// void input() { a=read(); b=read(); } void solve() { dfs1(6); dfs1(8); sort(&c[1],&c[cnt+1],cmp); printf("old cnt=%d\n",cnt); per(i,cnt,1) per(j,i-1,1) if(c[j]%c[i]==0) del[j]=1; int tot=0; rep(i,1,cnt) if(!del[i]) c[++tot]=c[i]; cnt=tot; printf("new cnt=%d\n",cnt); dfs2(1,1,-1); printf("%d\n",clock); } ///////////////////////////////////////////////// int main() { #ifndef _TEST freopen("std.in","r",stdin); freopen("std.out","w",stdout); #endif input(),solve(); return 0; }
运行下,貌似可以卡过去,开始容斥吧~
rank挺靠前的
//#define _TEST _TEST #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include <cmath> #include <algorithm> using namespace std; /************************************************ Code By willinglive Blog:http://willinglive.cf ************************************************/ #define rep(i,l,r) for(int i=l,___t=(r);i<=___t;i++) #define per(i,r,l) for(int i=r,___t=(l);i>=___t;i--) #define MS(arr,x) memset(arr,x,sizeof(arr)) #define LL unsigned long long #define INE(i,u,e) for(int i=head[u];~i;i=e[i].next) inline const LL read() {LL r=0,k=1;char c=getchar();for(;c<'0'||c>'9';c=getchar())if(c=='-')k=-1; for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return k*r;} ///////////////////////////////////////////////// LL a,b; LL c[2050],cnt; //int clock; bool del[2050]; LL ans=0; ///////////////////////////////////////////////// bool cmp(LL a,LL b){return a>b;} LL gcd(LL a,LL b){return b?gcd(b,a%b):a;} inline LL lcm(LL a,LL b){return a/gcd(a,b)*b;} void dfs1(LL n) { if(n>b) return; c[++cnt]=n; dfs1(n*10+6); dfs1(n*10+8); } void dfs2(LL n,int pos,int x) { //clock++; if(n>b) return; if(pos>cnt) { if(n==1) return; ans+=x*(b/n); ans-=x*(a/n); //cout<<n<<" "<<x<<endl; return; } LL nn=lcm(n,c[pos]); if(nn<=b) dfs2(nn,pos+1,-x); dfs2(n,pos+1,x); } ///////////////////////////////////////////////// void input() { a=read(); b=read(); a--; } void solve() { dfs1(6); dfs1(8); sort(&c[1],&c[cnt+1],cmp); per(i,cnt,1) per(j,i-1,1) if(c[j]%c[i]==0) del[j]=1; int tot=0; rep(i,1,cnt) if(!del[i]) c[++tot]=c[i]; cnt=tot; dfs2(1,1,-1); cout<<ans<<endl; } ///////////////////////////////////////////////// int main() { #ifndef _TEST freopen("std.in","r",stdin); freopen("std.out","w",stdout); #endif input(),solve(); return 0; }
update Dec.29
尼玛这题不是裸的容斥原理嘛,我还想这么多。。。我是怎么想到数位DP的。。。。。。