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
BZOJ 4807 車 组合数学的更多相关文章
- bzoj 4807: 車【组合数+高精+线性筛】
设n>m,答案是\( C_n^m \),然后高精就行了 具体做法是先把指数筛出来,然后对每个数因数分解,记录质因子个数,最后被除数减去除数质因子个数,把剩下的质因子乘起来就行了 #include ...
- BZOJ4807:車(组合数学,高精度)
Description 众所周知,車是中国象棋中最厉害的一子之一,它能吃到同一行或同一列中的其他棋子.車跟車显然不能在一起打起来,于是rly一天又借来了许多许多的車在棋盘上摆了起来……他想知道,在N× ...
- BZOJ 3997: [TJOI2015]组合数学 [偏序关系 DP]
3997: [TJOI2015]组合数学 题意:\(n*m:\ n \le 1000\)网格图,每个格子有权值.每次从左上角出发,只能向下或右走.经过一个格子权值-1.至少从左上角出发几次所有权值为0 ...
- bzoj 3907: 网格 组合数学
3907: 网格 Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 13 Solved: 7[Submit][Status][Discuss] Descr ...
- bzoj 3997 [TJOI2015]组合数学(DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题意] 给定一个nm的长方形,每次只能使经过格子权值减1,每次只能向右向下,问 ...
- BZOJ 3997 TJOI2015 组合数学
分析一下样例就可以知道,求的实际上是从左下角到右上角的最长路 因为对于任意不在这个最长路的上的点,都可以通过经过最长路上的点的路径将这个点的价值减光 (可以用反证法证明) 之后就是一个非常NOIP的D ...
- BZOJ 1008 越狱 (组合数学)
题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案. #include <c ...
- BZOJ 2142 礼物 组合数学 CRT 中国剩余定理
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 1450 Solved: 593[Submit][Status][Discuss] ...
- BZOJ 1008 越狱 组合数学
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1008 题目大意: 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗 ...
随机推荐
- 关于php发送邮件(PHPmailer)的傻瓜式操作
首先打开QQ邮箱(此处我们以QQ邮箱为例) 点击设置里面的账户开启pop3和smtp(此处需要用到绑定的手机号进行短信或QQ安全中心动态码进行验证) 接着复制以下email代码 //发送邮件 publ ...
- QT聊天室--重大bug
发送qqqqqqqqqqqqqqqqqqqqqqq: 发送test
- python __enter__ 与 __exit__的作用,以及与 with 语句的关系(转)
https://blog.csdn.net/xc_zhou/article/details/80810111 python __enter__ 与 __exit__的作用,以及与 with 语句的关系
- List 集合的常用方法总结
@org.junit.Test public void testListToCompare() { List<String> list1 = new ArrayList<>() ...
- cnn健康增胖和调理好身体
吃不胖,其实大部分情况是消化系统不好,大部分食物都没有被身体吸收就被排掉了. 1,改善肠胃消化功能: 每天早上一杯全脂鲜牛奶(或者羊奶), 每天晚上一杯酸奶 ps:白天和鲜牛奶可以激发肠胃的消化能力. ...
- Kong命令(二)service
service介绍: service 是声明了一组name.host.port.protocol等配置的函数.可以绑定route.upstream上下游服务.并且对于route.upstream可以绑 ...
- springboot2.0介绍1
SpringBoot 一. Spring介绍 1.1.SpringBoot简介 在您第1次接触和学习Spring框架的时候,是否因为其繁杂的配置而退却了?在你第n次使用Spring框架的时候,是否觉得 ...
- Go微服务 grpc的简单使用
作者:薇文文链接:https://www.jianshu.com/p/20ed82218163来源:简书 准备工作 先安装Protobuf 编译器 protoc,下载地址:https://github ...
- sql server存储过程返回数据只有一个字符
SqlParameter[] param = { new SqlParameter("@shopId",shopId), new SqlParameter("@newSh ...
- Windows服务 + Quartz.NET
服务基础 安装管理员打开cmd cd C:\Windows\Microsoft.NET\Framework\v4.0.30319 InstallUtil.exe Path_WinSvc.exe 或者 ...