博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
upc 9318 Slot Machines
阅读量:5111 次
发布时间:2019-06-13

本文共 3962 字,大约阅读时间需要 13 分钟。

Slot Machines

时间限制: 1 Sec  内存限制: 128 MB
提交: 198  解决: 53
[] [] [] [命题人:]

题目描述

Slot machines are popular game machines in casinos. The slot machine we are considering has six places where a figure appears. By combination of figures, one may earn or lose money. There are ten kinds of figures, so we will represent a figure with a number between 0 and 9. Then we can use a six-digit number w = w1w2w3w4w5w6 where 0 ≤ w1, w2, w3, w4, w5, w6 ≤ 9 to represent one possible outcome of the slot machine.
Figure I.1. The layout of a slot machine.
Old slot machines were made up with mechanical components, but nowadays they were replaced by PC-based systems. This change made one critical flaw: they are based on pseudo-random number generators and the outcome sequences of a slot machine are periodic. Let T[i] be the i-th outcome of a slot machine. At first, there is a truly random sequence of length k,T[1],T[2],…,T[k]. Then there exists one positive number such that T[i+p] = T[i]for all possible values of i(>k). Once an attacker can find out the exact values of k and p, he or she can exploit this fact to beat the casino by betting a lot of money when he or she knows the outcome with a good combination in advance.
For example, you have first six numbers of outcome sequences: 612534, 3157, 423, 3157, 423, and 3157. Note that we can remove first 0’s. Therefore, 3157 represents 003157 and 423 represents 000423. You want to know its tenth number. If you know the exact values of k and p, then you can predict the tenth number. However, there are many candidates for k and p: one extreme case is k=5 and p=1, and another is k=0 and p=6. The most probable candidate is the one where both k and p are small. So, our choice is the one with the smallest k+p. If there are two or more such pairs, we pick the one where p is the smallest. With our example, after some tedious computation, we get k=1 and p=2.
Assume that you have n consecutive outcomes of a slot machine, T[1], T[2], …, T[n]. Write a program to compute the values of k and p satisfying the above-mentioned condition.

 

输入

Your program is to read from standard input. The first line contains a positive integer n (1 ≤ n ≤ 1,000,000), representing the length of numbers we have observed up to now in the outcome sequence. The following line contains n numbers. Each of these numbers is between zero and 999,999.

 

输出

Your program is to write to standard output. Print two integers k and p in one line.

 

样例输入

6612534 3157 423 3157 423 3157

 

样例输出

1 2

 

题意

给n个数,删去p个数后会形成循环,最小循环节为k,求p+k最小的解。

分析

用KMP直接求最小循环节即可。

///  author:Kissheart  ///#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define FAST_IO ios::sync_with_stdio(false)const double PI = acos(-1.0);const double eps = 1e-6;const int MAX=1e6+10;const int mod=1e9+7;typedef long long ll;using namespace std;#define gcd(a,b) __gcd(a,b)inline ll lcm(ll a,ll b){ return a/gcd(a,b)*b;}inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){ if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}inline ll inv1(ll b){ return qpow(b,mod-2);}inline ll exgcd(ll a,ll b,ll &x,ll &y){ if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );ll s[MAX],n;ll Next[MAX];void KMP(){ ll i,j; i=1,j=0; Next[1]=0; while(i<=n) { if(j==0 || s[i]==s[j]) i++,j++,Next[i]=j; else j=Next[j]; } return ;}int main(){ scanf("%lld",&n); for(ll i=n;i>=1;i--) scanf("%lld",&s[i]); KMP(); ll tp,tk,k,p; k=INF;p=INF; for(int i=1;i<=n;i++) { tp=i-Next[i+1]+1; tk=n-i; if(tp+tk
View Code

 

转载于:https://www.cnblogs.com/Kissheart/p/9744373.html

你可能感兴趣的文章
jQuery方法大全
查看>>
[转贴]安装ssh
查看>>
WebView加载网页详情
查看>>
【转载】Linux screen 命令详解
查看>>
dd命令 建立两颗一模一样的磁盘
查看>>
常用的jquery触屏手机页面特效代码下载
查看>>
background-clip,background-origin
查看>>
C# 如何创建一个Windows服务
查看>>
集群和分布式区别
查看>>
Android(java)学习笔记153:采用post请求提交数据到服务器(qq登录案例)
查看>>
Java基础知识强化101:Java 中的 String对象真的不可变吗 ?
查看>>
Android 高级UI设计笔记12:ImageSwitcher图片切换器
查看>>
虚拟主机与虚拟目录学习小结
查看>>
hlg1414安装雷达【贪心】
查看>>
Blog文章待看
查看>>
NOI 题库 7084
查看>>
CF933A A Twisty Movement
查看>>
c++友元函数、友元类、友成员函数
查看>>
计算机体系结构--海明码
查看>>
Monitoring RMAN Backups
查看>>