LightOJ 1070 Algebraic Problem
来源:互联网 发布:网络日报 编辑:程序博客网 时间:2024/05/16 16:05
Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessarily have to be real numbers.
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains three non-negative integers, p, q and n. Here p denotes the value of a+b and qdenotes the value of ab. Each number in the input file fits in a signed 32-bit integer. There will be no such input so that you have to find the value of 00.
For each test case, print the case number and (an+bn) modulo 264.
2
10 16 2
7 12 3
Case 1: 68
Case 2: 91
嗯,一开始说对2^64取模,一脸懵逼,心想这个怎么做啊。然后用的是unsigned long long 超过64会自动取模的。。。。emmmmmm那你如果是个负数也取模成正数么。
估计应该是的,负数应该变成正的,实际上也不能算错吧。
这个咋一看会想到(a+b)^n然后,展开发现仍旧不能做,实际上我们换个想法,能不能找到递推式呢?不难发现a^(n+1) + b^(n+1) = p(a^n+b^n) - q(a^(n-1) + b^(n-1)。
也就是要求的F(n) = p*F(n-1) - q*F(n-2)。
然后就可以矩阵快速幂加速了。
#include <iostream>#include <cstdio>#include <algorithm>using namespace std;typedef unsigned long long LL;const LL mod = 7;const int Size = 2;long long n,p,q;struct matrix{ LL a[4][4]= {{0}}; matrix operator *(const matrix b)const { matrix ans; for(int i = 0; i < Size; ++i) for(int k = 0; k < Size; ++k) for(int j = 0; j < Size; ++j) { ans.a[i][j] = (ans.a[i][j] + a[i][k]*b.a[k][j]); } return ans; }};LL get_ans(){ if(n == 0)return 2; matrix ans,base; ans.a[0][0] = p; ans.a[0][1] = 2; base.a[0][0] = p; base.a[0][1] = 1; base.a[1][0] = -q; n--; while(n) { if(n & 1)ans = ans*base; base = base*base; n >>= 1; } return ans.a[0][0];}int main(){ //cout <<INT_MAX<<endl; int t; int ca = 0; scanf("%d",&t); while(t--) { scanf("%lld%lld%lld",&p,&q,&n); printf("Case %d: %llu\n",++ca,get_ans()); } return 0;}
- LightOJ 1070 Algebraic Problem
- LightOJ 1070 - Algebraic Problem 矩阵快速幂
- Algebraic Problem LightOJ
- LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)
- Lightoj 1070 Algebraic Problem(矩阵快速幂)
- LOJ 1070 Algebraic Problem
- Light oj 1070 - Algebraic Problem
- lightoj1070 - Algebraic Problem(Matrix)
- LIGHT OJ1070 Algebraic Problem
- LOJ 1070 - Algebraic Problem(矩阵快速幂啊)
- uva 10655 Algebraic Problem(矩阵快速幂)
- LightOJ - 1102 Problem Makes Problem
- LightOj 1179 Josephus Problem
- LOJ1070:Algebraic Problem(矩阵快速幂 & 数学 a^n+b^n)
- LightOJ 1004 - Monkey Banana Problem
- lightoj 1004 - Monkey Banana Problem
- LightOJ 1004 Monkey Banana Problem
- LightOJ 1004 - Monkey Banana Problem
- StreamTook(周考1)
- Xutils
- 在web应用中使用solr(一)
- 搭建自己的Linux根文件系统
- 重写Python脚本时关于建立框架的想法
- LightOJ 1070 Algebraic Problem
- 收费系统——SQLHelper类分析+重构
- 周考1(配置)
- linux 添加python 模块搜索路径(以thrift为例)
- 光纤分类——多模和单模
- java
- protocol buffers 安装问题
- navicat 提示“1045 access denied for user 'root'@'localhost' ”解决方法
- 关于JavaScript的一些方法