国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

uva 10518 How Many Calls?

2019-11-08 02:13:16
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

原題: The fibonacci number is defined by the following recurrence: ? fib(0) = 0 ? fib(1) = 1 ? fib(n) = fib(n ? 1) + fib(n ? 2) But we’re not interested in the fibonacci numbers here. We would like to know how many calls does it take to evaluate the n-th fibonacci number if we follow the given recurrence. Since the numbers are going to be quite large, we’d like to make the job a bit easy for you. We’d only need the last digit of the number of calls, when this number is rePResented in base b. Input Input consists of several test cases. For each test you’d be given two integers n (0 ≤ n < 2 63 ? 1), b (0 < b ≤ 10000). Input is terminated by a test case where n = 0 and b = 0, you must not process this test case. Output For each test case, print the test case number first. Then print n, b and the last digit (in base b) of the number of calls. There would be a single space in between the two numbers of a line. Note that the last digit has to be represented in decimal number system. Sample Input 0 100 1 100 2 100 3 100 10 10 0 0 Sample Output Case 1: 0 100 1 Case 2: 1 100 1 Case 3: 2 100 3 Case 4: 3 100 5 Case 5: 10 10 7

中文: 問(wèn)你求第n個(gè)斐波那契數(shù)時(shí),要調(diào)用多少次斐波那契函數(shù)(線性),結(jié)果取b的模。

#include<bits/stdc++.h>using namespace std;typedef long long ll;ll f[10001];ll n,b;ll solve(ll x){ if(x==0||x==1) return 1; ll f1=1,f2=1; ll f3; for(int i=2;i<=x;i++) { f3=(f1+f2+1)%b; f1=f2; f2=f3; } return f3;}int main(){ ios::sync_with_stdio(false); f[1]=1; for(int i=2;i<=10000;i++) { ll tmp[100000]={1,1}; for(int ind=2;;ind++) { tmp[ind]=(tmp[ind-1]+tmp[ind-2]+1)%i; if(tmp[ind]==1&&tmp[ind-1]==1) { f[i]=ind-1; break; } } } int t=1; while(cin>>n>>b,n+b) { if(b==1) { cout<<"Case "<<t++<<": "<<n<<" "<<b<<" "<<0<<endl; continue; } ll cnt=n%f[b];// cout<<cnt<<endl; ll ans=solve(cnt); cout<<"Case "<<t++<<": "<<n<<" "<<b<<" "<<ans<<endl; } return 0;}

思路:

調(diào)用多少次函數(shù)的公式為 fib(i)=fib(i-1)+fib(i-2)+1

因?yàn)槭侨的模,而b的值在10000以?xún)?nèi),也就是會(huì)出現(xiàn)循環(huán)節(jié) 如果是取模的話(huà) fib(i)=(fib(i-1)+fib(i-2)+1)%b 找到fib(i-1)=1且fib(i)=1時(shí)的i-1值,就是當(dāng)前的循環(huán)節(jié)。提前打個(gè)表保存一下即可,也可以把這個(gè)循環(huán)節(jié)里面的所以數(shù)都算出來(lái)打表,更加方便。


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 天津市| 博野县| 金溪县| 阳泉市| 繁昌县| 柏乡县| 峨边| 嘉禾县| 天镇县| 景宁| 论坛| 讷河市| 吉安县| 和静县| 乌苏市| 社旗县| 封开县| 如皋市| 晋中市| 防城港市| 沾益县| 沙河市| 蓝田县| 中方县| 克什克腾旗| 长汀县| 益阳市| 牟定县| 龙游县| 萨嘎县| 四子王旗| 紫金县| 资中县| 邵阳县| 札达县| 二连浩特市| 余干县| 昔阳县| 黑山县| 嵩明县| 哈尔滨市|