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表示向右 ...
随机推荐
- P1601 A+B Problem(高精加法)
[高精度就是一个固定的格式吧] #include<iostream>#include<cstdio>#include<cmath>#include<algor ...
- slideDown留言板
<!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...
- The Little Prince-12/02
The Little Prince-12/02 What moves me so deeply, about this little prince who is sleeping here, is h ...
- centos6二进制安装mysql5.5
centos 6.5,安装mysql 5.5.60 所需安装包mysql-5.5.60-linux-glibc2.12-x86_64.tar.gz.ncurses-devel-5.7-4.200902 ...
- CRC、MD5和SHA1的区别?
什么是CRC校验?CRC即循环冗余校验码:是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定.循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并将 ...
- LVS+Keepalived+Mysql+主主数据库架构[2台]
架构图 安装步骤省略. 158.140 keepalived.conf ! Configuration File for keepalived global_defs { #全局标识模块 notifi ...
- python简说(二十二)写日志
分四个级别 import nnloglog = nnlog.Logger('book_server.log') log.debug('xxx值是什么')log.info('调用了什么xxx')log. ...
- C++进程间通信之共享内存
转载:http://blog.csdn.net/taily_duan/article/details/51692999 转载:http://blog.csdn.net/fengrx/article/d ...
- java开发注意点之String使用equals和==的区别
"=="操作符的作用 1.用于基本数据类型的比较 2.判断引用是否指向堆内存的同一块地址. equals所在位置: 在Object类当中,而Object是所有类的父类,包含在jdk ...
- 同时import两个版本的QtQuick【1、2】,默认使用
在同一个qml文件中,如果同时import了Qtquick1和2,那么谁在后面,谁起作用