Frogs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4904    Accepted Submission(s): 1631

Problem 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.

 
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
 
Source
 

题意就是跳青蛙,通过分析会发现,就是步数a[i]与石头数m,通过gcd(a[i],m)之后,gcd的倍数的和。

因为重复的数只计算一次,所以要去重。

一开始想的是容斥去重,然而还是太捞了,。。。

这道题和队友讨论了3天,还问了学长,发现几个问题:

(1)如果直接枚举gcd的遍历,应该为去重他们的最小公倍数,也就是这样的。

for(ll j=;j<cnt;j++)
{
if(i&(<<j))
temp=temp*g[j]/gcd(temp,g[i]),jishu++;
}

(2)直接gcd的容斥枚举去重会超时,因为极限数据可能要枚举1<<36次,for一次的极限数据个人认为可能就是1e7再带点常数,1<<36次跑不出来,程序会崩。所以这种容斥是不可以的,虽然想法真的很好,但是真的过不去。所以,最后放弃了这种思路,其实还是可以容斥的,但是是有技巧的容斥。

直接看的题解,所以也不好说什么,毕竟是人家的劳动成果,只是分析一下。

做法一:

欧拉函数的延伸用法:小于或等于n的数中,与n互质的数的总和为:φ(n) * n / 2  (n>1)。

做法二:

枚举m的因子个数,这样就会少很多,就不存在超时的问题了。

以上两种做法的具体题解传送门:HDU 5514 Frogs(欧拉函数+数论YY)

直接贴代码吧。

代码1(欧拉函数):

 //欧拉函数的公式求解
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); ll a[maxn],n,m; ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
} ll euler(ll n)
{
ll ans=n;
for(int i=;i*i<=n;i++){
if(n%i==){
ans=ans/i*(i-);
while(n%i==) n/=i;
}
}
if(n>) ans=ans/n*(n-);
return ans;
} bool solve(int x)
{
for(int i=;i<n;i++){
if(x%a[i]==) return true;
}
return false;
} int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
memset(a,,sizeof(a));
scanf("%lld%lld",&n,&m);
for(int i=;i<n;i++){
scanf("%d",a+i);
a[i]=gcd(a[i],m);
}
ll ans=;
for(int i=;i*i<=m;i++){
if(m%i) continue;
if(solve(i)) ans+=(ll)euler(m/i)*m/;
if(i*i==m||i==) continue;
if(solve((m/i))) ans+=(ll)euler(i)*m/;
}
printf("Case #%d: %lld\n",cas,ans);
}
}

代码2(容斥原理):

 //容斥定理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); ll gcd(ll a,ll b)
{
return b==?a:gcd(b,a%b);
} ll g[maxn],fac[maxn];
int tp[maxn],num[maxn],vis[maxn]; int main()
{
int t;
scanf("%d",&t);
for(int cas=;cas<=t;cas++){
ll n,m;
scanf("%lld%lld",&n,&m);
int ok=;
for(int i=;i<n;i++){
scanf("%lld",&g[i]);
g[i]=gcd(g[i],m);
if(g[i]==) ok=;
}
if(ok==){
printf("Case #%d: %lld\n",cas,m*(m-)/);
continue;
}
sort(g,g+n);
n=unique(g,g+n)-g;
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
int cnt=;
for(ll i=;i*i<=m;i++){
if(i*i==m) fac[cnt++]=m/i;
else if(m%i==) fac[cnt++]=i,fac[cnt++]=m/i;
}
sort(fac,fac+cnt);
int cnt1=;
for(int i=;i<n;i++){
if(!vis[i]){
tp[cnt1++]=g[i];
for(int j=;j<n;j++)
if(g[j]%g[i]==) vis[j]=;
}
}
memset(vis,,sizeof(vis));
for(int i=;i<cnt;i++){
for(int j=;j<cnt1;j++){
if(fac[i]%tp[j]==){
vis[i]=;
break;
}
}
}
ll sum=;
for(int i=;i<cnt;i++){
if(num[i]!=vis[i]){
sum+=m*(m/fac[i]-)/*(vis[i]-num[i]);
for(int j=i+;j<cnt;j++)
if(fac[j]%fac[i]==)
num[j]=num[j]+vis[i]-num[i];
}
}
printf("Case #%d: %lld\n",cas,sum);
}
return ;
}

贴一下我们想了3天的错误代码,纪念一下。

代码(错误的):

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 ll gcd(ll a,ll b)
{
return b?gcd(b,a%b):a;
} ll sum(ll x,ll n)
{
ll temp=(n-)/x;
return temp*x+(temp*(temp-)/)*x;
} ll a[maxn]; int main()
{
ll t;
scanf("%lld",&t);
for(int cas=;cas<=t;cas++)
{
ll n,m;
scanf("%lld%lld",&n,&m);
ll h=;
for(int i=;i<n;i++)
{
ll x;
scanf("%lld",&x);
a[h++]=gcd(x,m);
}
vector<ll> g;
sort(a,a+h);
for(int i=h-;i>=;i--){
int flag=;
for(int j=i-;j>=;j--){
if(a[i]%a[j]==) flag=;
}
if(!flag) g.push_back(a[i]);
}
int cnt=g.size();
ll ans=;
for(ll i=;i<(1ll<<cnt);i++)
{
ll temp=,jishu=;
for(ll j=;j<cnt;j++)
{
if(i&(<<j))
temp=temp*g[j]/gcd(temp,g[i]),jishu++;
}
if(jishu==)continue;
if(jishu&) ans+=sum(temp,m);
else ans-=sum(temp,m);
}
printf("Case #%d: %lld\n",cas,ans);
}
}

到此为止,拜拜,再也不看这个题了。

HDU 5514.Frogs-欧拉函数 or 容斥原理的更多相关文章

  1. HDU 5514 Frogs 欧拉函数

    题意: 有\(m(1 \leq m \leq 10^9)\)个石子排成一圈,编号分别为\(0,1,2 \cdots m-1\). 现在在\(0\)号石头上有\(n(1 \leq n \leq 10^4 ...

  2. HDU 1695 GCD (欧拉函数,容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. hdu 1695 GCD (欧拉函数+容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. hdu 1695 GCD (欧拉函数、容斥原理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  5. HDU 2824 简单欧拉函数

    1.HDU 2824   The Euler function 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=2824 3.总结:欧拉函数 题意:求(a ...

  6. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

  7. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. GuGuFishtion HDU - 6390 (欧拉函数,容斥)

    GuGuFishtion \[ Time Limit: 1500 ms\quad Memory Limit: 65536 kB \] 题意 给出定义\(Gu(a, b) = \frac{\phi(ab ...

  9. HDU 2588 GCD (欧拉函数)

    GCD Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status De ...

  10. hdu 6434 Count (欧拉函数)

    题目链接 Problem Description Multiple query, for each n, you need to get $$$$$$ \sum_{i=1}^{n} \sum_{j=1 ...

随机推荐

  1. Ubuntu14.04-Python2.7-Virtualenv-Django1.9-MySQL完整环境配置

    一.安装Ubuntu14.04LTS 1.下载了ubuntu14.04后用ultraISO写到硬盘镜像(U盘) 开机启动项改成U盘在前,安装. 清空分区,重新分配. /最少10G,我放了100G. 物 ...

  2. ObservableCollection 类

    假设您正在创建 Windows 窗体应用程序,并且已将 DataGridView 控件绑定到标准 List(Of Customer) 数据结构.您希望能够使网格中的项目与基础数据源中的值保持同步.也就 ...

  3. B树及其变种

    B树是为磁盘或其他直接存取的辅助存储设备而设计的一种平衡搜索树.B树类似于红黑树,但它们在降低磁盘I/O操场数方面要更好一些.许多数据库系统使用B树或B树的变种来存储信息. 介绍 常见的动态查找树包括 ...

  4. LightOJ 1226 - One Unit Machine Lucas/组合数取模

    题意:按要求完成n个任务,每个任务必须进行a[i]次才算完成,且按要求,第i个任务必须在大于i任务完成之前完成,问有多少种完成顺序的组合.(n<=1000 a[i] <= 1e6 mod ...

  5. Canvas 基本绘图方法总结

    一.基本内容  1.简单来说,HTML5提供的新元素<canvas>  2.Canvas在HTML页面提供画布的功能,在画布中绘制各种图形  3.Canvas绘制的图形与HTML页面无关, ...

  6. 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows

    题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...

  7. 禁用 Cortana 的解决办法

    1. GPedit.msc 2. 然后在本地组策略编辑器中,点击“用户配置”中的“管理模版”,接着双击右侧的“Windows 组件”. 3. 下拉滚动条,并找到“文件资源管理器”,双击进入. 找到“在 ...

  8. spin lock的理解

    为什么在spin lock保护的代码里面不允许有休眠的操作呢? 因为spin lock不是空实现的前提下(内核没关抢占,或者是SMP打开),spin lock中是关抢占的,如果一个进程A拿到锁,内核抢 ...

  9. Linux内核中内存cache的实现【转】

    Linux内核中内存cache的实现 转自:http://blog.chinaunix.net/uid-127037-id-2919545.html   本文档的Copyleft归yfydz所有,使用 ...

  10. MVC 从控制器将数据对象赋值给前端JS对象

    @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport&quo ...