POJ-1942-Paths on a Grid-组合水题
2016-01-23 10:41
363 查看
http://poj.org/problem?id=1942
求C(n+m,n)
这题数据有点特殊....
C(a,b)的b 取min(b,a-b)就能ac了。。。
求组合只能实打实边乘边除了。。用ll或double足矣
double:
求C(n+m,n)
这题数据有点特殊....
C(a,b)的b 取min(b,a-b)就能ac了。。。
求组合只能实打实边乘边除了。。用ll或double足矣
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <iostream> using namespace std; __int64 inf=15; double eps=0.000001; __int64 C(__int64 a,__int64 b) { __int64 ans=1; if (b<a-b) b=a-b; __int64 i; __int64 tmp=a-b; for (i=a;i>b;i--) { ans*=i; while(tmp>1&& ans%tmp==0 ) {ans/=tmp;tmp--;} } return ans; } int main() { __int64 n,m; while(scanf("%I64d%I64d",&n,&m)!=EOF) { if (!n&&!m) break; __int64 ans=1; __int64 tmp=n+m; ans=C(tmp,n); printf("%I64d\n",ans); } return 0; }
double:
#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <iostream> using namespace std; __int64 inf=15; double eps=0.000001; __int64 C(__int64 a,__int64 b) { double ans=1; if (b<a-b) b=a-b; __int64 i; __int64 tmp=a-b; for (i=a;i>b;i--) { ans*=i; while(tmp>1&& (__int64)(ans)%tmp==0 ) { ans=ans/ (double)tmp; ans=(__int64)(ans+0.5);tmp--;} } return (__int64)(ans+0.5); } int main() { __int64 n,m; while(scanf("%I64d%I64d",&n,&m)!=EOF) { if (!n&&!m) break; __int64 ans=1; __int64 tmp=n+m; ans=C(tmp,n); printf("%I64d\n",ans); } return 0; }
相关文章推荐
- OpenStack设计与实现(四)消息总线(AMQP)
- Springmvc学习篇(3)之参数绑定
- List<T>与DataTable相互转换
- lightoj 1281 - New Traffic System
- VS集成Qt环境搭建
- [实战]MVC5+EF6+MySql企业网盘实战(28)——其他列表
- C++之路进阶——差分约束(FFF的后宫)
- android ImageView点击时背景图修改(简单方法)
- 理论: 二分查找(2): 假定一个解并判断是否可行
- Android布局
- Codeforce 614A Link/Cut Tree 水题
- [微积分学习]Day 3.极限不等于A及例题
- libcurl使用1:用get请求方式+图灵机器人接口实现智能聊天系统
- JavaScript实现ASC转汉字及汉字转ASC的方法
- VMware 中很多ATA ChannelX 问题解决方法
- MT6580/8321进摄像头白屏现象
- 入坑单片机第一天
- 查找 替换 细节
- 查看SUSE LINUX版本
- 个人收藏-Android样式的开发