Problem G. Generators

Input file: generators.in

Output file: generators.out
Little Roman is studying linear congruential generators — one of the oldest and best known pseudorandom number generator algorithms. Linear congruential generator (LCG) starts with a non-negative integer number x0 also known as seed and produces an infinite sequence of non-negative integer numbers xi (0 ≤ xi < c) which are given by the following recurrence relation:
xi+1 = (axi + b) mod c
here a, b, and c are non-negative integer numbers and 0 ≤ x0 < c. Roman is curious about relations between sequences generated by different LCGs. In particular, he has n different LCGs with parameters a(j), b(j), and c(j) for 1 ≤ j ≤ n, where the j-th LCG is generating a sequence x(j) i . He wants to pick one number from each of the sequences generated by each LCG so that the sum of the numbers is the maximum one, but is not divisible by the given integer number k. Formally, Roman wants to find integer numbers tj ≥ 0 for 1 ≤ j ≤ n to maximize s =Pn j=1 x(j) tj subject to constraint that s mod k 6= 0. Input The first line of the input file contains two integer numbers n and k (1 ≤ n ≤ 10000, 1 ≤ k ≤ 109). The following n lines describe LCGs. Each line contains four integer numbers x(j) 0 , a(j), b(j), and c(j) (0 ≤ a(j),b(j) ≤ 1000, 0 ≤ x(j) 0 < c(j) ≤ 1000). Output If Roman’s problem has a solution, then write on the first line of the output file a single integer s — the maximum sum not divisible by k, followed on the next line by n integer numbers tj (0 ≤ tj ≤ 109) specifying some solution with this sum. Otherwise, write to the output file a single line with the number −1.
Sample input and output
2 3

1 1 1 6

2 4 0 5

8

4

1

2 2

0 7 2 8

2 5 0 6

-1

In the first example, one LCG is generating a sequence 1, 2, 3, 4, 5, 0, 1, 2, ..., while the other LCG a sequence 2, 3, 2, 3, 2, ....

In the second example, one LCG is generating a sequence 0, 2, 0, 2, 0, ..., while the other LCG a sequence 2, 4, 2, 4, 2, ....

题目中的xi+1是指xi的下一项。

第四场训练赛的题,是欧洲赛的题,比赛地址:传送门

题意:n行,每行x,a,b,c,推公式,每一行在这一行当中取一个数使得他们的和最大且不能被k整除,如果不存在输出-1。

题解:记录第一大和第二大的数,并且第二大的数不能被k整除,求出所有行中与第一个数相差最小的这个第二个数。如果最大的数的所有总和被k整除,就用这个数去换,记得用数组记录选取的数的下标。鸽笼原理最多c个数,如果算过就可以跳出,用动态数组记录。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
const int maxn=1e6+;
const int mm=;
bool vis[maxn];
int f1[maxn],f2[maxn];
int main()
{
freopen("generators.in","r",stdin);
freopen("generators.out","w",stdout);
int n,k,ans=,tmp=mm,flag=-;
scanf("%d%d",&n,&k);
for(int i=; i<n; i++)
{ int x,a,b,c;
scanf("%d%d%d%d",&x,&a,&b,&c);
for(int j=; j<c; j++)
vis[j]=;
vector <int> data;
for(int j=; j<c; j++)
{
if(vis[x]) break;
vis[x]=;
data.push_back(x);
x=(a*x+b)%c;
}
int max1=-,max1i=-;
for (int i = ; i < data.size(); i++)
{
if (data[i] > max1)
{
max1 = data[i];
max1i = i;
}
}
ans+=max1;
int max2=-,max2i=-;
int tmp2=max1%k;
for (int i = ; i < data.size(); i++)
{
if (data[i] > max2&&(data[i]%k)!=tmp2)
{
max2 = data[i];
max2i = i;
}
}
f1[i]=max1i;
f2[i]=max2i;
int minn=max1-max2;
if(max2i!=-&&minn<tmp)
{
tmp=minn;
flag=i;
}
//cout<<max1<<" "<<max2<<endl;
}
if(ans%k==&&flag==-)
{
cout<<-<<endl;
}
else if(ans%k==)
{
f1[flag]=f2[flag];
ans-=tmp;
cout<<ans<<endl;
for(int i=;i<n-;i++)
cout<<f1[i]<<" ";
cout<<f1[n-]<<endl;
}
else
{
cout<<ans<<endl;
for(int i=;i<n-;i++)
cout<<f1[i]<<" ";
cout<<f1[n-]<<endl;
}
return ;
}

Gym 100851G Generators (vector+鸽笼原理)的更多相关文章

  1. CodeChef February Challenge 2018 Points Inside A Polygon (鸽笼原理)

    题目链接  Points Inside A Polygon 题意  给定一个$n$个点的凸多边形,求出$[ \frac{n}{10}]\ $个凸多边形内的整点. 把$n$个点分成$4$类: 横坐标奇, ...

  2. 1393 0和1相等串 鸽笼原理 || 化简dp公式

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1393 正解一眼看出来的应该是鸽笼原理.记录每个位置的前缀和,就是dp[i ...

  3. Codeforce-Ozon Tech Challenge 2020-C. Kuroni and Impossible Calculation(鸽笼原理)

    To become the king of Codeforces, Kuroni has to solve the following problem. He is given n numbers a ...

  4. HDU 5762 Teacher Bo (鸽笼原理) 2016杭电多校联合第三场

    题目:传送门. 题意:平面上有n个点,问是否存在四个点 (A,B,C,D)(A<B,C<D,A≠CorB≠D)使得AB的横纵坐标差的绝对值的和等于CD的横纵坐标差的绝对值的和,n<1 ...

  5. POJ_1065_Wooden_Sticks_(动态规划,LIS+鸽笼原理)

    描述 http://poj.org/problem?id=1065 木棍有重量 w 和长度 l 两种属性,要使 l 和 w 同时单调不降,否则切割机器就要停一次,问最少停多少次(开始时停一次). Wo ...

  6. poj 3370 鸽笼原理知识小结

    中学就听说过抽屉原理,可惜一直没机会见识,现在这题有鸽笼原理的结论,但其实知不知道鸽笼原理都可以做 先总结一下鸽笼原理: 有n+1件或n+1件以上的物品要放到n个抽屉中,那么至少有一个抽屉里有两个或两 ...

  7. poj 2356鸽笼原理水题

    关于鸽笼原理的知识看我写的另一篇博客 http://blog.csdn.net/u011026968/article/details/11564841 (需要说明的是,我写的代码在有答案时就输出结果了 ...

  8. UVA 10620 - A Flea on a Chessboard(鸽笼原理)

    UVA 10620 - A Flea on a Chessboard 题目链接 题意:给定一个跳蚤位置和移动方向.如今在一个国际象棋棋盘上,左下角为黑格,一个格子为s*s,推断是否能移动到白格子.问要 ...

  9. Gym - 100851G:Generators(人尽皆知但是WA题)

    题意:现在有函数,每一项Xi=(A*X(i-1)+B)%C.现在给定N个函数以及K:X0,A,B,C.然你再每个函数选择一个数,使得其和最大,而且不被K整除. X0,A,B,C<=1e3 :K& ...

随机推荐

  1. Weak is not weak,Strong is not strong

    问题 今天做浏览器Controller的时候,碰到了一个奇怪的问题:每次pop浏览器controller之后,等几秒,总会碰到类似下面的错误(其中的xxxController就是浏览器或继承他的子类C ...

  2. easyui datagrid使用

    http://www.cnblogs.com/zgqys1980/archive/2011/01/04/1925775.html 加载相关js和css,因为easyui依赖jquery,所有加载eas ...

  3. 微信公众平台开发接口PHP SDK完整版

    <?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved */ define("TOKEN&q ...

  4. JS小记

    好记性不如烂笔头. 1.document.ElementFromPoint:根据坐标获得元素 2.有时候要操作DOM页面,但是得不到预期结果,很可能是因为页面还没加载完成,在console控制台可以看 ...

  5. 通过开源程序同时解决DNS劫持和DNS污染的问题

    我们知道,某些网络运营商为了某些目的,对DNS进行了某些操作,导致使用ISP的正常上网设置无法通过域名取得正确的IP地址.常用的手段有:DNS劫持和DNS污染.关于DNS劫持和DNS污染的区别,请查找 ...

  6. ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇(转)

    ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇   阅读目录 ASP.NET Identity 前世今生 建立 ASP.NET Identity 使用ASP.NET ...

  7. Linux CP文件夹略过目录的解决

    在Ubuntu Linux复制文件夹时出现一个问题,做个笔记,希望能给刚入门的菜鸟学习一下(见图1). 出现略过目录: www_linuxidc_com@linuxidc-Aspire-3680:~$ ...

  8. HNU 12833 Omar’s Bug(分情况讨论)

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12833&courseid=268 解题报告:有个11个 ...

  9. 建立你的第一个 Git 仓库

    虽然 Git 确实是被许多重要软件选作版本控制工具,但是并不是仅能用于这些重要软件;它也能管理你购物清单(如果它们对你来说很重要的话,当然可以了!).你的配置文件.周报或日记.项目进展日志.甚至源代码 ...

  10. Unity导出iOS真机测试教程

    原地址:http://unity3d.9tech.cn/news/2014/0410/40177.html 学 习了两天的Android开发,我感觉Android开发跟IOS开发和.NET平台下的开发 ...