Hdu6050 Funny Function(2017多校第2场)
来源:互联网 发布:ios广告拦截软件 编辑:程序博客网 时间:2024/06/11 08:45
Funny Function
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 422 Accepted Submission(s): 180
Problem Description
Function Fx,y satisfies:
![](http://acm.hdu.edu.cn/data/images/6050-1.jpg)
For given integers N and M,calculateFm,1 modulo 1e9+7.
![](http://acm.hdu.edu.cn/data/images/6050-1.jpg)
For given integers N and M,calculate
Input
There is one integer T in the first line.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
The next T lines,each line includes two integers N and M .
1<=T<=10000,1<=N,M<2^63.
Output
For each given N and M,print the answer in a single line.
Sample Input
22 23 3
Sample Output
233
Source
2017 Multi-University Training Contest - Team 2
Recommend
liuyiding
—————————————————————————————————
题目的意思是根据给出的式子,求第m行第一个是多少
思路:nm巨大绝对找规律或(矩阵)快速幂,现场找了1个多小时规律(还是太菜),打表找规律发现:
n为偶数是:am=2*(2^n -1)^(m-1)/3;
n为奇数是:am=(2^n-1)am-1-c(n/2) c(x)=4*c(x-1)+2;
矩阵快速幂搞一波。
注意偶数时除法取模要逆元
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <map> #include <cmath>#include <set> #include <stack> #include <queue> #include <vector> #include <bitset> #include <functional>using namespace std;#define LL long long const int INF = 0x3f3f3f3f;const LL mod = 1000000007;LL n, m;struct Matrix{ LL v[9][9]; Matrix() { memset(v, 0, sizeof v); }} dan;Matrix mul(Matrix a, Matrix b, int d){ Matrix ans; for (int i = 0; i < d; i++) { for (int j = 0; j < d; j++) { for (int k = 0; k < d; k++) { ans.v[i][j] += (a.v[i][k] * b.v[k][j]) % mod; ans.v[i][j] %= mod; } } } return ans;}Matrix qpow(Matrix a, LL k, int d){ Matrix ans = dan; while (k) { if (k & 1) ans = mul(ans, a, d); k >>= 1; a = mul(a, a, d); } return ans;}LL qpow(LL x, LL y){ LL ans = 1; while (y) { if (y & 1) ans *= x, ans %= mod; y >>= 1; x *= x; x %= mod; } return ans;}LL extend_gcd(LL a, LL b, LL &x, LL &y){ if (!b) { x = 1, y = 0; return a; } LL gcd = extend_gcd(b, a%b, x, y); LL tmp = x; x = y; y = tmp - (a / b)*y; return gcd;}int main(){ int t; scanf("%d", &t); while (t--) { scanf("%lld%lld", &n, &m); if (m == 1) { printf("1\n"); continue; } Matrix a, ans; LL ans1 = (qpow(2, n) - 1 + mod) % mod; if (n % 2) { dan.v[0][0] = 0, dan.v[0][1] = 1; a.v[0][0] = 4, a.v[0][1] = 0, a.v[1][0] = 2, a.v[1][1] = 1; ans = qpow(a, n / 2, 2); dan.v[0][0] = 1, dan.v[0][1] = 1; a.v[0][0] = ans1, a.v[0][1] = 0, a.v[1][0] = -ans.v[0][0], a.v[1][1] = 1; ans = qpow(a, m - 1, 2); printf("%lld\n", (ans.v[0][0]+mod)%mod); } else { ans1 = qpow(ans1, m - 1); LL x, y; extend_gcd(3, mod, x, y); ans1 = ans1*x%mod; ans1 = (ans1 * 2) % mod; printf("%lld\n", ans1); } } return 0;}
阅读全文
0 0
- Hdu6050 Funny Function(2017多校第2场)
- 2017多校训练第二场 hdu6050 Funny Function(数学+快速幂+逆元)
- HDU6050-Funny Function
- HDU6050-Funny Function
- 2017杭电多校联赛第二场-Funny Function (hdu6050)快速幂解数学方程
- 2017多校训练Contest2: 1006 Funny Function hdu6050
- HDU6050 Funny Function[矩阵快速幂]
- Funny Function————(hdu6050)矩阵快速幂
- hdu6050 Funny Function【打表+找规律+矩阵快速幂】
- 2017 多校训练第二场 HDU 6050 Funny Function
- 2017多校2 1006 Funny Function
- 2017多校第二场 HDU 6050 Funny Function 推公式
- Funny Function(HDU 6050)
- HDU-2017 多校训练赛2-1006-Funny Function
- Funny Function
- 2017多校联合第二场 1006题 hdu 6050 Funny Function 递推公式 / 矩阵快速幂
- HDU 6050 Funny Function(矩阵)
- HDU 6050 Funny Function(快速幂)
- 移植oprofile到海思
- Android_属性动画
- 学习linux溢出-1
- Predix发布全新UI视觉系统, 引领工业物联网应用平台新体验
- Android的.so文件、ABI和CPU的关系
- Hdu6050 Funny Function(2017多校第2场)
- 网络请求判断有无网络
- 滚动到指定的nav固定在顶部
- 极角排序以及凸包模板
- centos pip install MySql-python
- Shell命令之用脚本判断源码包是否安装
- oracle 利用 ROW_NUMBER() OVER实现重复数据只取一条并且循环取直到满足所需的条数
- java中HashMap详解
- Qt之界面实现技巧之按钮,边框,主题背景等