1.题目描述:

1175. Strange Sequence

Time limit: 1.0 second
Memory limit: 2 MB
You have been asked to discover some important properties of one strange sequences set. Each sequence of the parameterized set is given by a recurrent formula:
Xn+1 = F(Xn-1, Xn),
where n > 1, and the value of F(X,Y) is evaluated by the following algorithm:
  1. find H = (A1*X*Y + A2*X + A3*Y + A4);
  2. if H > B1 then H is decreased by C until H ≤ B2;
  3. the resulting value of H is the value of function F.
The sequence is completely defined by nonnegative constants A1, A2, A3, A4, B1, B2 and C.
One may easily verify that such sequence possess a property that Xp+n = Xp+q+n for appropriate large enough positive integers p and q and for all n ≥ 0. You task is to find the minimal p and q for the property above to hold. Pay attention that numbers p and q are well defined and do not depend on way minimization is done.

Input

The first line contains seven integers: A1, A2, A3, A4, B1, B2 and C. The first two members of sequence (X1 and X2) are placed at the second line. You may assume that all intermediate values of H and all values of F fit in range [0..100000].

Output

An output should consist of two integers (p and q) separated by a space.

Sample

input output
0 0 2 3 20 5 7
0 1
2 3

2.解题思路

题目要求确定p和q,我们先确定q再确定p,有了q只需要从x1,x2开始逐个尝试就能得出p。如何确定q呢?既然题目说了这个序列以q为循环节,我们可以假定序列在迭代足够多次之后就进入了循环,设这个次数为max。这样迭代max后再从1开始寻找q就可以啦~

3.代码:

#include <iostream>
#define max 300000
using namespace std; int a1,a2,a3,a4,b1,b2,c,x1,x2;
int p,q; int cal(int x, int y) {
int h = a1*x*y+a2*x+a3*y+a4;
if (h > b1) {
while (h > b2) {
h -= c;
}
}
return h;
} void iter(int& x, int& y) {
int tmp = cal(x, y);
x = y;
y = tmp;
} //假定迭代max次后处于稳定状态
//思路:迭代max次;继续迭代找到q;x1,x2迭代q次得到x3,x4;同时迭代直到x1==x3 && x2 == x4,找到p
int main() {
cin >> a1 >> a2 >> a3 >> a4 >> b1 >> b2 >> c >> x1 >> x2;
int i, tmp1, tmp2, tmp3, tmp4;
tmp3 = x1;
tmp4 = x2;
for (i = ; i < max; i++) {
iter(x1, x2);
}
tmp1 = x1;
tmp2 = x2;
q = ;
iter(x1, x2);
while (x1 != tmp1 || x2 != tmp2) {
q++;
iter(x1, x2);
}
tmp1 = tmp3;
tmp2 = tmp4;
iter(tmp1,tmp2);
i = ;
while (i != q) {
i++;
iter(tmp1, tmp2);
}
p = ;
while (tmp1 != tmp3 || tmp2 != tmp4) {
p++;
iter(tmp1, tmp2);
iter(tmp3, tmp4);
}
cout << p << " " << q << endl;
}

4.复杂度:

空间:O(1)

时间:O(p+q+max)

5.心得:

把这道题写出来,是因为我第一次尝试时思路是错的:把<x0,x1>作为一个pair,pos是迭代次数,用一个map<pair,pos> result存储,一直迭代<x0,x1>,递增pos,并把结果存入result,当重复插入时(x.pair == y.pair,x.pos < y.pos),就求出了p,q: p = x.pos, q = y.pos - x.pos。

。。。

没错我当时就是这么写的,然后华丽丽的爆了内存。

这种做法的复杂度:

时间:O(p+q)

空间:O(p+q)

然而根据题目要求,空间限制2M,是很严格的。。以后一定要好好看题目。。

timus 1175. Strange Sequence 解题报告的更多相关文章

  1. 【九度OJ】题目1175:打牌 解题报告

    [九度OJ]题目1175:打牌 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1175 题目描述: 牌只有1到9,手里拿着已经排好序的 ...

  2. USACO Section2.1 Sorting a Three-Valued Sequence 解题报告

    sort3解题报告 —— icedream61 博客园(转载请注明出处)---------------------------------------------------------------- ...

  3. Ducci Sequence解题报告

    A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... ,  ...

  4. 【LeetCode】842. Split Array into Fibonacci Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  6. timus 1180. Stone Game 解题报告

    1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...

  7. ACM : HDU 2899 Strange fuction 解题报告 -二分、三分

    Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. LeetCode: Permutation Sequence 解题报告

    Permutation Sequence https://oj.leetcode.com/problems/permutation-sequence/ The set [1,2,3,…,n] cont ...

  9. USACO Section 2.1 Sorting a Three-Valued Sequence 解题报告

    题目 题目描述 给N个整数,每个整数只能是1,2,或3.现在需要对这个整数序列进行从小到大排序,问最少需要进行几次交换.N(1 <= N <= 1000) 样例输入 9 2 2 1 3 3 ...

随机推荐

  1. MySQL Replication需要注意的问题

    MySQL Replication 大家都非常熟悉了,我也不会写怎么搭建以及复制的原理,网上相关文章非常多,大家可以自己去搜寻.我在这里就是想总结一下mysql主从复制需要注意的地方.有人说主从复制很 ...

  2. 实战Java虚拟机之四:提升性能,禁用System.gc() ?

    今天开始实战Java虚拟机之四:"禁用System.gc()". 总计有5个系列 实战Java虚拟机之一“堆溢出处理” 实战Java虚拟机之二“虚拟机的工作模式” 实战Java虚拟 ...

  3. NorthWind 数据库整体关系

    http://blog.csdn.net/bergn/article/details/1502150 今天看到一张非常有用的图,说明有关Northwind数据库整体关系的图,以前一直在用,但是没有一个 ...

  4. 10个jQuery小技巧

    收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...

  5. spring mvc controller间跳转 重定向 传参 (转)

    转自:http://zghbwjl.blog.163.com/blog/static/12033667220137795252845/ 1. 需求背景     需求:spring MVC框架contr ...

  6. 利用xtrabackup备份mysql数据库

    利用xtrabackup备份mysql数据库 一.安装1.直接下载二进制文件wget http://www.percona.com/downloads/XtraBackup/XtraBackup-2. ...

  7. apachetop 实时监控apache指定日志

    编译安装,压缩包戳我下载 帮助文档 # apachetop -h ApacheTop v0.12.6 - Usage: File options: -f logfile open logfile (a ...

  8. php 读取文件

    <?php /** *@param string $ip *@return string ip对应的地区 */ function getLocation($ip) { $ip_file_path ...

  9. ajax原理和跨域解决方法

    ajax是异步的 JavaScript 和 XML.通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步更新.这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新. 1--启动 ...

  10. 外部函数test显示的返回内部函数add的调用

    前段时间有个需求中有点击datagrid的单元格实现某种事件,皇冠现金代理调用datagrid的onclickCell这个方法很容易实现,但是体验不好啊,完全不知道自己刚才点击的是哪个单元格,然后就尝 ...