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种宗 ...
随机推荐
- 精选实用 Chrome 扩展(20)
● Reading List 简介:收藏网页,稍后阅读 ● OneTab 简介:收起当前已打开的标签页,需要的时候恢复 ● IE Tab 简介:网页用IE打开 ● uBlock Origin ● Pe ...
- MySQL部分2
- Scratch第四十九讲:完美的下落和反弹
做了很多小游戏,都会遇到碰撞和反弹的情况,CC哥大多时候也都是简单处理一下,包括之前的讲座也有提过,但是没有认真的讲解过.今天就专门为这个主题做一讲,把这部分内容彻底讲透,大家可以一起探讨一下. 是不 ...
- tuxedo10g3R的安装过程(原文url:http://www.cnblogs.com/xianfish/articles/1955244.html)
RHEL 5.1 32Bit 安装配置TUXEDO 10R3 [前言]: 目的: 在Linux上安装配置Tuxedo 10R3 环境: RHEL 5.1 32Bit [前提条件]: 下载Tuxed ...
- diverta 2019 Programming Contest
A:签到. #include<bits/stdc++.h> using namespace std; #define ll long long #define inf 1000000010 ...
- (一)Lucene简介以及索引demo
一.百度百科 Lucene是apache软件基金会4 jakarta项目组的一个子项目,是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查 ...
- (二十一)SpringBoot之集成mybatis:使用mybatis xml
一.引入maven依赖 <dependencies> <dependency> <groupId>org.springframework.boot</grou ...
- CentOS 系统下使用 yum 安装 Redis
本文主要介绍在 CentOS 7 系统下使用 yum 安装 Redis 的过程. 更改 yum 源 将 Centos 的 yum 源更换为国内的阿里云源. 首先备份你的原镜像文件,保证出错后可以恢复: ...
- Tomcat server.xml port server context 配置
- dll安装到GAC以及引用的方法【转】
一 首先 程序集(dll) 安装到 GAC 中的方法 所谓的GAC,就是全局程序集缓存(Global Assembly Cache). 针对一些类库项目或用户控件项目在程序开发完成后,有时需要将 ...