[Usaco2008 Dec]Patting Heads

17 篇文章 0 订阅
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值