数列
給定a,b和遞推式
x[1]=1
x[n]=a*x[n-1]+b
小y想知道,當(dāng)n無(wú)窮大時(shí),x[n]是否無(wú)窮大?
輸入:
給定A,B,題意中a=A/1e9,b=B/1e9
輸出:
一行一個(gè)yes或no
樣例輸入:
0 1
2000000000 1
輸出:
no
yes
?
a=1時(shí),判斷b是否為0就行
x[n]=a*x[n-1]+b
x[n]+b/(a-1)=a*(x[n-1]+b/(a-1))
x[n]+b/(a-1)=a^n*(1+b/(a-1))
x[n]=(a^n(a+b-1)-b)/(a-1)
現(xiàn)在就很好做了,分類討論
注意不要把把A,B除了,會(huì)有精度問(wèn)題,直接把1e9作基準(zhǔn)
其實(shí)還有簡(jiǎn)單的方法,算出來(lái):
x[n]=a^n+b*a^(n-1)+b*a^(n-2).......+b
x[n]=a^n+b*(a^n-1)/(a-1)
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 double a,b; 7 int main() 8 { 9 while (cin>>a>>b) 10 { 11 a=a; 12 b=b; 13 //printf("%.9lf %.9lf\n",a,b); 14 if (a>=0&&a<1e9) 15 { 16 printf("no\n"); 17 } 18 if (a==1e9) 19 { 20 if (b!=0) 21 printf("yes\n"); 22 else printf("no\n"); 23 } 24 if (a>1) 25 { 26 if (b!=1e9-a) 27 printf("yes\n"); 28 else printf("no\n"); 29 } 30 if (a==-1e9) 31 { 32 printf("no\n"); 33 } 34 if (a<0&&a>-1e9) 35 { 36 printf("no\n"); 37 } 38 if (a<-1e9) 39 { 40 if (b!=1e9-a) 41 printf("yes\n"); 42 else printf("no\n"); 43 } 44 } 45 }?
轉(zhuǎn)載于:https://www.cnblogs.com/Y-E-T-I/p/7609372.html
總結(jié)
- 上一篇: 配置完centos 6以后,大概需要安装
- 下一篇: 解决spark中遇到的数据倾斜问题