【模拟】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 */
View Code

 

posted @ 2016-08-30 19:01  Cool639zhu  阅读(431)  评论(0编辑  收藏  举报