码迷,mamicode.com
首页 > 其他好文 > 详细

HDU_5514_Frogs

时间:2017-10-09 20:51:11      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:des   mat   ott   more   font   eps   set   double   clu   

Frogs

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


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 (1n104, 1m109).

The second line contains n integers a1,a2,?,an, where ai denotes step length of the i-th frog (1ai109).
 

 

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
 
  • 观察对于n和m较小的时候的样例不难发现可以到达的点都是k*gcd(ai,m)<m
  • 但是对于给出的n个a[i]我们不能保证在计算过程中对于同一个点只计算一次
  • 考虑容斥原理解题
  • gcd(ai,m)=di的结果都属于m的因数集合D(m)
  • 如果 u*ai==v*aj 并且 u*ai 属于D(m),那么对于 u*ai的倍数就重复计算了一次,我们就应该在新的计算过程中减去多加的次数
  • 对于多加的次数这个变量的维护可以通过记录当前因数被使用的次数来实现,1-被使用次数就是当前因数对应增量的倍数

 

 1 #include <iostream>
 2 #include <string>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <cmath>
 8 #include <vector>
 9 #include <queue>
10 #include <stack>
11 #include <set>
12 #include <map>
13 using namespace std;
14 typedef long long           LL ;
15 typedef unsigned long long ULL ;
16 const int    maxn = 1e5 + 10   ;
17 const int    inf  = 0x3f3f3f3f ;
18 const int    npos = -1         ;
19 const int    mod  = 1e9 + 7    ;
20 const int    mxx  = 100 + 5    ;
21 const double eps  = 1e-6       ;
22 const double PI   = acos(-1.0) ;
23 
24 LL gcd(LL x, LL y){
25     return y?gcd(y,x%y):x;
26 }
27 int T, tot;
28 LL n, m, a, d, k, ans;
29 LL b[maxn], c[maxn], e[maxn];
30 int main(){
31     // freopen("in.txt","r",stdin);
32     // freopen("out.txt","w",stdout);
33     while(~scanf("%d",&T)){
34         for(int kase=1;kase<=T;kase++){
35             ans=0LL;
36             tot=0;
37             scanf("%lld %lld",&n,&m);
38             for(LL i=1LL;i<=(LL)sqrt(m);i++)
39                 if(0==m%i){
40                     c[tot++]=i;
41                     if(i!=m%i)
42                         c[tot++]=m/i;
43                 }
44             sort(c,c+tot);
45             memset(b,0,sizeof(b));
46             memset(e,0,sizeof(e));
47             for(int i=1;i<=n;i++){
48                 scanf("%lld",&a);
49                 d=gcd(a,m);
50                 b[lower_bound(c,c+tot,d)-c]=1;
51             }
52             for(int i=0;i<tot;i++)
53                 if(b[i])
54                     for(int j=i+1;j<tot;j++)
55                         if(c[j]%c[i]==0 && b[j]==0)
56                             b[j]=1;
57             for(int i=0;i<tot;i++)
58                 if(b[i]){
59                     LL cnt=b[i]-e[i];
60                     k=(m-1)/c[i];
61                     // k=(m/c[i])-1;
62                     ans+=cnt*k*(c[i]+k*c[i])/2;
63                     for(int j=i+1;j<tot;j++)
64                         if(c[j]%c[i]==0)
65                             e[j]+=cnt;
66                 }
67             printf("Case #%d: %lld\n",kase,ans);
68         }
69     }
70     return 0;
71 }

 

 

 

HDU_5514_Frogs

标签:des   mat   ott   more   font   eps   set   double   clu   

原文地址:http://www.cnblogs.com/edward108/p/7642574.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!