SGU 181.X-Sequence

时间限制:0.5s

空间限制:4M

题意

令X0=A

Xi=(a*Xi-1^2,b*Xi-1+c)%m;

求Xk,(0<=k<=109),(0<=a,b<=100),(1<m<1000);



Solution:

                题目的关键在于m的范围,1000的范围显然是会在取余后出现循环的。

                只要得出循环的数列的长度以及它的每一个数,和任意一个循环的起点位置。就可以算出X值。

                要注意的是X是不对m取余的,还有当X0很大时,计算X1 的中间量是有可能超过int范围的。

code:

#include <iostream>
#include <string>
#include <cstring>
#include <vector>
using namespace std;
const int INF = 111111;
vector<int> ans;
int f[INF];
long long  x, a, b, c, m, k, t;
inline long long  get (long long x) {
	return (a * x * x  + b * x  + c) % m;
}
int main() {
	cin >> x >> a >> b >> c >> m >> k;
	//找到第一次循环结束的地方,即第二次循环开始的地方
	for (t = 0; !f[x]; x = get (x), t++) {
		f[x] = 1;
		if (t == k) {
			cout << x;
			return 0;
		}
	}
	k -= (t-1);//从循环起点算起的K的新位置
     //得到循环数组
	memset (f, 0, sizeof f);
	for (t = 0; !f[x];) {
		f[x] = 1;
		ans.push_back (x);
		x = get (x), t++;
	}
	//找到k在循环节里的位置并输出
	k = k % t - 1;
	if (k < 0) k = t - 1;
	cout << ans[k];
	return 0;
}

  

SGU 181.X-Sequence,布布扣,bubuko.com

时间: 2024-10-11 15:36:47

SGU 181.X-Sequence的相关文章

SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)

题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) run much slower than the same code written in C++ or Java. We do not gua

sgu 455. Sequence analysis (floyd 判圈算法,O(1)空间复杂度求循环节)

455. Sequence analysis Time limit per test: 1 second(s)Memory limit: 4096 kilobytes input: standardoutput: standard Due to the slow 'mod' and 'div' operations with int64 type, all Delphi solutions for the problem 455 (Sequence analysis) run much slow

SGU - 117 - Counting (快速幂取模!)

SGU - 117 Counting Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description Find amount of numbers for given sequence of integer numbers such that after raising them to the M-th power they will be divided by

SGU - 105 - Div 3 (简单数学题!)

SGU - 105 Div 3 Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. You must determine amount of numb

SGU - 123 - The sum (简单数学!)

SGU - 123 The sum Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description Here is your second problem, keep calm and solve it . Nacci sequence of numbers is known to all : F1 = 1; F2 = 1; Fn+1 = Fn + Fn-1,

(数论) SGU 105

D - Div 3 Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 105 Appoint description:  System Crawler  (2015-01-20) Description There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first

poj2778 DNA Sequence【AC自动机】【矩阵快速幂】

DNA Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19991   Accepted: 7603 Description It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's very useful to analyze a segment of DNA Sequence,For ex

LeetCode OJ - Longest Consecutive Sequence

这道题中要求时间复杂度为O(n),首先我们可以知道的是,如果先对数组排序再计算其最长连续序列的时间复杂度是O(nlogn),所以不能用排序的方法.我一开始想是不是应该用动态规划来解,发现其并不符合动态规划的特征.最后采用类似于LRU_Cache中出现的数据结构(集快速查询和顺序遍历两大优点于一身)来解决问题.具体来说其数据结构是HashMap<Integer,LNode>,key是数组中的元素,所有连续的元素可以通过LNode的next指针相连起来. 总体思路是,顺序遍历输入的数组元素,对每个

1005 Number Sequence

Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple test cases. Each test case co