题意 有n个循环 给出x a b c xi+1=(a*x+b)%c 要求是从这些循环中各取一个数 使加和最大并且给出一个m 满足sum%m!=0

n的范围是4次方 c的范围是3次方 训练赛的时候看了一眼就觉得好难 稍微一处理就要超时..

天气好热 又wa了水题两次 十分不想做题QAQ 看着有点难就懒得想 掏出手机开始玩 坑了蕾姐一把 QAQ 以后再也不这样了

然而蕾姐还是机智的想出了几近正解的办法QAQ

在这个循环中 每两个紧挨着的数的间隔必定是一样的 这些数的值在0~C之间 是1000

我们对每个循环的最大值和次大值进行保存

可以看出 一旦出现sum%m 由于每个循环必定需要一个数 如果这个间隔%m==0 那么我们将一个大的数换为一个小的数 仍然会有sum%m==0 是浪费

如果间隔%m!=0 那么换次大值就可以达到使sum%m==0变为!=0

所以 我们把所有循环的最大值加起来作为一个sum 之后 找出最小且%k!=0的间隔

如果sum%m!=0直接输出就可以 如果==0 就减去间隔 由于间隔%k!=0 所以减去间隔的sum%k!=0

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<iostream>
using namespace std;
/// 保存下来 每个循环的循环大小(最大的和第二大的) 以及该循环是否可以%k 记录最大的和第二大的地点
int xh[10050];
int maxw1[10050];
int maxw2[10050];
int vis[1050];
bool ok[10050];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(ok,false,sizeof(ok));
memset(vis,0,sizeof(vis));
int x,a,b,c;
int sum=0;
for(int i=1; i<=n; i++)
{
scanf("%d%d%d%d",&x,&a,&b,&c);
vis[x]=i;
int maxx=x;
int maxx2=-1;
int maxxid=1;
int maxx2id=-1;
int cnt=1;
while(1)
{
x=(a*x+b)%c;
if(vis[x]==i)
break;
vis[x]=i;
cnt++;
if(x>maxx)
{
maxx2=maxx;
maxx=x;
maxx2id=maxxid;
maxxid=cnt;
}
else if(x>maxx2)
{
maxx2=x;
maxx2id=cnt;
}
}
maxw1[i]=maxxid;
if(maxx2!=-1)
{
maxw2[i]=maxx2id;
int z=maxx-maxx2;
xh[i]=z;
if(z%m!=0)
ok[i]=true;
else ok[i]=false;
}
else
{
ok[i]=false;
}
sum+=maxx;
}
if(sum%m!=0)
{
printf("%d\n",sum);
for(int i=1; i<=n; i++)
{
printf("%d",maxw1[i]-1);
if(i==n)
printf("\n");
else printf(" ");
}
}
else
{
int minn=1005;
int w=-1;
for(int i=1; i<=n; i++)
{
if(ok[i])
{
if(xh[i]<minn)
{
minn=xh[i];
w=i;
}
}
}
if(minn!=1005)
{
maxw1[w]=maxw2[w];
printf("%d\n",sum-xh[w]);
for(int i=1; i<=n; i++)
{
printf("%d",maxw1[i]-1);
if(i==n)
printf("\n");
else printf(" ");
}
}
else
{
printf("-1\n");
}
}
}
}

  

UVALive 7472的更多相关文章

  1. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  2. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  3. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

  4. 思维 UVALive 3708 Graveyard

    题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...

  5. UVALive 6145 Version Controlled IDE(可持久化treap、rope)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  6. UVALive 6508 Permutation Graphs

    Permutation Graphs Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit ...

  7. UVALive 6500 Boxes

    Boxes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Pract ...

  8. UVALive 6948 Jokewithpermutation dfs

    题目链接:UVALive 6948  Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...

  9. 【暑假】[实用数据结构]UVAlive 3135 Argus

    UVAlive 3135 Argus Argus Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

随机推荐

  1. Shopping(hdu 3768)

    题意:给你一个无向图,求从0号点开始遍历所有的指定点再回到0号点的最短路径 #include<cstdio> #include<iostream> #include<qu ...

  2. 设置IIS会话过期时间

    打开默认网站----双击ASP--展开会话属性---更改超时时间-

  3. rsync实时同步文件

    http://rsync.samba.org/download.html [root@v01 src]# yum install git [root@v01 src]# git clone git:/ ...

  4. 如何使用SAE的Storage

    转自:http://blog.csdn.net/xujainxing/article/details/8981904 Storage在里面当然可以创建文件夹,只不过无法通过代码创建,而是在后台管理页面 ...

  5. POJ 3461 Oulipo KMP

    题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...

  6. MATLAB学习笔记(四)——文件操作

    首先,声明,如果学过C的话就可以不用看了,因为是一样的,只要注意一些系统变量的名字稍微变动了而已.都是基于ANSI C语言的标准库函数写的. (一)文件的打开与关闭 一.文件的打开 1.语法 fid= ...

  7. kinect学习笔记(二)—— Sdk平台的搭建~、

    一.资源下载        由于我们使用的kinect v1.0,所以我们只需要使用1.8版本的sdk就好了,然后资源包,在QQ群的共享里面已经有啦,所以大家可以直接下载. 二.软件安装        ...

  8. GSON解析

    1.把GSON.jar包放到工程lib目录下并依赖 2.使用HiJSON工具格式化json字符串 2.创建对象类 public class MyNews { public News_Paramz pa ...

  9. BaseAdapter自定义适配器

    listview = (ListView) findViewById(R.id.listview); imageview = (ImageView) findViewById(R.id.imagevi ...

  10. codeforces 385 c

    Description Recently, the bear started studying data structures and faced the following problem. You ...