It's Bessie's birthday and time for party games! Bessie has instructed the N (1 <= N <= 100,000) cows conveniently numbered 1..N to sit in a circle (so that cow i [except at the ends] sits next to cows i-1 and i+1; cow N sits next to cow 1). Meanwhile, Farmer John fills a barrel with one billion slips of paper, each containing some integer in the range 1..1,000,000. Each cow i then draws a number A_i (1 <= A_i <= 1,000,000) (which is not necessarily unique, of course) from the giant barrel. Taking turns, each cow i then takes a walk around the circle and pats the heads of all other cows j such that her number A_i is exactly divisible by cow j's number A_j; she then sits again back in her original position. The cows would like you to help them determine, for each cow, the number of other cows she should pat.

题目大意就是

给出n个数,然后对每个数,求出其他的数有几个是它的约数

数的范围是到100W

然后n的范围是10W

不能直接n^2暴力求

不过可以使用一种类似于筛法的方法。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int a[1111111];
int num[1111111];
int n, nt;
int x[111111];
int t[111111];
void ready()
{
for(int i = 0; i < nt; i++)
for(int j = x[i]; j <= 1000000; j += x[i])
a[j] += num[x[i]];
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d", &x[i]), t[i] = x[i], num[x[i]]++;
sort(x, x + n);
nt = unique(x, x + n) - x;
ready();
for(int i = 0; i < n; i++) printf("%d\n", a[t[i]] - 1);
return 0;
}

[Usaco2008 Dec]Patting Heads的更多相关文章

  1. BZOJ-1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法+乱搞

    1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 1383 Solved: 7 ...

  2. BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法

    1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lyds ...

  3. BZOJ 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Description   今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏.     贝茜让N(1≤N≤10 ...

  4. [bzoj1607][Usaco2008 Dec]Patting Heads 轻拍牛头_筛法_数学

    Patting Heads 轻拍牛头 bzoj-1607 Usaco-2008 Dec 题目大意:题目链接. 注释:略. 想法:我们发现,位置是没有关系的. 故,我们考虑将权值一样的牛放在一起考虑,c ...

  5. bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头

    筛法. 枚举每个数,它会对它的倍数的答案有贡献. 数大了以后,倍数相应少了很多.比枚举每个数的约数要好的多. 自己yy了一种分步做法.小于sqrt(m)被当作约数枚举,大于sqrt(m)的枚举倍数. ...

  6. [Usaco2008 Dec]Patting Heads 轻拍牛头[筛法]

    Description   今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏.     贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶 ...

  7. [BZOJ1607] [Usaco2008 Dec] Patting Heads 轻拍牛头 (数学)

    Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号 ...

  8. BZOJ1607 [Usaco2008 Dec]Patting Heads 轻拍牛头 筛法

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1607 题意概括 给出n个数,每一个数字<1000000,对于每一个数,让你求剩余的n-1个数 ...

  9. 【BZOJ】1607: [Usaco2008 Dec]Patting Heads 轻拍牛头(特殊的技巧)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1607 其实题目描述不清楚,应该是 别人拿的数能整除自己拿的数 数据范围很大,n<=100000 ...

随机推荐

  1. 08-UIKit(UITableTableViewCell、自定义Cell、xcode调试)

    目录: 1. UITableTableViewCell 2. tag技术 3. 自定义Cell 4. 用nib文件构造自定义的静态表 5. TableView数据模型总结 6. Xcode代码调试 & ...

  2. 基于visual Studio2013解决C语言竞赛题之1004平均值

     题目 解决代码及点评 /************************************************************************/ /* 4. 编一个程序, ...

  3. Java__jar包的简单操作

    工作需要研究了下jar,这里就举个简单的例子,以helloworld为例吧 1.启动eclipse编写代码: 次步不做赘述 package cn.test.jar; public class Hell ...

  4. RedHat Linux 5.5安装JDK+Tomcat并部署Java项目

    与大家分享下RedHat Linux 5.5安装JDK+Tomcat并部署Java项目的步骤,希望对大家有用. 1.下载并安装jdk 虚拟机中安装RedHat Linux 5.5 64位企业版, 这里 ...

  5. Service的开启和停止以及生命周期

    1.清单文件 <service android:name=".TestService"></service> 2.开启Service Intent inte ...

  6. 查询PO的预付款剩余金额

    FUNCTION zrfc_mm016. *"---------------------------------------------------------------------- * ...

  7. HDU Wolf and Rabbit

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  8. JDK 安装环境配置(ubuntu)

    在Ubuntu 上安装jdk,先去官网下载相对应的tar包 网址:(这是jdk1.8) http://www.oracle.com/technetwork/java/javase/downloads/ ...

  9. boost.xml_parser中文字符问题

    当使用xml_parser进行读xml时,如果遇到中文字符会出现解析错误. 网上有解决方案说使用wptree来实现,但当使用wptree来写xml时也会出错.而使用ptree来写中文时不会出错. 综合 ...

  10. 将某个MySQL库中的UTF8字符列都转成GBK格式

    DELIMITER $$ DROP PROCEDURE IF EXISTS `dba`.`Proc_ChangeCharacter2GBK`$$ CREATE DEFINER=`root`@`%` P ...