BZOJ 4807 車 组合数学
思路:高精度\((what)\)
提交:2次(后来发现有种更快的写法)
题解:
设\(n>m\),那么显然答案为\(C(n,m)\),相当于只能放\(m\)个棋子,可以在\(n\)列中选任意不同的\(m\)列上。
刚开始是这种解法:(\(3560ms\))
#include<cstdio>
#include<iostream>
#define ull unsigned long long
#define ll long long
#define R register int
using namespace std;
#define pause (for(R i=1;i<=10000000000;++i))
#define In freopen("NOIPAK++.in","r",stdin)
#define Out freopen("out.out","w",stdout)
namespace Fread {
static char B[1<<15],*S=B,*D=B;
#ifndef JACK
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
#endif
inline int g() {
R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return (ch<=36||ch>=127);}
inline void gs(char* s) {
register char ch; while(isempty(ch=getchar()));
do *s++=ch; while(!isempty(ch=getchar()));
}
} using Fread::g; using Fread::gs;
namespace Luitaryi {
const int N=1000010;
const ll B=1E+10;
int n,m,sz=1,c[N];
ll a[7];
inline void inc(int x) {for(R i=2;i*i<=x;++i) while(x%i==0) x/=i,++c[i]; if(x&&x!=1) ++c[x];}
inline void dec(int x) {for(R i=2;i*i<=x;++i) while(x%i==0) x/=i,--c[i]; if(x&&x!=1) --c[x];}
inline void mul(int x) { register ll tmp=0;
for(R i=1;i<=sz;++i) {
a[i]*=x,a[i]+=tmp;
tmp=a[i]/B,a[i]%=B;
} if(tmp) ++sz,a[sz]=tmp; if(sz>5) sz=5;
}
inline void calc() {for(R i=2;i<=n;++i) while(c[i]) mul(i),--c[i];}
inline void main() {
n=g(),m=g(); m>n?swap(n,m):void(0);
if(m==n) return (void)printf("1\n");
for(R i=n;i>m;--i) inc(i);
for(R i=2;i<=n-m;++i) dec(i);
a[1]=1; calc(); printf("%lld",a[sz]); for(R i=sz-1;i;--i) printf("%010lld",a[i]);
}
}
signed main() {
Luitaryi::main(); return 0;
}
后来看到有这样的:(快的一批\(260ms\))
#include<cstdio>
#include<iostream>
#define ull unsigned long long
#define ll long long
#define R register int
using namespace std;
#define pause (for(R i=1;i<=10000000000;++i))
#define In freopen("NOIPAK++.in","r",stdin)
#define Out freopen("out.out","w",stdout)
namespace Fread {
static char B[1<<15],*S=B,*D=B;
#ifndef JACK
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
#endif
inline int g() {
R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix;
if(ch==EOF) return EOF; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix;
}
} using Fread::g;
namespace Luitaryi {
const int N=1000010;
const ll B=1E+10;
int n,m,sz=1,cnt,c[N],mnd[N],pri[N>>1];
ll a[7];
inline void PRE(int n) {
for(R i=2;i<=n;++i) {
if(!mnd[i]) mnd[i]=i,pri[++cnt]=i;
for(R j=1;j<=cnt&&i*pri[j]<=n;++j) {
mnd[i*pri[j]]=pri[j];
if(i%pri[j]==0) break;
}
}
}
inline void inc(int x) {while(x>1) ++c[mnd[x]],x/=mnd[x];}
inline void dec(int x) {while(x>1) --c[mnd[x]],x/=mnd[x];}
inline void mul(int x) { register ll tmp=0;
for(R i=1;i<=sz;++i) {
a[i]*=x,a[i]+=tmp;
a[i]>=B?tmp=a[i]/B,a[i]%=B:tmp=0;
} if(tmp) a[++sz]=tmp; if(sz>5) sz=5;
}
inline void calc() {for(R i=1;i<=cnt;++i) while(c[pri[i]]) mul(pri[i]),--c[pri[i]];}
inline void main() {
n=g(),m=g(); m>n?swap(n,m):void(0); PRE(n);
if(m==n) return (void)printf("1\n");
for(R i=n;i>m;--i) inc(i);
for(R i=2;i<=n-m;++i) dec(i);
a[1]=1; calc(); printf("%lld",a[sz]); for(R i=sz-1;i;--i) printf("%010lld",a[i]);
}
}
signed main() {
Luitaryi::main(); return 0;
}
\(zz\)忽然感受到我数学白学了
2019.07.23