【51nod 2023省选联训 第二场】 B 迭代

不是一道简单题,为什么题解不给详解……


记嵌套 i i i 次后的分子分母分别为 a i , b i a_i,b_i ai,bi

a 0 = x , b 0 = 1 a i b i = a i − 1 b i − 1 + b i − 1 a i − 1 2 ⇒ { a i = ( a i − 1 ) 2 + ( b i − 1 ) 2 b i = 2 a i − 1 b i − 1 { a i + b i = ( a i − 1 + b i − 1 ) 2 = ( ( ( a j + b j ) 2 ) 2 ) 2... ( n u m b e r   o f   2   e q u i v e   ( i − j ) ) = ( a j + b j ) 2 n a i − b i = ( a i − 1 − b i − 1 ) 2 = ( ( ( a j − b j ) 2 ) 2 ) 2... ( n u m b e r   o f   2   e q u i v e   ( i − j ) ) = ( a j − b j ) 2 n \begin{aligned} & a_0 = x,b_0 = 1 \\ &\frac{a_i}{b_i} = \frac{\frac{a_{i - 1}}{b_{i - 1}} + \frac{b_{i-1}}{a_{i-1}}}{2} \\ \Rightarrow &\begin {cases} &a_i = (a_{i-1})^2 + (b_{i - 1})^2 \\ &b_i = 2 a_{i - 1} b_{i-1} \end{cases} \\ &\begin{cases} a_{i} + b_{i} = (a_{i - 1} + b_{i - 1})^2 = (((a_{j} + b_{j}) ^ 2) ^ {2})^{2...(number \ of \ 2 \ equive \ (i - j))} = (a_j + b_j) ^ {2^n} \\ a_{i} - b_{i} = (a_{i - 1} - b_{i - 1})^2 = (((a_{j} - b_{j}) ^ 2) ^ {2})^{2...(number \ of \ 2 \ equive \ (i - j))} = (a_j - b_j) ^ {2^n} \end{cases} \end{aligned} a0=x,b0=1biai=2bi1ai1+ai1bi1{ai=(ai1)2+(bi1)2bi=2ai1bi1{ai+bi=(ai1+bi1)2=(((aj+bj)2)2)2...(number of 2 equive (ij))=(aj+bj)2naibi=(ai1bi1)2=(((ajbj)2)2)2...(number of 2 equive (ij))=(ajbj)2n

经典韦达算 a i , b i a_i,b_i ai,bi

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define fi first
#define se second
#define db double
#define LL long long
#define int long long
#define PII pair <int, int>
#define ULL unsigned long long
#define MP(x,y) make_pair (x, y)
#define rep(i,j,k) for (int i = (j); i <= (k); i++)
#define per(i,j,k) for (int i = (j); i >= (k); i--)

template <typename T>
void read (T &x) {
	x = 0; T f = 1; 
	char ch = getchar ();
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1;
		ch = getchar ();
	}
	while (ch >= '0' && ch <= '9') {
		x = (x << 3) + (x << 1) + ch - '0';
		ch = getchar ();
	}
	x *= f;
}
template <typename T, typename... Args>
void read (T &x, Args&... Arg) {
	read (x), read (Arg...);
}
const int MaxPrint = 1000;
int Poi_For_Print, Tmp_For_Print[MaxPrint + 5];
template <typename T>
void write (T x) {
	if (x == 0) {
		putchar ('0');
		return;
	}
	bool flag = (x < 0 ? 1 : 0);
	x = (x < 0 ? -x : x);
	while (x) Tmp_For_Print[++Poi_For_Print] = x % 10, x /= 10;
	if (flag) putchar ('-');
	while (Poi_For_Print) putchar (Tmp_For_Print[Poi_For_Print--] + '0');
}
template <typename T, typename... Args>
void write (T x, Args... Arg) {
	write (x); putchar (' '); write (Arg...);
}
template <typename T, typename... Args>
void print (T x, char ch) {
	write (x); putchar (ch);
}
template <typename T> T Max (T x, T y) { return x > y ? x : y; }
template <typename T> T Min (T x, T y) { return x < y ? x : y; }
template <typename T> T Abs (T x) { return x > 0 ? x : -x; }

const int Mod = 1000000007;

int quick_pow (int x, int y, int Mod) {
	int res = 1;
	while (y) {
		if (y & 1) res = res * x % Mod;
		x = x * x % Mod; y >>= 1;
	}
	return res;
}
int Calc (int x, int n) {
	return quick_pow (x, quick_pow (2, n, Mod - 1), Mod);
}

signed main () {
//	freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.in", "r", stdin);
//	freopen ("C:\\Users\\Administrator\\Desktop\\lihan\\1.out", "w", stdout);

	int t, x, n;
	read (t);
	while (t--) {
		read (x, n), n--;
		int tmp1 = Calc (x + 1, n), tmp2 = Calc (x - 1, n);
		int a = (tmp1 + tmp2) * quick_pow (2, Mod - 2, Mod) % Mod, b = (tmp1 - tmp2 + Mod) % Mod * quick_pow (2, Mod - 2, Mod) % Mod;
		print (a * quick_pow (b, Mod - 2, Mod) % Mod, '\n');
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值