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 ...
随机推荐
- 解决 internet connection sharing 启动不了
1.确认Windows Firewall服务是否启动(有异常可参考下面) a.打开注册表,找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ ...
- 谈Web前端安全编码
最近开发中涉及到有关输出正确的HTML标签这样的问题,正好对字符编码这块儿多看看,之前对这个方面认识的不深,思考的确实不够,如果下次再碰见类似的问题,若再次不少时间去调研的花,就得不偿失了. 就像正则 ...
- Cocos2dx中零散知识点
cocos2dx中有三种定时器:schedule,scheduleUpdate,scheduleOnce.功能分别是 每隔几秒调用自定义函数.调用系统默认的update()函数.只调用一次自定义函数 ...
- .net学习笔记----二级域名站点共享Session状态
前面一篇文章提到了如何在使用了ASP.NET form authentication的二级站点之间共享登陆状态, http://www.cnblogs.com/jzywh/archive/2007/0 ...
- CC2540开发板学习笔记(九)—— BLE协议简介
一.BLE协议简介 1.协议是什么? 协议是一系列的通信标准,双方需要共同按照这进行正常数据 协议是一系列的通信标准,双方需要共同按照这进行正常数据发射和 接收.协议栈是的具体实现形式,通俗点来理解就 ...
- 【练习】使用接口回调和handler实现数据加载到listview
代码结构 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- 如何查找MySQL中查询慢的SQL语句
如何查找MySQL中查询慢的SQL语句 更多 如何在mysql查找效率慢的SQL语句呢?这可能是困然很多人的一个问题,MySQL通过慢查询日志定位那些执行效率较低的SQL 语句,用--log-slow ...
- Linphone iOS客户端编译时打开G729支持
Assuming you were able to compile the SDK and the linphone XCode project, here is what you need to d ...
- MVC 路由模块分析(一)
概述 主要分为四个部分进行分析. First ——Usage: then ——Extension: and then ——Design: last ——Test. 1. Usage 1.1 Displ ...
- ZOJ1232 Adventure of Super Mario(DP+SPFA)
dp[u][t]表示从起点出发,到达i点且用了t次magic boot时的最短时间, 方程如下: dp[v][t]=min(dp[v][t],dp[u][t]+dis[u][v]); dp[v][t] ...