POJ P2926 轻拍牛头 【对整除的联想】【约数】

# [USACO08DEC]Patting Heads S

## 题面翻译

今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏。

贝茜让 $N$ ($1\leq N\leq 10^5$) 头奶牛坐成一个圈。除了 $1$ 号与 $N$ 号奶牛外,$i$ 号奶牛与 $i-1$ 号和 $i+1$ 号奶牛相邻。$N$ 号奶牛与 $1$ 号奶牛相邻。农夫约翰用很多纸条装满了一个桶,每一张包含了一个不一定是独一无二的 $1$ 到 $10^6$ 的数字。

接着每一头奶牛 $i$ 从柄中取出一张纸条 $A_i$。每头奶牛轮流走上一圈,同时拍打所有手上数字能整除在自己纸条上的数字的牛的头,然后做回到原来的位置。牛们希望你帮助他们确定,每一头奶牛需要拍打的牛。

## 题目描述

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.

## 输入格式

\* Line 1: A single integer: N

\* Lines 2..N+1: Line i+1 contains a single integer: A\_i

## 输出格式

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

## 样例 #1

### 样例输入 #1

```





4
```

### 样例输出 #1

```




3
```

## 提示

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.要想使a∣b​​  (b能整除a),则b必须包含所有a​的质因数

/*
本题离开题面,总结起来就是
对于序列中的某个数来说,序列中的其他的数有多少个
是它的约数? 

 
*/


#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int a[N];
int cnt[N]; //存某个数在原序列中出现了多少次 
int sum[N]; //存当前下标数有多少在原序列中的约数 
int main()
{
	int n;
	cin>>n;
	
	for(int i=1;i<=n;i++)
	{
		
		cin>>a[i];
		cnt[a[i]]++; 
	} 
	
	
	for(int i=1;i<N;i++)
	for(int j=i;j<N;j+=i)
	{
		sum[j]+=cnt[i];
	} 
	
	for(int i=1;i<=n;i++)
	cout<<sum[a[i]]-1<<endl;
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值