ACM1005:Number Sequence
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 2 10
0 0 0
5
- #include<stdio.h>
- #include<string.h>
- #define SIZE 2
- long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE]);
- int main()
- {
- int A, B;
- long long int n;
- long long int factor[SIZE][SIZE];
- long long int ans[SIZE][SIZE];
- while (scanf("%d%d%lld", &A, &B, &n) && (A||B||n))
- {
- memset(factor, 0, 2 * 2 * sizeof(int));
- memset(ans, 0, 2 * 2 * sizeof(int));
- factor[0][0] = A;
- factor[0][1] = B;
- factor[1][0] = 1;
- factor[1][1] = 0;
- //矩阵快速幂
- for (int i = 0; i < SIZE; i++)
- ans[i][i] = 1;
- n = n - 2;
- while (n > 0)
- {
- if (n & 1)
- memcpy(ans, multiple(ans, factor), 2 * 2 * sizeof(long long int));
- memcpy(factor, multiple(factor, factor), 2 * 2 * sizeof(long long int));
- n >>= 1;
- }
- printf("%lld\n", (ans[0][0] + ans[0][1]) % 7);
- }
- return 0;
- }
- long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE])
- {
- int i, j, k;
- long long int c[SIZE][SIZE];
- memset(c, 0, 2 * 2 * sizeof(long long int));
- for(i = 0; i < SIZE; i++)
- for(j = 0; j < SIZE; j++)
- for (k = 0; k < SIZE; k++)
- {
- c[i][j] += a[i][k] * b[k][j];
- c[i][j] = c[i][j] % 7;
- }
- return c;
- }
参考文章:
https://blog.csdn.net/codeswarrior/article/details/81258928
https://www.cnblogs.com/cmmdc/p/6936196.html
ACM1005:Number Sequence的更多相关文章
- 1005:Number Sequence(hdu,数学规律题)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- 递推:Number Sequence(mod找规律)
解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...
- HDU 1711:Number Sequence(KMP)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- POJ 1019:Number Sequence 二分查找
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36013 Accepted: 10409 ...
- HDU 1711:Number Sequence(KMP模板,求位置)
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu5014:number sequence对称思想
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5014 题目大意:给定数组 a[]={0,1,2......n} 求一个数组b[] 元素也为0.... ...
- HDU1711:Number Sequence
Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...
- hdu1005 Number Sequence(寻找循环节)
主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...
- [AX]AX2012 Number sequence framework :(三)再谈Number sequence
AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...
随机推荐
- Linux基础和常用命令
经常使用的命令: #查看端口被占用情况 netstat -tunlp|grep #查看java进程 ps -ef|grep java #压缩前端工程 rar a -ep1 ./update/win32 ...
- redis下的持久化保存
rdb.h rdb.c ---> 完成数据保存到临时文件,再利用rename保存到指定文件的过程: 如果需要写一个数据持久化保存的功能时,可以参考这部分代码: //rdb API int ...
- Chapter 2 Secondary Sorting:Detailed Example
2.1 Introduction MapReduce framework sorts input to reducers by key, but values of reducers are arbi ...
- 深入了解Node模块原理
深入了解Node模块原理 当我们编写JavaScript代码时,我们可以申明全局变量: var s = 'global'; 在浏览器中,大量使用全局变量可不好.如果你在a.js中使用了全局变量s,那么 ...
- python读取xml文件报错ValueError: multi-byte encodings are not supported
1.在使用python对xml文件进行读取时,提示ValueError: multi-byte encodings are not supported 很多贴子上说把xml的编码格式改为,就可以正常执 ...
- 把对象缓存到HttpRuntime.Cache里,你能安全地使用它吗?
每每勤勤恳恳,思来想去,趁还有激情,先把它记录下来... 定义一个Stu的类: public class Stu { public string Name { get; set; } public i ...
- 1053. [HAOI2007]反素数ant【DFS+结论】
Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x ,则称x为反质数.例如,整数 ...
- windows C++ new/delete内存大小
转载自:https://blog.csdn.net/will_hsbsch/article/details/21124055 windows 上,但使用C++语言new一块内存,用指针P指向这块内存, ...
- 【问题】 cookie 不保存特殊字符 解决办法
遇到的问题: 在做项目,用geolocation 获取经纬度,格式如(23.1133,113.2552) ,想保存到cookie中备用.但读取cookie出来之后发现逗号变成了 %2c. 找到的原因 ...
- Lua库-string库
string.len(s) string.rep(s,n) string.lower(s) string.upper(s) string.sub(s,i);//截取s第i个开始的后缀 string.s ...