题意 有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. July 20th, Week 30th Wednesday, 2016

    Learn from yesterday, live for today, and hope for tomorrow. 借鉴昨天,活着当下,憧憬未来. Yesterday is the past, ...

  2. java用代理访问

    Properties prop = System.getProperties(); prop.setProperty("http.proxyHost", "localho ...

  3. 深入剖析PHP输入流 php://input(与POST/GET的区别)

    PHP输入流php://input 转:http://www.nowamagic.net/academy/detail/12220520 在使用xml-rpc的时候,server端获取client数据 ...

  4. wp8 入门到精通 WebClient Post

    WebClient wc = new WebClient(); var URI = new Uri("http://your_uri_goes_here"); //If any e ...

  5. hdu 4006 优先队列 2011大连赛区网络赛F **

    签到题都要想一会 #include<cstdio> #include<iostream> #include<algorithm> #include<cstri ...

  6. 一个快速、高效的Levenshtein算法实现

    转自:http://www.cnblogs.com/ymind/archive/2012/03/27/fast-memory-efficient-Levenshtein-algorithm.html ...

  7. IE8上传文件时文件本地路径变成"C:\fakepath\"的问题

    转自:http://yunzhu.iteye.com/blog/1116893 在使用<input id="file_upl" type="file" / ...

  8. 使用pm2常见问题

    一.日志 1.pm2 的log怎么查看?(安装pm2后默认日志的路径为~/.pm2/),可以通过pm2 show (name)来查看某个进程下的日志地址 2.修改日志的输出路径,通过写一个程序启动的配 ...

  9. Loadrunner中web_custom_request使用场景

    其中有一段从服务器段动态返回的字符串需要重新提交给服务器(见红色标注) 录制自动生成的脚本是: web_submit_data("generateYfLstAction.do",  ...

  10. ls命令

    ls(list) 命令可以说是Linux下最常用的命令之一 #ls -l;列出文件的详细信息 #ll 以上两个命令一样,ll是ls -l的简写 #ls -al;列出目录下的所有文件,包括以 . 开头的 ...