Frogs

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

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≤10^4 1≤m≤10^9).

The second line contains n integers a1,a2,⋯,an, where ai denotes step length of the i-th frog (1≤ai≤10^9).

 
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

解析  这道题真的非常好,有技巧的容斥。

AC代码

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<math.h>
using namespace std;
#define maxn 10005
int gcd(int a,int b)
{
return b==?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=;cas<=tt;cas++)
{
int n,m;
int cnt = ;
memset(vis,,sizeof(vis));
memset(num,,sizeof(num));
scanf("%d%d",&n,&m);
for(int i=;i<=sqrt(m);i++)//把因子全部筛出来
{
if(m%i==)
{
ppp[cnt++]=i;
if(i*i!=m)
ppp[cnt++]=m/i;
}
}
sort(ppp,ppp+cnt);
for(int i=;i<n;i++)
{
int x;scanf("%d",&x);
int kk = gcd(x,m);
for(int j=;j<cnt;j++)
if(ppp[j]%kk==)//说明这个因子的所有,都是可以被跳到的位置
vis[j]=;
}
vis[cnt-]=;//显然 m是不可能被跳到的
long long ans = ;
for(int i = ; i < cnt; i++)
{
if(vis[i] != num[i])
{
int t = (m-)/ppp[i];
ans += (long long)t*(t+)/ * ppp[i] * (vis[i]-num[i]);
//容斥一波
//一开始vis[i] - num[i] = 1的
//对于每个因数,如果重复计算了,在之后,减去就好了
t = vis[i] - num[i];
for(int j = i; j < cnt; j++)
if(ppp[j]%ppp[i] == )
num[j] += t;
}
}
printf("Case #%d: %lld\n",cas,ans);
}
}

wa的代码,暴力容斥,极限数据是36个gcd 应该直接爆复杂度了,不应该是wa的

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n")
#define debug(a,b) cout<<a<<" "<<b<<" "<<endl
#define ffread(a) fastIO::read(a)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn=1e4+;
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;
}
int main()
{
ll t,n,m;
int kase=;
scanf("%lld",&t);
while(t--)
{
set<ll> s;
scanf("%lld%lld",&n,&m);
for(int i=;i<n;i++)
{
ll x;
scanf("%lld",&x);
s.insert(gcd(x,m));
}
vector<ll> g;
for(auto it:s)
{
int flag=;
for(auto itt:s)
if(it%itt==&&it!=itt)
flag=;
if(flag)g.pb(it);
}
ll 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(g[j],temp),jishu++;
}
if(jishu==)
continue;
if(jishu%==)
ans+=sum(temp,m);
else
ans-=sum(temp,m);
}
printf("Case #%d: %lld\n",kase++,ans);
}
}

最新文章

  1. VirtualBox Ubuntu Server 16.04 手动设置 网络(IP, DNS, 路由)
  2. Linux中获取当前程序的绝对路径
  3. SQL封装、多态与重载
  4. yii2-admin 插件使用简要教程
  5. php整理(四): mysql
  6. UVA 11021 - Tribles(概率)
  7. HDU4521+线段树+dp
  8. Android 的开源电话/通讯/IM聊天项目全集
  9. easyui之datagrid(不定时补充)
  10. Probability theory
  11. github桌面软件使用教程
  12. JDK版本问题 发展史
  13. 关键字中mysql数据库查询条件带中文无结果解决办法
  14. css IFC 与 BFC分析
  15. 微信小程序的布局css样式
  16. IE6条件下的bug与常见的bug及其解决方法
  17. Linux centos6.5 系统语言改成中文简体
  18. Alpha 冲刺 —— 十分之四
  19. C++ 编译,执行过程 具体解释。
  20. 使用eclipse进行web开发的3个lib文件夹

热门文章

  1. codevs 1043 方格取数 2000年NOIP全国联赛提高组
  2. # Transition:添加弹出过渡效果
  3. 一个SAP开发人员的双截棍之路
  4. SEO 第二章
  5. iview table icon dorpdown html页面级别vue组件 #vuez#
  6. PHP24 自定义分页类
  7. JAVA基础——网络编程之网络链接
  8. 35个Redis面试题
  9. 离散数学-集合的交并差集运算--STL-set类
  10. html/css:简单网页