题意:

给你一个n*n的蛋糕,如果某个位置是'C'那就代表这是一个巧克力块,否则就不是。如果某两个巧克力块在同一行或同一列,那么这个家庭的幸福值就会加1,问你这个家庭的幸福值最大是多少

Input
3
.CC
C..
C.C
Output
4

Input
4
CC..
C..C
.CC.
.CC.
Output
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. (1, 2) and (1, 3)
  2. (3, 1) and (3, 3)

Pieces that share the same column are:

  1. (2, 1) and (3, 1)
  2. (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 用阶乘会爆掉的更多相关文章

  1. sqoop关系型数据迁移原理以及map端内存为何不会爆掉窥探

    序:map客户端使用jdbc向数据库发送查询语句,将会拿到所有数据到map的客户端,安装jdbc的原理,数据全部缓存在内存中,但是内存没有出现爆掉情况,这是因为1.3以后,对jdbc进行了优化,改进j ...

  2. linux调整缓存写入磁盘的时间,减少磁盘爆掉的可能性

    缓存数据存入磁盘的最长时间,如果这段时间写不完,就会报异常停止写,这样缓存数据会不断积累,导致内存爆掉. echo 0 > /proc/sys/kernel/hung_task_timeout_ ...

  3. 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 ...

  4. 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 ...

  5. 一个sql导致temp表空间爆掉

    Buffer sort引发的血案 今天遇到的一个问题,在线系统上,有两张表,test1大概50G,test2大概200G,需要查询出来test1表中部分记录,并且这些记录不存在test2表中.于是就写 ...

  6. Codeforces Round 480 Div 2 光荣掉分记

    痛 痛苦 痛苦啊. 越接近黄名想的越多了啊…… 都说了不要在意rating这破玩意了…… 没出E就算了,策略问题. 居然还FST了: FST个D就算了: FST个A算个**啊. 紧张的时候总会写出一些 ...

  7. hdu-5651 xiaoxin juju needs help(数学+gcd约分求阶乘)

    题目链接: xiaoxin juju needs help Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  8. 【codeforces 749E】 Inversions After Shuffle

    http://codeforces.com/problemset/problem/749/E (题目链接) 题意 给出一个1~n的排列,从中等概率的选取一个连续段,设其长度为l.对连续段重新进行等概率 ...

  9. Codeforces Round #404 (Div. 2)A,B,C

    A. Anton and Polyhedrons 题目链接:http://codeforces.com/contest/785/problem/A 智障水题 实现代码: #include<bit ...

随机推荐

  1. LeetCode 371两数之和

    题目描述: 不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a .b ​​​​​​​之和. 思路: 既然不能使用运算符操作就要考虑到,位运算的加法. 加法有进位的时候和不进位的时候 ...

  2. 【ORA】Specified value of MEMORY_TARGET is too small, needs to be at least 3072M解决办法

    今天安装EM12C的时候遇到了一个报错: 修改好数据库中的参数大小后,重新启动报错 Specified value of MEMORY_TARGET is too small, needs to be ...

  3. yml文件中${DB_HOST:localhost}的含义

    引自:https://blog.csdn.net/chen462488588/article/details/109057342 今天学习eladmin项目中看到application-dev.yml ...

  4. 命名秘籍周获近五千星——GitHub 热点速览 v.21.04

    作者:HelloGitHub-小鱼干 命名一直是编程界的难点,这次 naming-cheatsheet 就能帮上你的忙.按照它的 SID(Short..Intuitive.Descriptive)原则 ...

  5. windows 系统 MySQL_5.6.21安装教程

      1.双击安装文件 mysql_installer_community_V5.6.21.1_setup.1418020972.msi,等待安装界面出现,见下图: 2.勾选:I accept thel ...

  6. jQuery 勾选显示

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. BigDecimal add方法问题:调用add后,求和结果没变

    import java.math.BigDecimal; public class DecimalAdd { public static void main(String[] args) { BigD ...

  8. 手把手教你定位常见Java性能问题

    概述 性能优化一向是后端服务优化的重点,但是线上性能故障问题不是经常出现,或者受限于业务产品,根本就没办法出现性能问题,包括笔者自己遇到的性能问题也不多,所以为了提前储备知识,当出现问题的时候不会手忙 ...

  9. Language Guide (proto3) | proto3 语言指南(十五)生成类

    Generating Your Classes - 生成类 要生成Java.Python.C++.Go.Ruby.ObjuleC或C代码,需要使用.proto文件中定义的消息类型,还需要在.proto ...

  10. Java网络基础

    本来主要是讲自己在网络编程方面的学习总结,里面主要讲计算网络的基础.TCP的通信协议,还有些简单的案例.下面是我学习的一个简单路线, 一.概述 计算机网络是将不同地理位置的具有独立功能的多台计算机及其 ...