有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:
 
fun(A)
    sum = 0
    for i = 1 to A.length
        for j = i+1 to A.length
            sum = 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。

收起

 

输入

第1行:1个数N,表示数组A的长度(1 <= N <= 100000)。
第2 - N + 1行:每行1个数A[i](1 <= A[i] <= 10^9)。

输出

输出fun(A)的计算结果。

输入样例

3
1 4 1

输出样例

4

暴力应该会超时吧,可以找规律,这样一道题不能说上来就暴力,和除以积向下取整,可以分解(x+y)/x*y = 1/x+1/y,是两个分子为1的分数的和,这样我们发现如果1/x+1/y小于1,那么原分数的值将为0,也就是说原分数取值也就是0,1,2这三种,x和y都是1那么就是取值2,x和y一个是1,另一个不是1,那么就取值1,或者x和y都是2,取值1,其他的都是0.
代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
typedef pair<int,int> pa;
int n,sum,a,b;
int s[];
int main() {
scanf("%d",&n);
for(int i = ;i < n;i ++) {
scanf("%d",&s[i]);
if(s[i] == ) a ++;
else if(s[i] == ) b ++;
}
sum = a * (n - ) + b * (b - ) / ;
printf("%d",sum);
}

51nod 1305 Pairwise Sum and Divide的更多相关文章

  1. 51Nod 1305 Pairwise Sum and Divide | 思维 数学

    Output 输出fun(A)的计算结果. Input示例 3 1 4 1 Output示例 4 first try: #include "bits/stdc++.h" using ...

  2. [51nod] 1305 Pairwise Sum and Divide 数学

    有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = 0     for i = 1 to A.length         for j = ...

  3. 1305 Pairwise Sum and Divide

    1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对 ...

  4. 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏

    1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...

  5. 1305 Pairwise Sum and Divide(数学 ,规律)

    HackerRank   1305 Pairwise Sum and Divide   有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整:   fun(A)     sum = ...

  6. 51nod P1305 Pairwise Sum and Divide ——思路题

    久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...

  7. 51nod1305 Pairwise Sum and Divide

    题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...

  8. 51nod 1305:Pairwise Sum and Divide

    1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有这样一段 ...

  9. Pairwise Sum and Divide 51nod

      1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 有这样 ...

随机推荐

  1. python:校验邮箱格式

    # coding:utf-8 import re def validateEmail(email): if re.match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\ ...

  2. 简单的Lock死锁例子

    static void Main(string[] args) { lock (_lock1) { var t = new Thread(() => { lock (_lock1) { Cons ...

  3. 【VS开发】VS2015没修改源文件也导致重新编译的解决办法

    在使用VS2010编译C++程序的时候,每次修改工程中的某一个文件,点击"生成-仅用于项目-仅生成**"时,往往都是整个工程都需要重新编译一遍.由于这个工程代码量太大,每次编译完成 ...

  4. 各种实用的 PHP 开源库推荐【转】

    转自: https://my.oschina.net/editorial-story/blog/882780 PHP 是一种通用开源脚本语言.语法吸收了 C 语言.Java 和 Perl 的特点,利于 ...

  5. 【剑指offer】面试题 24. 反转链表

    面试题 24. 反转链表

  6. [转载] - Entity Framework 性能优化建议

    1.对象管理机制-复杂为更好的管理模型对象,EF提供了一套内部管理机制和跟踪对象的状态,保存对象一致性,使用方便,但是性能有所降低. 2.执行机制-高度封装在EF中,所有的查询表达式都会经过语法分析. ...

  7. linux svn开机自动启动服务

    SVN设置开机自动启动 usr/lib/systemd/system/添加svn.service文件 home/sdbdatasvn/svnrepos(换成绝对路径) 如果出现权限问题,请chmod  ...

  8. django 请求 与 响应

    目录 请求(HttpRequest) 与 响应(HttpResponse) HttpRequest对象 请求相关的常用值 属性 HttpResponse对象 JsonResponse对象 render ...

  9. python实战项目 — selenium登陆豆瓣

    利用selenium 模仿浏览器,登陆豆瓣 重点: 1. 要设置好 chromedriver配置与使用, chromedriver.exe 和 Chrome的浏览器版本要对应, http://chro ...

  10. java.IO.EOFException异常

    错误代码为: 43 boolean booleanResult = dis.readBoolean();//dis为DateInputStream的实例 44 System.out.println(b ...