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级算法题 收藏 关注 有这样 ...
随机推荐
- 《梦断代码Dreaming In Code》阅读笔记(三)
最后这几章感觉上更多是从软件完成整体上来讲的.比如说技术.方法等. 在我看来,其实一个团队一直坚持一种好的.先进的方法是不可少的.如果一个优秀的团队刚愎自用,只随着成员们喜好发展,那不能长久.比如说, ...
- II 3.1 连接到服务器
II 3.1 连接到服务器 package socket; import java.io.IOException; import java.io.InputStream; import java.ne ...
- OnDraw和Opanit的区别
OnPaint是WM_PAINT消息的消息处理函数,在OnPaint中调用OnDraw,一般来说,用户自己的绘图代码应放在OnDraw中. OnPaint() 是CWnd的类成员,负责响应WM_PA ...
- python获取指定长度的字符串
from random import Random def random_str(randomlength=31): str = '' chars = 'abcdefghijklmnopqrstuvw ...
- AutoResetEvent的基本用法
The following example uses an AutoResetEvent to synchronize the activities of two threads.The first ...
- [C/C++] C/C++错题集
1. 解析: A:在GCC下输出:0 在VC6.0下输出:1 B:在GCC下输出:段错误 (核心已转储) 在VC6.0下输出:已停止工作,出现了一个问题,导致程序停止正常工作. C:正常 ...
- 【python】Python: Enum枚举的实现
从C#系语言过来用Python,好不容易适应了写代码不打花括号,突然有一天发现它居然木有枚举……于是stackoverflow了一把,发现神人的枚举(enum)实现到处都是,于是汉化总结过来. 如果是 ...
- matlab eval【转】
Matlab 简单谈谈EVAL函数的用法 EVAL(s)相当于把字符串s的内容作为语句来执行. 比如:eval('a=3*5') 和直接在command 窗口中输入 a=3*5 等效 eval 一个经 ...
- filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行
filter过滤器 默认情况下只对客户端发来的请求有过滤作用 对服务端的跳转不起作用 需要显示的在xml定义过滤的方式才行
- Python虚拟环境virtualenv的使用
virtualenv 是一个创建孤立的Python环境的工具.可以让你创建各自独立的.互不影响的Python开发环境. 使用pip安装即可 pip install virtualenv 查看是否安装成 ...