洛谷 P2926 [USACO08DEC]拍头Patting Heads

P2926 [USACO08DEC]拍头Patting Heads

题目描述

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(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号奶牛与1号奶牛相邻.农夫约翰用很多纸条装满了一个桶,每一张包含了一个不一定是独一无二的1到1,000,000的数字.

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

输入输出格式

输入格式:

  • 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: 复制

5
2
1
2
3
4

输出样例#1: 复制

2
0
2
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.

思路:用类似于欧拉筛法的东西去解决。刷表法就可以了。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,maxn;
int a[100010];
int ma[1000010],ans[1000100];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        ma[a[i]]++;maxn=max(a[i],maxn);
    }
    for(int i=1;i<=maxn;i++){
        if(!ma[i])    continue;
        for(int j=i;j<=maxn;j+=i)
            ans[j]+=ma[i];
    }
    for(int i=1;i<=n;i++)
        cout<<ans[a[i]]-1<<endl;
} 

原文地址:https://www.cnblogs.com/cangT-Tlan/p/8157194.html

时间: 2024-08-29 03:37:47

洛谷 P2926 [USACO08DEC]拍头Patting Heads的相关文章

洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm

P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm 题目描述 Every year in Wisconsin the cows celebrate the USA autumn holiday of Halloween by dressing up in costumes and collecting candy that Farmer John leaves in the N (1 <= N <= 100,000) stalls conven

洛谷P2925 [USACO08DEC]干草出售Hay For Sale

题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his hay inventory, leaving him with nothing to feed the cows. He hitched up his wagon with capacity C (1 <= C <= 50,000) cubic units and sauntered over t

洛谷——P2925 [USACO08DEC]干草出售Hay For Sale

https://www.luogu.org/problem/show?pid=2925 题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his hay inventory, leaving him with nothing to feed the cows. He hitched up his wagon with capacity C (1 <= C

洛谷 P2922 [USACO08DEC]秘密消息Secret Message

题目描述 Bessie is leading the cows in an attempt to escape! To do this, the cows are sending secret binary messages to each other. Ever the clever counterspy, Farmer John has intercepted the first b_i (1 <= b_i <= 10,000) bits of each of M (1 <= M &

[BZOJ1607][Usaco2008 Dec]Patting Heads 轻拍牛头

1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 2590  Solved: 1361[Submit][Status][Discuss] Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号奶牛与1号奶牛相邻.农夫约翰用很多纸条装

[bzoj 1607] [Usaco2008 Dec]Patting Heads 轻拍牛头 筛数

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1607 1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 3 Sec Memory Limit: 64 MB Submit: 1654 Solved: 874 [Submit][Status][Discuss] Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一

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

1607: [Usaco2008 Dec]Patting Heads 轻拍牛头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/problem.php?id=1607 Description 今天是贝茜的生日,为了庆祝自己的生日,贝茜邀你来玩一个游戏. 贝茜让N(1≤N≤100000)头奶牛坐成一个圈.除了1号与N号奶牛外,i号奶牛与i-l号和i+l号奶牛相邻.N号奶牛与1号奶牛相邻.农夫

洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day

P2866 [USACO06NOV]糟糕的一天Bad Hair Day 75通过 153提交 题目提供者洛谷OnlineJudge 标签USACO2006云端 难度普及/提高- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 题目标题 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her

洛谷P3045 [USACO12FEB]牛券Cow Coupons

P3045 [USACO12FEB]牛券Cow Coupons 71通过 248提交 题目提供者洛谷OnlineJudge 标签USACO2012云端 难度提高+/省选- 时空限制1s / 128MB 提交  讨论  题解 最新讨论更多讨论 86分求救 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget