Output
输出fun(A)的计算结果。
Input示例
3
1 4 1
Output示例
4

first try:
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f3f
#define PI acos(-1)
#define N 100010
#define MOD 10
LL arr[N];
LL fun(int n){
LL sum=;
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){
sum+=floor((arr[i]+arr[j])/(arr[i]*arr[j]));
}
}
return sum;
}
int main()
{
int n;
while(~scanf("%d",&n)){
for(int i=;i<n;i++){
scanf("%d",&arr[i]);
}
LL ans=fun(n);
printf("%d\n",ans);
}
return ;
}

time limit exceeded

second try:

(a + b)/ab = 1/a + 1/b  a>2且b>2时 不等式(a+b)/ab < 1  a,b不全为2时等号成立
取整后为0 , 所以只用查找1和2,以及>=2的个数即可
公式:sum = 2(N1-1)*N1/2 + N1*(∑Nk k>=2) + N2*(N2-1)/2
#include "bits/stdc++.h"
using namespace std;
#define LL long long
#define INF 0x3f3f3f3f3f
#define PI acos(-1)
#define N 100010
#define MOD 10
int x,n;
int one, two, other;
int main()
{
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &x);
if (x == )
one++;
if (x == )
two++;
if (x >= )
other++;
}
LL sum = ;
sum += (one-)*one + one*other + ( (two*(two-))>> );
printf("%lld\n", sum); return ;
}

another:

1、观察算式:floor(A+B)/(A*B),不难发现,如果其中A.B都是>=2的数值,那么对应的值一定是0.那么根据这个特性我们继续讨论:

①如果A=1&&B=1,那么值为2,

②如果A=1||B=1&&另外一个不是1,那么值为1,

③如果A=2&&B=2,那么值为1.

根据以上特性,我们肯定是要来判断a【i】==1||a【i】==2的个数来决定结果。

2、对于每一个1,我们考虑,其贡献出来的值为:n-1,那么我们一层for扫这个数组,如果有一个1,那么对应结果加上n-1.

接下来我们统计2的个数,对应在最终结果上加:(cont2-1+1)*(cont2-1)/2;

3、数据范围比较大, 注意使用LL.

#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
int main()
{
int n;
while(~scanf("%d",&n))
{
ll cont2=;
ll output=;
for(int i=;i<n;i++)
{
int x;
scanf("%d",&x);
if(x==)
{
output+=n-;
}
if(x==)
{
cont2++;
}
}
printf("%I64d\n",output+(cont2)*(cont2-)/);
}
}

http://www.cnblogs.com/whileskies/p/7220026.html

http://www.voidcn.com/article/p-pymuhyiw-da.html

51Nod 1305 Pairwise Sum and Divide | 思维 数学的更多相关文章

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

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

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

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

  3. 51nod 1305 Pairwise Sum and Divide

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

  4. 1305 Pairwise Sum and Divide

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

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

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

  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面向对象-访问限制

    在Class内部,可以有字段,方法和属性,而外部代码可以通过直接调用实例变量的方法来操作数据, (1)私有普通字段 比如对于下面的Student类,name字段可以在外面通过对象进行直接访问: cla ...

  2. Thunder团队第一周 - Scrum会议7

    Scrum会议7 小组名称:Thunder 项目名称:爱阅app Scrum Master:宋雨 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...

  3. windows下cudnn的安装过程

    在CUDA安装成功之后,系统环境变量中会有如下两个变量显示:CUDA_PATH和CUDA_PATH_8 在安装完CUDA之后,到官网下载与其版本对应的CUDNN        下载地址:https:/ ...

  4. 策略模式,ASP.NET实现

    策略模式,ASP.NET实现 using System; using System.Collections.Generic; using System.Linq; using System.Web; ...

  5. javaIO--字符流

    java提供字符流对自否刘式文件进行数据读写操作.字符输入流类是Reader及其子类,输出流是Writer及其子类. 另外,上一篇javaIO写的是字节流,字节流方式也可以对以字符为基本类型的流式文件 ...

  6. PCA算法理解及代码实现

    github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维   在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...

  7. python爬虫-使用xpath方法

    #coding=utf-8 import re from lxml import etree import requests response = requests.get("http:// ...

  8. java 文件操作知识点

    1.每个文件以一个文件路径和文件名称进行表示,在不同的操作系统环境下,文件路径的表示形式是不一样的,例如在Windows操作系统中一般的表示形式为C:\windows\system,而Unix上的表示 ...

  9. name(实例化类名).hbm.xml文件案例

    [html] view plain copy print? <span xmlns="http://www.w3.org/1999/xhtml"><?xml ve ...

  10. vs code 自动补全效果不理想的问题

    之前一直用webstorm,最近换换口味,改用了VS Code,发现VS Code 智能提示得到的都不是我想要的 就比如  ! + tab ,HTML结构都出不来.经过一番搜索,发现是 VS Code ...