51Nod 1305 Pairwise Sum and Divide | 思维 数学
输出fun(A)的计算结果。
3
1 4 1
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 | 思维 数学的更多相关文章
- 1305 Pairwise Sum and Divide(数学 ,规律)
HackerRank 1305 Pairwise Sum and Divide 有这样一段程序,fun会对整数数组A进行求值,其中Floor表示向下取整: fun(A) sum = ...
- [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 = ...
- 1305 Pairwise Sum and Divide
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 有这样一段程序,fun会对 ...
- 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏
1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...
- 51nod P1305 Pairwise Sum and Divide ——思路题
久しぶり! 发现的一道有意思的题,想了半天都没有找到规律,结果竟然是思路题..(在大佬题解的帮助下) 原题戳>>https://www.51nod.com/onlineJudge/ques ...
- 51nod1305 Pairwise Sum and Divide
题目链接:51nod 1305 Pairwise Sum and Divide 看完题我想都没想就直接暴力做了,AC后突然就反应过来了... Floor( (a+b)/(a*b) )=Floor( ( ...
- 51nod 1305:Pairwise Sum and Divide
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有这样一段 ...
- Pairwise Sum and Divide 51nod
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有这样 ...
随机推荐
- quartz入门实例
一 Quarta介绍 1 Quartz是什么 Quartz就是一个纯 Java 实现的作业调度工具,相当于数据库中的 Job.Windows 的计划任务.Unix/Linux 下的 Cron,但 Qu ...
- Thunder团队第三周 - Scrum会议5
Scrum会议5 小组名称:Thunder 项目名称:i阅app Scrum Master:苗威 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传康 ...
- Java中I/O流之缓冲流
Java 中的缓冲流: 1. 缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法(带缓冲区的,显著减少对 IO 的读写次数,保护硬盘). 2. ...
- iOS- iPad里有趣的UIPopoverController
效果: 1.对UIPopoverController的简单概述 1.1 UIPopoverController是在iPad开发中常用的一个组件(在iPhone上不允许使用),使用非常简单 1.2 ...
- Swift-assert使用时机
什么时候使用断言呢? 包含下面的情况时使用断言: 1.整型下标索引作为值传给自定义索引实现的参数时,但下标索引值不能太低也不能太高时,使用断言 2.传值给函数但如果这个传过来的值无效时,函数就不能完成 ...
- TCP系列04—连接管理—3、TCP连接的半打开和半关闭
在前面部分我们我们分别介绍了三次握手.四次挥手.同时打开和同时关闭,TCP连接还有两种场景分别是半打开(Half-Open)连接和半关闭(Half-Close)连接.TCP是一个全双工(Full-Du ...
- Scala快速入门-基本数据结构
模式匹配 使用用模式匹配实现斐波那契 def fibonacci(in: Any): Int = in match { case 0 => 0 case 1 => 1 case n: In ...
- WinForm连续点击按钮只打开一次窗体
许多朋友,学习C#时,制作WinForm小程序总会有一个问题,如果我们在父窗体设置的是点击一个按钮,打开一个子窗体,连续点击总会连续出现一样窗体,可是我们有时只想打开一次窗体,怎么办? 呵呵,我来方法 ...
- 实现一个可以实时提示的textarea组件
该组件输入.换行.变换光标可以实时给出提示 效果: textarea.vue <template> <div> <el-input id="user-input ...
- hdu2295-Radar
有n个城市,\(m\)个雷达,\(k\)个操作员,每个操作员只能操作一个雷达.每个雷达的覆盖范围是一个以雷达坐标为中心的圆,所有雷达的覆盖半径是相同的. 现在给出这\(n\)个城市,\(m\)个雷达的 ...