当前位置:网站首页>C language problem solving number sequence
C language problem solving number sequence
2022-07-22 05:48:00 【Xa_ L】
C Language problem solving
Update one every day OJ subject , Today I will introduce how to find the array sequence .
Preface
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).
The 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 need to calculate f(n) Value .
One 、 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.
The input consists of multiple test cases . Each test case contains 3 It's an integer A、B and n (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros indicate the end of input , This test case is not processed .
Two 、 Output
For each test case, print the value of f(n) on a single line.
For each test case , Print on one line f(n) Value .
Sample Input
1 1 3
1 2 10
0 0 0
Sample Output
2 5
Ideas : The idea is simple , It's OK to find it directly in a cycle , But there's a problem , That's it n There's a lot of scope , This is the cycle of violence, then it is easy to timeout , And this question must not be as simple as it looks , First, we analyze a digital analog 7 There are seven possibilities 0,1,2,3,4,5,6; So the expression above can be f(n) = (A * f(n - 1) mod 7 + B * f(n - 2) mod 7) thus it can be seen ,f[n] Only 49 Medium condition , Then, according to a sample column, the pre 50 term , It can be found that the period is 49 Number of cycles , So just ask f[n%49] Just go .
Code
#include<stdio.h>
int main()
{
int a,b,n;
int f[100];
f[0]=1;f[1]=1;
while(1)
{
scanf("%d%d%d",&a,&b,&n);
if (a < 1 || b > 1000 || n < 1 || n > 100000000)
return -1;
if(a==0&&b==0&&n==0)
break;
for(int i=2;i<49;i++)
f[i]=(a*f[i-1]+b*f[i-2])%7;
printf("%d\n",f[(n-1)%49]);
}
return 0;
}
That's what we're going to talk about today .
边栏推荐
- 【微信小程序授权】获取用户手机号,昵称
- Write it down once Net analysis of CPU explosion of an intelligent transportation background service
- The ultra-low price of domestic chips, while being replaced by domestic products, has led to a large number of exports, putting pressure on American chips
- [JS foundation] common methods of operating arrays in JS
- QT 之事件的相关使用用例
- 【js基础】逻辑判断篇(易错点)
- 判断一个人靠不靠谱,就看这3点
- [featured] expression package bucket map applet (drainage, traffic master, rights and interests take away CPS, with PC background management)
- 聊聊接口设计的36个小技巧
- ES6 from getting started to mastering 06: arrow function this direction and precautions
猜你喜欢
Performance area: the more you know, the more you don't know
半导体产业动态杂谈
爱奇艺抖音和好,微博躺枪?
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
专注跨境支付一体化服务 iPayLinks获《财资》3A亚洲奖
Virtual machine cannot connect to the Internet
Postman - post请求application/json参数
[featured] expression package bucket map applet (drainage, traffic master, rights and interests take away CPS, with PC background management)
814. 二叉树剪枝 : 简单递归运用题
华为机试-题目核心考点
随机推荐
C语言解题——Number Sequence
ES6 from getting started to mastering 05: extended operators and arrow functions of functions
StringUtils 和 String 方法一览
The relevant person in charge of the state Internet Information Office answered reporters' questions on the decision to impose administrative penalties related to network security review on didi Globa
表情包小程序,副业日入400+
Onvif协议及协议测试工具使用详解
内存分配与回收策略
Those violations in the store will be punished by the official secondary punishment, the most common four
Doris code structure
814. Binary tree pruning: simple recursive problem
Pycocotools installation method - one step~
idea报错Port 8080 is already in use
函数防抖与函数节流
低代码平台搭建跨部门沟通系统案例分析
十大券商开户安全吗,风险大吗?
GAN的发展系列二(PGGAN、SinGAN)
QT 之事件的相关使用用例
ES6 from introduction to mastery 01:es6 introduction
When the cold winter of mobile phones comes, Samsung is forced to snipe Chinese mobile phones, and apple stands alone on the mountain to see the scenery
通用Service + 多线程完美解决批处理问题