思路:高精度\((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 車 组合数学的更多相关文章

  1. bzoj 4807: 車【组合数+高精+线性筛】

    设n>m,答案是\( C_n^m \),然后高精就行了 具体做法是先把指数筛出来,然后对每个数因数分解,记录质因子个数,最后被除数减去除数质因子个数,把剩下的质因子乘起来就行了 #include ...

  2. BZOJ4807:車(组合数学,高精度)

    Description 众所周知,車是中国象棋中最厉害的一子之一,它能吃到同一行或同一列中的其他棋子.車跟車显然不能在一起打起来,于是rly一天又借来了许多许多的車在棋盘上摆了起来……他想知道,在N× ...

  3. BZOJ 3997: [TJOI2015]组合数学 [偏序关系 DP]

    3997: [TJOI2015]组合数学 题意:\(n*m:\ n \le 1000\)网格图,每个格子有权值.每次从左上角出发,只能向下或右走.经过一个格子权值-1.至少从左上角出发几次所有权值为0 ...

  4. bzoj 3907: 网格 组合数学

    3907: 网格 Time Limit: 1 Sec  Memory Limit: 256 MBSubmit: 13  Solved: 7[Submit][Status][Discuss] Descr ...

  5. bzoj 3997 [TJOI2015]组合数学(DP)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3997 [题意] 给定一个nm的长方形,每次只能使经过格子权值减1,每次只能向右向下,问 ...

  6. BZOJ 3997 TJOI2015 组合数学

    分析一下样例就可以知道,求的实际上是从左下角到右上角的最长路 因为对于任意不在这个最长路的上的点,都可以通过经过最长路上的点的路径将这个点的价值减光 (可以用反证法证明) 之后就是一个非常NOIP的D ...

  7. BZOJ 1008 越狱 (组合数学)

    题解:正难则反,从总数中减去全部相邻不相同的数目就是答案,n*(n-1)^(m-1):第一个房间有n中染色方案,剩下m-1个房间均只有n-1种染色方案,用总数减就是答案. #include <c ...

  8. BZOJ 2142 礼物 组合数学 CRT 中国剩余定理

    2142: 礼物 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1450  Solved: 593[Submit][Status][Discuss] ...

  9. BZOJ 1008 越狱 组合数学

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1008 题目大意: 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗 ...

随机推荐

  1. C++程序设计学习-第2章

    第二章 变量与基本类型 1.基本内置类型 C++定义了一套包括算术类型和空类型在内的基本数据类型 算术类型:整型和浮点型,包括带符号类型(signed)和无符号类型(unsigned),带符号类型可以 ...

  2. PAT甲级 堆 相关题_C++题解

    堆 目录 <算法笔记>重点摘要 1147 Heaps (30) 1155 Heap Paths (30) <算法笔记> 9.7 堆 重点摘要 1. 定义 堆是完全二叉树,树中每 ...

  3. Jupyter修改工作目录(Anaconda环境)

    Anaconda安装时未添加环境变量 1.打开Anaconda Prompt 输入jupyter notebook --generate-config (base) C:\Users\Sroxi> ...

  4. 笔记-5:mysql数据更新

    1.插入数据 1.1 插入完整的数据 # 语法格式: INSERT INTO tb_name(column_list) VALUES(value_list); tb_name:指定要插入数据的表名. ...

  5. 1266: gcd和lcm(Java)

    WUSTOJ 1266: gcd和lcm 参考 1naive1的博客 Description   已知a,b的最大公约数为x,也即gcd(a,b)=x; a,b的最小公倍数为y,也即lcm(a,b)= ...

  6. DMA—直接存储器访问

    DMA 简介 DMA(Direct Memory Access)—直接存储器存取,是单片机的一个外设,它的主要功能是用来搬数据,但是不需要占用 CPU,即在传输数据的时候,CPU 可以干其他的事情,好 ...

  7. MogileFS安装

    MogileFS是一款开源的.高性能的.分布式的文件系统,用于组建分布式文件集群.用来存取海量文件,而不用关心具体的文件存放位置.存储容量大小,以及文件损坏和丢失等问题 MogileFS有三大部分:存 ...

  8. This is very likely to create a memory leak. Stack trace of thread错误分析

    1.问题描述 启动tomcat部署项目时,报This is very likely to create a memory leak. Stack trace of thread错误. 29-May-2 ...

  9. Linux每隔1秒kill掉cpu大于50%的进程

    1.新建/test/killcpu.sh shell脚本 并授予权限0755#!/bin/bashps axf -o "pid %cpu" | awk '{if($2>=50 ...

  10. Abp 添加权限项<一>

    1.下载代码,数据库迁移,npm install 2.添加权限项: public static class PermissionNames { public const string Pages_Te ...