[CodeForces-629A 用阶乘会爆掉
题意:
给你一个n*n的蛋糕,如果某个位置是'C'那就代表这是一个巧克力块,否则就不是。如果某两个巧克力块在同一行或同一列,那么这个家庭的幸福值就会加1,问你这个家庭的幸福值最大是多少
3
.CC
C..
C.C
4
4
CC..
C..C
.CC.
.CC.
9
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:
- (1, 2) and (1, 3)
- (3, 1) and (3, 3)
Pieces that share the same column are:
- (2, 1) and (3, 1)
- (1, 3) and (3, 3)
题解:
原本写的是先统计一下每一行每一列上巧克力块的个数,然后对于一行或一列用排列组合方式求出来有多少巧克力对,比如某行或某列有n块巧克力,那么巧克力对数就是C2n
但是这种方法要求阶乘,会爆掉long long
WA代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<math.h>
6 #include<vector>
7 #include<queue>
8 #include<map>
9 using namespace std;
10 typedef long long ll;
11 const int maxn=110;
12 const int INF=0x3f3f3f3f;
13 char s[maxn][maxn];
14 ll row[maxn],col[maxn],result[maxn];
15 int main()
16 {
17 ll n;
18 scanf("%lld",&n);
19 result[1]=result[0]=1;
20 for(ll i=2;i<=n;++i)
21 {
22 result[i]=result[i-1]*i;
23 }
24 for(ll i=0;i<n;++i)
25 {
26 scanf("%s",s[i]);
27 }
28 for(ll i=0;i<n;++i)
29 {
30 for(ll j=0;j<n;++j)
31 {
32 if(s[i][j]=='C')
33 row[i]++,col[j]++;
34 }
35 }
36 ll sum=0;
37 for(ll i=0;i<n;++i)
38 {
39 //printf("%lld**\n",result[row[i]]);
40 if(row[i]>=2)
41 sum=sum+result[row[i]]/(2*result[row[i]-2]);
42 }
43 for(ll i=0;i<n;++i)
44 {
45 //printf("%lld****\n",result[col[i]]);
46 if(col[i]>=2)
47 sum=sum+result[col[i]]/(2*result[col[i]-2]);
48 }
49 printf("%lld\n",sum);
50 return 0;
51 }
我没有用快速乘和边乘边约分去优化,感觉用的话也可以过。。。但是还要打板子,,我换了一种方式
用时间换空间,暴力去找有多少对,,具体看代码
代码:
1 #include<stdio.h>
2 #include<string.h>
3 #include<iostream>
4 #include<algorithm>
5 #include<math.h>
6 #include<vector>
7 #include<queue>
8 #include<map>
9 using namespace std;
10 typedef long long ll;
11 const int maxn=110;
12 const int INF=0x3f3f3f3f;
13 ll n;
14 ll Map[maxn][maxn];
15 char s[maxn][maxn];
16 ll dfs(ll x, ll y)
17 {
18 ll xx = 0, yy = 0;
19 for (ll i = x + 1; i < n; i++)
20 {
21 if (Map[i][y])
22 {
23 xx++;
24 }
25 }
26 for (ll i = y + 1; i < n; i++)
27 {
28 if (Map[x][i])
29 {
30 yy++;
31 }
32 }
33 return xx + yy;
34 }
35 int main()
36 {
37 ll sum=0;
38 scanf("%lld",&n);
39 for(ll i=0;i<n;++i)
40 scanf("%s",s[i]);
41 for (ll i = 0; i < n; i++)
42 {
43 for (ll j = 0; j < n; j++)
44 {
45 if (s[i][j]=='C')
46 {
47 Map[i][j] = 1;
48 }
49 }
50 }
51 for (ll i = 0; i < n; i++)
52 {
53 for (ll j = 0; j < n; j++)
54 {
55 if (Map[i][j])
56 {
57 sum += dfs(i, j);
58 }
59 }
60 }
61 printf("%lld\n",sum);
62 return 0;
63 }
[CodeForces-629A 用阶乘会爆掉的更多相关文章
- sqoop关系型数据迁移原理以及map端内存为何不会爆掉窥探
序:map客户端使用jdbc向数据库发送查询语句,将会拿到所有数据到map的客户端,安装jdbc的原理,数据全部缓存在内存中,但是内存没有出现爆掉情况,这是因为1.3以后,对jdbc进行了优化,改进j ...
- linux调整缓存写入磁盘的时间,减少磁盘爆掉的可能性
缓存数据存入磁盘的最长时间,如果这段时间写不完,就会报异常停止写,这样缓存数据会不断积累,导致内存爆掉. echo 0 > /proc/sys/kernel/hung_task_timeout_ ...
- Codeforces Round #670 (Div. 2) 深夜掉分(A - C题补题)
1406A. Subset Mex https://codeforces.com/contest/1406/problem/A Example input 4 6 0 2 1 5 0 1 3 0 1 ...
- codeforces 629A Far Relative’s Birthday Cake
A. Far Relative’s Birthday Cake time limit per test 1 second memory limit per test 256 megabytes inp ...
- 一个sql导致temp表空间爆掉
Buffer sort引发的血案 今天遇到的一个问题,在线系统上,有两张表,test1大概50G,test2大概200G,需要查询出来test1表中部分记录,并且这些记录不存在test2表中.于是就写 ...
- Codeforces Round 480 Div 2 光荣掉分记
痛 痛苦 痛苦啊. 越接近黄名想的越多了啊…… 都说了不要在意rating这破玩意了…… 没出E就算了,策略问题. 居然还FST了: FST个D就算了: FST个A算个**啊. 紧张的时候总会写出一些 ...
- hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)
题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- 【codeforces 749E】 Inversions After Shuffle
http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...
- Codeforces Round #404 (Div. 2)A,B,C
A. Anton and Polyhedrons 题目链接:http://codeforces.com/contest/785/problem/A 智障水题 实现代码: #include<bit ...
随机推荐
- Writing in the science: Introducion
1.what makes a good writing? 2.what makes a good writer? 1) have something to say 2) logical thinkin ...
- logback为不同的包或类指定输出日志文件
对日志分割的常见需求是,需要按不同的等级进行输出,这个的配置方式类似如下,在appender节点内添加内容 <appender name="FILE-INFO" class= ...
- [从源码学设计]蚂蚁金服SOFARegistry之配置信息
[从源码学设计]蚂蚁金服SOFARegistry之配置信息 目录 [从源码学设计]蚂蚁金服SOFARegistry之配置信息 0x00 摘要 0x01 业务范畴 1.1 配置作用 1.2 学习方向 0 ...
- Nginx和Tomcat配置SSL实现https访问
环境:CentOS 7 Nginx版本: nginx/1.18.0 1. 安装nginx 详细步骤可以参考如下官网:http://nginx.org/en/linux_packages.html#RH ...
- 同步与异步 Python 有何不同?
你是否听到人们说过,异步 Python 代码比"普通(或同步)Python 代码更快?果真是那样吗? 1 "同步"和"异步"是什么意思? Web 应用 ...
- (02)-Python3之--列表(list)操作
1.定义 列表的关键字:list 列表以[]括起来,数据之间用 , 隔开.列表当中的数据,可以是任意类型.数值是可以重复的. 列表元素是 可变的,顺序是 有序的. 例如: b = ["萝卜& ...
- 写给 Poppy 的 MySQL 速查表
昨天 Poppy 问我是不是应该学一些网页开发的东西, 我的回答是这样的: 今天花了点时间汇总了一些 MySQL 简单的命令. ======== 正文分割线 ======== 有哪些常见的数据库: O ...
- 一文打尽 Linux/Windows端口复用实战
出品|MS08067实验室(www.ms08067.com) 本文作者:Spark(Ms08067内网安全小组成员) 定义:端口复用是指不同的应用程序使用相同端口进行通讯. 场景:内网渗透中,搭建隧道 ...
- vercel是什么神仙网站?
Vercel? vercel是我用过的最好用的网站托管服务.本网站就是基于hexo引擎模板开发,托管在vercel上的. vercel类似于github page,但远比github page强大,速 ...
- 异步日志 Loguru
https://mp.weixin.qq.com/s/hy68s610B9GbL_wgwTn7nA 更优美的python日志管理库Loguru Asynchronous, Thread-safe, M ...