[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 ...
随机推荐
- Python3爬取小说并保存到文件
问题 python课上,老师给同学们布置了一个问题,因为这节课上学的是正则表达式,所以要求利用python爬取小说网的任意小说并保存到文件. 我选的网站的URL是'https://www.biquka ...
- MySQL select join on 连表查询和自连接查询
连表查询 JOIN ON 操作 描述 inner join 只返回匹配的值 right join 会从右表中返回所有的值, 即使左表中没有匹配 left join 会从左表中返回所有的值, 即使右表中 ...
- ctfshow—web—web3
打开靶机 提示是文件包含漏洞 测试成功 https://d7c9f3d7-64d2-4110-a14b-74c61f65893c.chall.ctf.show/?url=../../../../../ ...
- i春秋新春战疫—web—简单的招聘系统
打开靶机 打开后看到登录界面 利用万能密码,以admin身份登录 登录成功后看到如下界面 在Blank Page界面内发现注入点,抓包 保存在sqlmap目录下test.txt文件夹,使用sqlmap ...
- Apache目录详解
Apache的主要目录和配置文件理解 参考链接:http://httpd.apache.org/docs/2.4/misc/security_tips.html 一.Apache主要配置文件注释(演示 ...
- 本地jar添加到本地仓库 本地jar依赖无效问题
最近工作发生了一个很奇怪的事情,我在本地写了一个项目,打包成jar,然后敲命令mvn install:install-file -DgroupId=com.yzwine -DartifactId=yz ...
- 查看Java的汇编指令
在IDEA配置VM options,打印汇编指令 -XX:+UnlockDiagnosticVMOptions -XX:+PrintAssembly windows系统 下载插件 hsdis-amd6 ...
- 1.8V转3V,1,8V转3.3V电源芯片的规格书参数
1.8V电平如何稳压稳定输出3V或者3.3V,就需要用到1.8V转3V,1,8V转3.3V电源芯片,就PW5100(低功耗,外围简单),PW5200A是可调输出电压,可以输出电压根据外围电阻来设置命令 ...
- 目标检测的评价指标(TP、TN、FP、FN、Precision、Recall、IoU、mIoU、AP、mAP)
1. TP TN FP FN GroundTruth 预测结果 TP(True Positives): 真的正样本 = [正样本 被正确分为 正样本] TN(True Negatives): 真的 ...
- ftp上传文件出现553 Could not creat files 严重文件传输错误
之前上传文件到云服务器上一直出错,发现可以下载但是不能上传和编辑,后来终于找到原因了,是因为上传文件所在文件夹默认只有root用户才有写权限,所以我们还要将写权限赋予给其他用户.可以用Xshell 5 ...