UVALive 7472
题意 有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的更多相关文章
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- 思维 UVALive 3708 Graveyard
题目传送门 /* 题意:本来有n个雕塑,等间距的分布在圆周上,现在多了m个雕塑,问一共要移动多少距离: 思维题:认为一个雕塑不动,视为坐标0,其他点向最近的点移动,四舍五入判断,比例最后乘会10000 ...
- UVALive 6145 Version Controlled IDE(可持久化treap、rope)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- UVALive 6508 Permutation Graphs
Permutation Graphs Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit ...
- UVALive 6500 Boxes
Boxes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Pract ...
- UVALive 6948 Jokewithpermutation dfs
题目链接:UVALive 6948 Jokewithpermutation 题意:给一串数字序列,没有空格,拆成从1到N的连续数列. dfs. 可以计算出N的值,也可以直接检验当前数组是否合法. # ...
- 【暑假】[实用数据结构]UVAlive 3135 Argus
UVAlive 3135 Argus Argus Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %l ...
随机推荐
- svn插件subclipse使用http代理同步svn时出现异常(解决)
现象描述: 对项目进行“与资源库进行同步”时弹出对话框显示以下错误信息: 同步 SVNStatusSubscriber 时报告了错误.1 中的 0 个资源已经同步. 同步 /MMonitorLogis ...
- Fresco 源码分析(二) Fresco客户端与服务端交互(3) 前后台打通
4.2.1.2.4 PipelineDraweeControllerBuilder.obtainController()源码分析 续 上节中我们提到两个核心的步骤 obtainDataSourceSu ...
- vsftp 一键安装包
http://pan.baidu.com/s/1mibAJC8
- 搭建邮局(邮件服务器) - hmailserver
1.查看服务器mx是否解析成功 nslookup set type=mx 2.hmailserver服务器 smtp设置 3.foxmail 设置 4.使用webmail(after ...
- Telnet命令检测远程主机上的端口是否开启
ping命令不能检测端口,只能检测你和相应IP是否能连通. 本地虚拟机里安装了一个Ubuntu,使用Putty连接22端口操作时提示失败,于是查看对应端口是否开启. Windows下要检测远程主机上的 ...
- SQL如何在数据库间复制表
方法一: DB1 tb1 DB2 tb2 选择DB1 到表的列表那里选择tb1表 右键 所有任务 数据导出 下一步 选择你要导出的数据库DB1 下一步 选择你要导入的数据库DB2 下一步 选 ...
- 如何使用SAE的Storage
转自:http://blog.csdn.net/xujainxing/article/details/8981904 Storage在里面当然可以创建文件夹,只不过无法通过代码创建,而是在后台管理页面 ...
- 【Grunt】关于Grunt可视化的尝试
使用Grunt遇到的问题? 必须要安装NodeJS 必须安装grunt-cli 需要编写复杂的Gruntfile.js规则 每个项目中必须存在nodejs的grunt模块 不方便管理每一个包含grun ...
- Android学习 之 startActivityForResult 和 onActivityResult
startActivityForResult 和 onActivityResult() 作用:主要用于 主Activity向调用的 子Activity 获得数据. 使用方法:在 主Activity写 ...
- XmlPull
XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); // 创建解析器. XmlPullParser parser = ...