日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

NYOJ 148

發布時間:2025/3/13 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 NYOJ 148 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

fibonacci數列(二)

時間限制:1000 ms | 內存限制:65535 KB 難度:3 描述

In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn ? 1 + Fn ? 2 for n ≥ 2. For example, the first ten terms of the Fibonacci sequence are:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

An alternative formula for the Fibonacci sequence is

.

Given an integer n, your goal is to compute the last 4 digits of Fn.

Hint

As a reminder, matrix multiplication is associative, and the product of two 2 × 2 matrices is given by

.

Also, note that raising any 2 × 2 matrix to the 0th power gives the identity matrix:

.

輸入
The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000). The end-of-file is denoted by a single line containing the number ?1.
輸出
For each test case, print the last four digits of Fn. If the last four digits of Fn are all zeros, print ‘0’; otherwise, omit any leading zeros (i.e., print Fn mod 10000).
樣例輸入
0 9 1000000000 -1
樣例輸出
0 34 6875 1 //找周期 2 #include <stdio.h> 3 4 #define N 20000 5 int a[N]={0,1}; 6 7 int init() 8 { 9 int i,j,k; 10 for(i=2;i<N;i++) 11 { 12 a[i]=(a[i-1]+a[i-2])%10000; 13 if(a[i]==0) 14 { 15 a[i+1]=(a[i]+a[i-1])%10000; 16 if(a[i+1]==1) 17 break; 18 } 19 } 20 return i; 21 } 22 23 int main() 24 { 25 int i,j,k; 26 int n; 27 int period = init(); 28 while(scanf("%d",&n),n!=-1) 29 { 30 n%=period; 31 printf("%d\n",a[n]); 32 } 33 return 0; 34 }

?

1 //快速冪算法 2 #include<stdio.h> 3 4 void fast_pow(int a1[][2], int a2[][2]) 5 { 6 int c[2][2]; 7 int i, j, k; 8 for (i=0; i<2; ++i) 9 { 10 for (j=0; j<2; ++j) 11 { 12 c[i][j] = 0; 13 for (k=0; k<2; ++k) 14 { 15 c[i][j] = (c[i][j] + a1[i][k] * a2[k][j]) % 10000; 16 } 17 } 18 } 19 for (i=0; i<2; ++i) 20 { 21 for (j=0; j<2; ++j) 22 { 23 a1[i][j] = c[i][j]; 24 } 25 } 26 } 27 28 int main() 29 { 30 int i,j,k; 31 int n; 32 while (scanf("%d", &n) , n != -1) 33 { 34 int a[2][2] = {{1, 1}, {1, 0}}; 35 int b[2][2] = {{1, 0}, {0, 1}};//單位矩陣 36 while (n) 37 { 38 if (n & 1) 39 fast_pow(b, a); 40 fast_pow(a, a); 41 n >>= 1; 42 } 43 printf ("%d\n", b[1][0]);//最后n必然從1變為0,所以最后一次總要執行 fast_pow(b, a); 44 } 45 return 0; 46 }

HDU 1005

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

?

同樣是構造矩陣

?

?

1 #include<stdio.h> 2 int main() 3 { 4 int f[200],a,b,n,i; 5 while(scanf("%d%d%d",&a,&b,&n),a||b||n) 6 { 7 if(n>2) 8 { 9 f[1]=f[2]=1; 10 for(i=3;i<200;i++) 11 { 12 f[i]=(a*f[i-1]+b*f[i-2])%7; 13 if(f[i-1]==1&&f[i]==1) 14 break; 15 } 16 i-=2;//LPP,最小正周期 17 n=n%i;//比如1~5是個循環周期,f(6)=f(1)、 18 if(n==0) 19 printf("%d\n",f[i]); 20 else 21 printf("%d\n",f[n]); 22 } 23 else 24 printf("1\n"); 25 } 26 return 0; 27 }

?

?

?

?

?

轉載于:https://www.cnblogs.com/hxsyl/archive/2012/09/12/2681566.html

總結

以上是生活随笔為你收集整理的NYOJ 148的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。