ACM--DP--HDOJ 1005--Number Sequence
2016-06-28 21:14
423 查看
HDOJ题目地址:传送门
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 150172 Accepted Submission(s): 36565
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.
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
Sample Output
题解:开始的时候用递归做超内存,后来发现 n 这个取值范围也太大了,后来看别人的,用DP,结果为48为一个
周期,这样就AC了
Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 150172 Accepted Submission(s): 36565
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.
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
题解:开始的时候用递归做超内存,后来发现 n 这个取值范围也太大了,后来看别人的,用DP,结果为48为一个
周期,这样就AC了
#include<stdio.h> #include<iostream> #include<string.h> #include<memory.h> using namespace std; int main(){ int a,b,n; int arr[48]; arr[1]=1; arr[2]=1; while(scanf("%d%d%d",&a,&b,&n)&&a&&b&&n){ for(int i=3;i<48;i++){ arr[i]=(a*arr[i-1]+b*arr[i-2])%7; } printf("%d\n",arr[n%48]); } }
相关文章推荐
- 抗锯齿方法paint.setAntiAlias(ture);paint.setFilterBitmap(true))
- Hibernate 之 初识 uuid 和 native
- vuejs切换视图同时保持状态
- UITableView的基本使用(二)
- HBuilder
- UITableView的基本使用(一)
- serialVersionUID介绍与作用
- UITTableView滑动删除
- APUE环境配置
- UE4 Smart_Pointer_Library
- android quick settings中去掉不需要的图标
- 日常坑-Illegal connection port value '3306'
- 设计模式---Builder模式
- ueditor 简单/复杂工具栏切换
- UI布局--四种布局
- 【J2EE】org.hibernate.exception.SQLGrammarException: could not execute query错误
- build 时候把场景加进去的时候,场景有对应的序号
- 设置UITextField的内容
- NGUI 指定视口大小
- iOS开发 UITabBar角标 红点形式 (tabBarItem.badgeValue)