mod fib

#include <stdio.h>
#include <math.h>
long long n, b, k;

struct state {
	long long s[2][2];
	state(long long a = 0, long long b = 0, long long  c = 0, long long d = 0) {
		s[0][0] = a, s[0][1] = b, s[1][0] = c, s[1][1] = d;
	}
}tmp(1, 0, 0, 1), c(1, 1, 1, 0);

state count(const state& p, const state& q) {
	state f;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 2; j++)
			f.s[i][j] = (p.s[i][0] * q.s[0][j] + p.s[i][1] * q.s[1][j]) % b;
	return f;
}

state solve(long long k) {
	if (k == 0) return tmp;
	else if (k == 1) return c;

	state a = solve(k / 2);

	a = count(a, a);
	if (k % 2) a = count(a, c);
	return a;
}

int main () {
	int m;
	while (scanf("%lld%d", &n, &m) == 2) {
		b = m;
		if (n) {
			state ans = solve(n - 1);
			k = ans.s[0][0];
		} else
			k = 0;
		printf("%lld\n", k);
	}
	return 0;
}

# U501347 「Stoi2025」爱的飞行日记 ## 题目背景 ![](bilibili:BV1fx411N7bU?page=125) ## 题目描述 $t$ 组询问,每次询问给定正整数 $n,m$,计算 $$\prod_{a_1=1}^{m}\prod_{a_2=1}^{m}\cdots\prod_{a_n=1}^{m}\operatorname{lcm}(f_{a_1},f_{a_2},\dots,f_{a_n})\bmod{37426667}$$ 的值。其中 $f_i$ 是斐波那契数,满足 $f_1=f_2=1$,且 $f_i=f_{i-1}+f_{i-2},\forall n\ge3$。 ## 输入格式 第一行输入一个正整数 $t$ 表示询问组数。 接下来 $t$ 行,每行两个正整数 $n,m$ 表示一次询问。 ## 输出格式 每次询问输出一行一个整数表示答案。 ## 输入输出样例 #1 ### 输入 #1 ``` 2 1 3 2 3 ``` ### 输出 #1 ``` 2 32 ``` ## 说明/提示 #### 样例解释 对于第一组询问,有答案为 $f_1f_2f_3=1\times1\times2=2$。 对于第二组询问,当 $a_1,a_2\in\{1,2\}$ 时 $\operatorname{lcm}(f_{a_1},f_{a_2})=1$,否则 $\operatorname{lcm}(f_{a_1},f_{a_2})=2$。故答案为 $2^5=32$。 #### 数据范围与限制 **本题采用捆绑测试,各 Subtask 的限制与分值如下。** | Subtask No. | $t\le$ | $n\le$ | $m\le$ | 分值 | | :-: | :-: | :-: | :-: | :-: | | $1$ | $1$ | $2$ | $2 \times 10^3$ | $13$ | | $2$ | $5$ | $2 \times 10^5$ | $2 \times 10^5$ | $24$ | | $3$ | $5$ | $2 \times 10^7$ | $2 \times 10^7$ | $36$ | | $4$ | $300$ | $2 \times 10^{17}$ | $2 \times 10^7$ | $27$ | 对于所有数据,满足 $1 \le t \le 300, 1 \le n \le 2 \times 10^{17}, 1 \le m \le 2 \times 10^7$。 #include<bits/stdc++.h> #define md 37426667 #define int unsigned long long using namespace std; inline int in() { int k=0,f=1; char c=getchar(); while(c<'0'||c>'9') { if(c=='-') f=-1; c=getchar(); } while(c>='0'&&c<='9') k=k*10+c-'0',c=getchar(); return k*f; } inline int out(int a) { if(a<0) putchar('-'),a=-a; if(a<10) putchar(a+'0'); else out(a/10),putchar(a%10+'0'); return 0; } inline int gcd(int a,int b) { while(b){ int t=b; b=a%b; a=t; } return a; } int lcm(int a,int b) { return (a/gcd(a,b))*b%md; } int fib[20000005]; main() { int t=in(); fib[1]=fib[2]=1; for(int i=3; i<=20000000;i++) fib[i]=(fib[i-1]+fib[i-2])%md; while(t--) { int n=in(),m=in(); int ans=1; if(n==1) for(int i=1; i<=m; i++) ans=(ans*fib[i])%md; else if(n==2) { for(int xx=1; xx<=m; xx++) { for(int yy=1; yy<=m; yy++) { ans=(ans*lcm(fib[xx],fib[yy]))%md; } } } out(ans%md); } }
最新发布
03-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值