码迷,mamicode.com
首页 > 其他好文 > 详细

Patting Heads

时间:2018-05-01 12:33:05      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:gif   技术   spl   head   次数   lines   hint   div   inpu   

Description

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 Ai (1 ≤ Ai ≤ 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 Ai is exactly divisible by cow j‘s number Aj; 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.

Input

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains a single integer: Ai

Output

* Lines 1..N: On line i, print a single integer that is the number of other cows patted by cow i.

Sample Input

5
2
1
2
3
4

Sample Output

2
0
2
1
3

Hint

The 5 cows are given the numbers 2, 1, 2, 3, and 4, respectively. The first cow pats the second and third cows; the second cows pats no cows; etc.

题目大概的意思就是说牛1手上的数字可以去整除其他牛的数字,则可以去爆他狗头。
一开始我是想说一一去枚举的,从一找到牛手上的数可以整除的数,去加他,但发现超时。
于是有了这种思路,我从1到牛手上的最大的数枚举,if这个数字是有的,那这个数的倍数都加上他的次数;
AC 代码
技术分享图片
#include<stdio.h>
 int num1[1000100]={0};
    int num2[1000100]={0};
    int a[100100];
int main()
{
    int n,i,mix=0,j;


    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        num1[a[i]]++;

    }

    for(i=1;i<=n;i++)
        if(mix<a[i])
        mix=a[i];

    for(i=1;i<=mix;i++)
    {    if(num1[i]!=0)
        for(j=i;j<=mix;j=j+i)
        {   
            num2[j]=num2[j]+num1[i];

        }
    }

   for(i=1;i<=n;i++)
        printf("%d\n",num2[a[i]]-1);
}
View Code

 

Patting Heads

标签:gif   技术   spl   head   次数   lines   hint   div   inpu   

原文地址:https://www.cnblogs.com/shuaihui520/p/8975764.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!