1. 程式人生 > >1005 Number Sequence 週期是48?不,是336!

1005 Number Sequence 週期是48?不,是336!

題目:

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 contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed. 

Output

For each test case, print the value of f(n) on a single line. 

Sample Input

1 1 3
1 2 10
0 0 0

Sample Output

2
5

顯然,這是一個找週期的問題。

我們很容易推出,一定有周期,而且最小週期不超過49.

(下面的論述中,都假設a和b小於7,即已經mod7)

而且,如果a、b不全為0,那麼一定是從(1,1)(即第1項)開始迴圈的

當a=b=0時,不是從第1項開始,而是從第3項開始迴圈。

我最開始的方法是,對於每組給的a、b,求出最小正週期

程式碼:

#include<iostream>
using namespacestd; int getT(int a, int b) { if (a == 0 || b == 0)return 12; if ((a + b) % 7 == 1)return 1; int T = 0, x1 = 1, x2 = 1; while (true) { int temp = (b*x1 + a*x2) % 7; x1 = x2; x2 = temp; T++; if (x1 == 1 && x2 == 1)break; } return T; } int main() { int a, b, n;
while (cin >> a >> b >> n) { if (n == 0)break; if (a % 7 == 0 && b % 7 == 0) { cout << (n<3) << endl; continue; } int x1 = 1, x2 = 1; if (n>20)n = (n - 20) % getT(a % 7, b % 7) + 20; if (n > 2)n -= 2; else n = 0; while (n--) { int temp = (b*x1 + a*x2) % 7; x1 = x2; x2 = temp; } cout << x2 << endl; } return 0; }

這個是AC了的,沒有任何問題。

其中,當b=0的時候,6一定是週期,這是費馬小定理。

當a=0的時候,恰好奇數列和偶數列是一樣的,而且6一定是它們的週期,所以12是數列的週期。

例如,當a=0,b=3時,前14項為1,1,3,3,2,2,6,6,4,4,5,5,1,1,週期為12

然後我發現vj裡面有個AC的程式碼很短,就看了一下,發現他是直接以48為週期。

網上搜了一下,很多人說48一定是週期,甚至還有人說49是週期。。。

但是我枚舉了a和b(一共也就49種情況)

程式碼:

int main()
{	
	for (int i = 0; i < 7; i++)
	{
		for (int j = 0; j < 7; j++)cout << getT(i, j) << " ";
		cout << endl;
	}	
	return 0;
}

結果是:


很明顯,有14、21、42的存在,48不是週期!

所以說,應該是VJ給的測試資料很水,所以有很多程式碼都渾水摸魚了。

當然,336一定是週期,有了這個資訊,也可以寫出很簡潔的程式碼了。

程式碼:

#include<iostream>
using namespacestd;
int main()
{	
	int a, b, n;
	while (cin >> a >> b >> n)
	{
		if (n == 0)break;
		if (a % 7 == 0 && b % 7 == 0)
		{
			cout << (n<3) << endl;
			continue;
		}
		int x1 = 1, x2 = 1;
		n = (n + 333) % 336 + 1;
		while (n--)
		{
			int temp = (b*x1 + a*x2) % 7;
			x1 = x2;
			x2 = temp;
		}
		cout << x2 << endl;
	}
	return 0;
}

這個也很快,也是0ms AC

相關推薦

1005 Number Sequence 週期48336

題目: Description A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n -

HDOJ 1005 Number Sequence48週期

Problem DescriptionA 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

HDU 1005 Number Sequence(找規律思維)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 153140    Accepted Submissio

HDU-1005-Number Sequence (迴圈週期

原題連結: 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

HDU 1005 Number Sequence

spa scanf scan %d content ble 取余 trac printf 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 找周期。當f(n)函數值再次出現1。1的時候就是一個循環

【ACM】杭電OJ 1005 Number Sequence (for java)

最開始的程式碼是這樣的,沒說我超時,直接報錯~~ import java.util.Arrays; import java.util.Scanner; public class Main { public static int num(int A,int B,int n) {

HDU 1005 ( Number Sequence )

Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S

HDU 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.

HDU 1005 Number Sequence(基礎矩陣快速冪)

//HDU 1005 15MS 1424K #include <cstdio> #include <cstring> #include <cmath> #in

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. G

1005 Number Sequence(長得像矩陣快速冪的找規律)

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 ca

hdoj 1005 Number Sequence (斐波那契數列 找迴圈節)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 158599    Accep

HDU OJ 1005 Number Sequence

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 178988    Accep

HDU 1005 Number Sequence【迴圈節(取模)】

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 158149    Ac

結束?開始

作為一名coder、OIer,我的程式設計師之路也有了一年了,就在上個星期,NOIP2018結束了,怎麼說呢,我也充分認識到了自己依然還遠遠不夠。   從最開始報以興趣的想法去學習c++,到如今對程式設計的熱愛...   時間會告訴我們自己有沒有真正去努力——   一

1005Number Sequence(hdu數學規律題)

his arch ear iostream tput ostream htm 數據 long Problem Description A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (

Number Sequence(找規律反正我沒找到遞迴超記憶體了....)

Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 178534    Accep

【HDOJ 1005Number Sequence (裸矩陣快速冪)

原諒我貼個水題。。。攢了一年的'恩怨'終於瞭解了 b( ̄▽ ̄)d 去年就接觸過矩陣快速冪 線代太弱  看他們程式碼沒參悟透。。可能真是悟性太差了。。 然後一隻以為矩陣快速冪是很叼的東西(不過確實很叼) 太高深 再沒敢碰。。有毒啊………… 直到最近比賽(VJ)出現矩陣快速冪

Servlet的生命週期看後悔一看必懂)

Servlet的生命週期可以分為載入,建立,初始化,處理客戶請求和銷燬5個階段 ①載入 容器通過類載入器來載入響應的Servlet ②建立 通過呼叫Servlet的建構函式來建立一個Servlet例項 ③初始化 通過呼叫Servlet的init()方法來完成初始化

杭電ACM第1005題——Number Sequence

#include <stdio.h> #include <stdlib.h> int main(){ int a,b,n,i; while(scanf("%d %d %d",&a,&b,&n)&&(a