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表示向右 ...
随机推荐
- .net 缓存
缓存有很多实现方法,所有这些可以被分为两类,基于内存的缓存和基于磁盘的缓存: 1. 内存驻留缓存——包含在内存中临时存储数据的所有实现方法,通常在以下情况下使用: a) 应用程序频繁使用 ...
- caffe生成voc格式lmdb
要训练ssd基本都是在liu wei框架下改,生成lmdb这一关照葫芦画瓢总遇坑,记录之: 1. labelmap_voc.prototxt要根据自己的分类修改,比如人脸检测改成这样: item { ...
- Python 字典(Dictionary) values()方法
描述 Python 字典(Dictionary) values() 函数以列表返回字典中的所有值. 语法 values()方法语法: dict.values() 参数 NA. 返回值 返回字典中的所有 ...
- sklearn异常检测demo
sklearn 异常检测demo代码走读 # 0基础学python,读代码学习python组件api import time import numpy as np import matplotlib ...
- (Review cs231n) Gradient Vectorized
注意: 1.每次更新,都要进行一次完整的forward和backward,想要进行更新,需要梯度,所以你需要前馈样本,马上反向求导,得到梯度,然后根据求得的梯度进行权值微调,完成权值更新. 2.前馈得 ...
- centos-ftp搭建
参照https://blog.csdn.net/a735834365/article/details/80622105 https://blog.csdn.net/a735834365/article ...
- bzoj2049 [Sdoi2008]Cave 洞穴勘测 link cut tree入门
link cut tree入门题 首先说明本人只会写自底向上的数组版(都说了不写指针.不写自顶向下QAQ……) 突然发现link cut tree不难写... 说一下各个函数作用: bool isro ...
- GoldenGate 12.3 MA架构介绍系列(3) - 各功能模块介绍
在新版的ogg 12.3 microservice architect中,提供了4个不同的服务模块和命令行模块. Admin Server: 负责连接用户.trandata, checkpoint的添 ...
- webstorm's interpreter
下载node.js 地址:http://nodejs.cn/ next——> Node interpreter: ……/……/node.exe
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...