【模拟】NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)

题目链接:

  http://codeforces.com/gym/100851

题目大意:

  n个序列。每个序列有4个值x,a,b,c,之后按照x=(a*x+b)%c扩展无穷项。

  求每个序列各取一个数之后求和不是K的倍数的最大值。

  (x,a,b,c<=1000,n<=10000,K<=109

题目思路:

  【模拟】

  先暴力把每个序列能够获得的值都求出来。存下最大的两个%K不相等的值。

  接下来先取每个序列最大的值,如果%K不为0则为答案。

  否则把其中一个换成次优值。因为前面满足%K不相等所以只用换一个。

  1 //
  2 //by coolxxx
  3 //#include<bits/stdc++.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<stack>
 10 #include<queue>
 11 #include<set>
 12 #include<bitset>
 13 #include<memory.h>
 14 #include<time.h>
 15 #include<stdio.h>
 16 #include<stdlib.h>
 17 #include<string.h>
 18 //#include<stdbool.h>
 19 #include<math.h>
 20 #define min(a,b) ((a)<(b)?(a):(b))
 21 #define max(a,b) ((a)>(b)?(a):(b))
 22 #define abs(a) ((a)>0?(a):(-(a)))
 23 #define lowbit(a) (a&(-a))
 24 #define sqr(a) ((a)*(a))
 25 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 26 #define mem(a,b) memset(a,b,sizeof(a))
 27 #define eps (1e-8)
 28 #define J 10
 29 #define mod 1000000007
 30 #define MAX 0x7f7f7f7f
 31 #define PI 3.14159265358979323
 32 #define N 10004
 33 #define M 1004
 34 using namespace std;
 35 typedef long long LL;
 36 int cas,cass;
 37 int n,m,lll,ans;
 38 struct xxx
 39 {
 40     int num,c;
 41 }q[N][2];
 42 int s[N];
 43 bool cmp1(xxx aa,xxx bb)
 44 {
 45     return aa.c>bb.c;
 46 }
 47 void print()
 48 {
 49     int i,j;
 50     for(i=1;i<=n;i++)
 51     {
 52         for(j=1;j<=s[i];j++)
 53         {
 54             printf("%d:%d  ",q[i][j].c,q[i][j].num);
 55         }
 56         puts("");
 57     }
 58 }
 59 int main()
 60 {
 61     #ifndef ONLINE_JUDGE
 62 //    freopen("1.txt","r",stdin);
 63 //    freopen("2.txt","w",stdout);
 64     #endif
 65     int i,j,k;
 66     int a,b,c,x;
 67 //    for(scanf("%d",&cass);cass;cass--)
 68 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
 69 //    while(~scanf("%s",s+1))
 70     while(~scanf("%d",&n))
 71     {
 72         ans=0;
 73         scanf("%d",&m);
 74         for(i=1;i<=n;i++)
 75         {
 76             scanf("%d%d%d%d",&x,&a,&b,&c);
 77             mem(s,0);q[i][0].num=q[i][1].num=q[i][0].c=q[i][1].c=-1;
 78             s[x]=1;
 79             for(j=2,x=(a*x+b)%c;!s[x];x=(a*x+b)%c,j++)
 80                 s[x]=j;
 81             for(x=c-1,j=0;x>=0 && j<2;x--)
 82             {
 83                 if(!s[x])continue;
 84                 if(x>=q[i][0].c)
 85                 {
 86                     if(x%m!=q[i][0].c%m)q[i][1]=q[i][0];
 87                     q[i][0].c=x,q[i][0].num=s[x];
 88                 }
 89                 else if(x>q[i][1].c && x%m!=q[i][0].c%m)
 90                     q[i][1].c=x,q[i][1].num=s[x];
 91                 if(q[i][1].c!=-1 && q[i][0].c!=-1)break;
 92             }
 93         }
 94         for(i=1;i<=n;i++)ans+=q[i][0].c;
 95         if(ans%m)
 96         {
 97             printf("%d\n",ans);
 98             for(i=1;i<=n;i++)
 99                 printf("%d ",q[i][0].num-1);
100             puts("");
101         }
102         else
103         {
104             cas=0;q[0][0].c=MAX;q[0][1].c=0;
105             for(i=1;i<=n;i++)
106             {
107                 if(q[i][1].c==-1)continue;
108                 if(q[i][0].c-q[i][1].c<q[cas][0].c-q[cas][1].c)
109                     cas=i;
110             }
111             if(cas==0){puts("-1");continue;}
112             ans=ans-q[cas][0].c+q[cas][1].c;
113             printf("%d\n",ans);
114             q[cas][0]=q[cas][1];
115             for(i=1;i<=n;i++)
116                 printf("%d ",q[i][0].num-1);
117             puts("");
118         }
119     }
120     return 0;
121 }
122 /*
123 //
124
125 //
126 */

时间: 2024-10-10 02:36:59

【模拟】NEERC15 G Generators(2015-2016 ACM-ICPC)(Codeforces GYM 100851)的相关文章

HDU 5873 Football Games 【模拟】 (2016 ACM/ICPC Asia Regional Dalian Online)

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 309 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

2016 ACM/ICPC Asia Regional Dalian Online 1002/HDU 5869

Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 240 Problem Description This is a simple problem. The teacher gives Bob a list of prob

2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5984 Pocky 题意 给出一根棒子(可以吃的)的长度x和切割过程中不能小于的长度d,每次随机的选取一个位置切开,吃掉左边的一半,对右边的棒子同样操作,直至剩余的长度不大于d时停止.现在给出x和d,问切割次数的数学期望是多少. 解题思路 当看到第二个样例2 1时,结果是1.693147,联想到ln

2016 ACM/ICPC Asia Regional Shenyang Online 1007/HDU 5898 数位dp

odd-even number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 388    Accepted Submission(s): 212 Problem Description For a number,if the length of continuous odd digits is even and the length

HDU 5889 Barricade 【BFS+最小割 网络流】(2016 ACM/ICPC Asia Regional Qingdao Online)

Barricade Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 997    Accepted Submission(s): 306 Problem Description The empire is under attack again. The general of empire is planning to defend his

2016 ACM/ICPC Asia Regional Shenyang Online 1003/HDU 5894 数学/组合数/逆元

hannnnah_j’s Biological Test Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 802    Accepted Submission(s): 269 Problem Description hannnnah_j is a teacher in WL High school who teaches biolog

2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 859    Accepted Submission(s): 325 Problem Description Every school has some legends, Northeastern University is the same. Enter

2016 ACM/ICPC Asia Regional Dalian Online 1006 /HDU 5873

Football Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 439    Accepted Submission(s): 157 Problem Description A mysterious country will hold a football world championships---Abnormal Cup

2016 ACM/ICPC Asia Regional Qingdao Online 1001/HDU5878 打表二分

I Count Two Three Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 782    Accepted Submission(s): 406 Problem Description I will show you the most popular board game in the Shanghai Ingress Resis