博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5514 Frogs 容斥定理
阅读量:5773 次
发布时间:2019-06-18

本文共 4626 字,大约阅读时间需要 15 分钟。

Frogs

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=5514

Description

There are m stones lying on a circle, and n frogs are jumping over them.

The stones are numbered from 0 to m−1 and the frogs are numbered from 1 to n. The i-th frog can jump over exactly ai stones in a single step, which means from stone j mod m to stone (j+ai) mod m (since all stones lie on a circle).
All frogs start their jump at stone 0, then each of them can jump as many steps as he wants. A frog will occupy a stone when he reach it, and he will keep jumping to occupy as much stones as possible. A stone is still considered ``occupied" after a frog jumped away.
They would like to know which stones can be occupied by at least one of them. Since there may be too many stones, the frogs only want to know the sum of those stones' identifiers.

Under two situations the player could score one point.
⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.
⋅2. Ignoring the buoys and relying on dogfighting to get point. If you and your opponent meet in the same position, you can try to fight with your opponent to score one point. For the proposal of game balance, two players are not allowed to fight before buoy #2 is touched by anybody.
There are three types of players.
Speeder: As a player specializing in high speed movement, he/she tries to avoid dogfighting while attempting to gain points by touching buoys.
Fighter: As a player specializing in dogfighting, he/she always tries to fight with the opponent to score points. Since a fighter is slower than a speeder, it's difficult for him/her to score points by touching buoys when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.
There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.
Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting. Since Asuka is slower than Shion, she decides to fight with Shion for only one time during the match. It is also assumed that if Asuka and Shion touch the buoy in the same time, the point will be given to Asuka and Asuka could also fight with Shion at the buoy. We assume that in such scenario, the dogfighting must happen after the buoy is touched by Asuka or Shion.
The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

There are multiple test cases (no more than 20), and the first line contains an integer t,

meaning the total number of test cases.
For each test case, the first line contains two positive integer n and m - the number of frogs and stones respectively (1≤n≤104, 1≤m≤109).
The second line contains n integers a1,a2,⋯,an, where ai denotes step length of the i-th frog (1≤ai≤109)

Output

For each test case, you should print first the identifier of the test case and then the sum of all occupied stones' identifiers.

Sample Input

3
2 12
9 10
3 60
22 33 66
9 96
81 40 48 32 64 16 96 42 72

Sample Output

Case #1: 42 Case #2: 1170 Case #3: 1872

HINT

 

题意

有一堆青蛙,一开始都在0点,然后有一堆圈成一圈的石子,石子的编号是从0-m-1的

然后青蛙只能顺时针跳,每个青蛙可以一次跳a[i]格,然后所有青蛙都这样一直跳下去

然后问你,这些青蛙踩过的石子的编号和是多少?

题解:

首先,对于第i只青蛙,他跳过的格子,一定是k*gcd(a[i],m)这种的

如果m小一点,我们就可以直接暴力了

当时m太大了,我们就分解m的因数之后,对于每个因数做暴力就好了

每个因数T的贡献是 for(int i=1;i<=M/T;i++)ans += M*i;

然后优化一下就好了,对于部分加多了的因数,我们后面用容斥搞一搞就行了

代码

 

#include
#include
#include
#include
#include
using namespace std;#define maxn 10005int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}//每个青蛙,可以跳到gcd(m,a[i])*k的位置int ppp[maxn];int num[maxn],vis[maxn];int main(){ int tt;scanf("%d",&tt); for(int cas=1;cas<=tt;cas++) { int n,m; int cnt = 0; memset(vis,0,sizeof(vis)); memset(num,0,sizeof(num)); scanf("%d%d",&n,&m); for(int i=1;i<=sqrt(m);i++)//把因子全部筛出来 { if(m%i==0) { ppp[cnt++]=i; if(i*i!=m) ppp[cnt++]=m/i; } } sort(ppp,ppp+cnt); for(int i=0;i

 

转载地址:http://czaux.baihongyu.com/

你可能感兴趣的文章
phoenix java API
查看>>
python中的正则表达式
查看>>
Android常用URI收藏
查看>>
jQuery Pocket Reference
查看>>
Lucene4.7 索引和检索的常用API(二)
查看>>
$_SESSION和session_start示例
查看>>
svn clean up 失败
查看>>
数据仓库技术概述(一看就是架构师写的,对我极其有用)
查看>>
在PHP中模拟post提交方式,调用JSON接口_php调用json接口
查看>>
SpringMVC拦截器(资源和权限管理)
查看>>
算法的特性
查看>>
Java泛型中E、T、K、V等的含义
查看>>
企业级Spring最佳实践 - 项目配置
查看>>
Linux 防火墙 iptables 规则
查看>>
pcl
查看>>
SOAP
查看>>
spring-001-Ioc 顶层容器
查看>>
Backbone.js HelloWorld,应用起步
查看>>
希尔排序
查看>>
java并发编程(三): 对象的组合
查看>>