51nod P1305 Pairwise Sum and Divide ——思路题
久しぶり!
发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题。。(在大佬题解的帮助下)
原题戳>>https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1305<<
有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:fun(A)sum = 0for i = 1 to A.lengthfor j = i+1 to A.lengthsum = sum + Floor((A[i]+A[j])/(A[i]*A[j]))return sum给出数组A,由你来计算fun(A)的结果。例如:A = {1, 4, 1},fun(A) = [5/4] + [2/1] + [5/4] = 1 + 2 + 1 = 4。Input第1行:1个数N,表示数组A的长度(1 <= N <= 100000)。
第2 - N + 1行:每行1个数A[i](1 <= A[i] <= 10^9)。Output输出fun(A)的计算结果。Input示例
3
1 4 1
Output示例4
一开始看到这个公式,直接脑补到了求并联电路电阻和的公式,然后神TM在这上面找规律。。。然后gg
其实这题的数字中只有1和2是有作用的。从公式里可以看出,两者都大于2的所有组合在向下取整后都会为0
例如5 7 ——>5+7/5x7<1--->0
而2和2组合则是1 2 2——>2+2/2*2=1
1和其他如何数组合都是1
所以这题统计一下3个数据——1的数目、2的数目、其他数数目(总数减前两者)即可。
之后再把他们按前面的结论组合求解即可,代码如下:
#include<iostream>
#include<stdio.h>
using namespace std;
int a[];
int main()
{
int n,i,j,e1=,e2=;
cin>>n;
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]==) e1++;
if(a[i]==) e2++;
}
long long ans=;
ans+=(e1-)*(e1-)+(e1-);
ans+=e1*(n-e1);
ans+=((e2-)*(e2-)+(e2-))/;
cout<<ans<<endl;
}
51nod P1305 Pairwise Sum and Divide ——思路题的更多相关文章
- 51Nod 1305 Pairwise Sum and Divide | 思维 数学
Output 输出fun(A)的计算结果. Input示例 3 1 4 1 Output示例 4 first try: #include "bits/stdc++.h" using ...
- [51nod] 1305 Pairwise Sum and Divide 数学
有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = ...
- 51nod 1305 Pairwise Sum and Divide
有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = 0 for i = 1 to A.length for j = ...
- 51nod1305 Pairwise Sum and Divide
题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...
- Pairwise Sum and Divide 51nod
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有这样 ...
- 51nod 1305:Pairwise Sum and Divide
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有这样一段 ...
- 1305 Pairwise Sum and Divide
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对 ...
- 1305 Pairwise Sum and Divide(数学 ,规律)
HackerRank 1305 Pairwise Sum and Divide 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = ...
- 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏
1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...
随机推荐
- APIView源码解析
1.首先安装pip install djangorestframework 2.导入from rest_framework.views import APIView class Courses(API ...
- gitlab提交内容关联到slack通知
gitlab提交内容关联到slack通知 https://docs.gitlab.com/ee/user/project/integrations/slack.html 首先去slack做相关的设置 ...
- php CI框架中URL特殊字符处理与SQL注入隐患
php CI框架中URL特殊字符处理与SQL注入隐患 php CI框架中URL特殊字符有很多是不支持的,导致像c++,括号这些常用的分类,字符都无法正常显示很头痛,而在配置里增加单引号' 反斜杠\ 这 ...
- Codeforces 456A - Laptops
题目链接:http://codeforces.com/problemset/problem/456/A One day Dima and Alex had an argument about the ...
- Elasticsearch集群内的原理
一个运行中的 Elasticsearch 实例称为一个 节点,而集群是由一个或者多个拥有相同 cluster.name 配置的节点组成, 它们共同承担数据和负载的压力.当有节点加入集群中或者从 ...
- 前端框架VUE----表单输入绑定
vue的核心:声明式的指令和数据的双向绑定. 那么声明式的指令,已经给大家介绍完了.接下来我们来研究一下什么是数据的双向绑定? 另外,大家一定要知道vue的设计模式:MVVM M是Model的简写,V ...
- jpg、gif、png-8、png-24的区别
一.gif格式的特点 1.透明性.gif是一种布尔透明类型,即它可以是全透明,也可以是全不透明,但是没有半透明 2.动画.gif支持动画 3.无损耗性.gif是一种无损耗的图像格式,这意味着你可以对g ...
- Ubuntu下sublime-text3安装步骤
1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...
- String类型转json 转JSONObject 转 JSONArray 以及 遍历
public PageVo getByPage(int pageNum, int pageSize) { PageVo pageVo = new PageVo(); System.out.printl ...
- vivado 连接不上板子 There is no current hw_target
前情提要: vivado连接板子点击auto connect报错 [Labtoolstcl 44-469] There is no current hw_target. 处理步骤: 首先排除硬件问题, ...