华华开始学信息学(分块思想+树状数组)

本文介绍了一种高效的算法,用于在一个区间内快速计算一个数及其倍数的数量,通过将修改操作区分为大于sqrt(n)的直接树状数组操作和小于sqrt(n)的预先记录后查询时计算两部分,实现了O(sqrt(n))的查询速度。适合解决涉及大量倍数查找的问题。
摘要由CSDN通过智能技术生成

题目链接

思路:

区间一个数的倍数的个数,我们可以 O(1)  算出的,问题在于要找的倍数有很多,考虑分块思想,把修改操作分为两类。

大于 sqrt(n) :

直接在树状数组中暴力 add

小于 sqrt(n) :

先记录下来,在每次查询操作时暴力计算

时间复杂度:

一次查询约是 O(sqrt(n))

Code:
 

#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <vector>
#include <string>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
//#include <unordered_map>
#define guo312 std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ll long long
#define Inf LONG_LONG_MAX
#define inf INT_MAX
#define endl "\n"
#define PI 3.1415926535898
using namespace std;
const int N=1e5+10;
int n,m,lk;
ll c[N];
int lowbit(int x){
	return x&(-x);
}
void add(int x,ll num){
	for( ;x<=n;c[x]+=num,x+=lowbit(x));
}
ll query(int x){
	ll re=0;
	for( ;x>0;re+=c[x],x-=lowbit(x));
	return re;
}
ll lazy[N];
int main(){
guo312;
	cin>>n>>m; lk=sqrt(n);
	ll op,l,r,d,k;
	for(int i=1;i<=m;i++){
		cin>>op;
		if(op==1){
			cin>>d>>k;
			if(d<lk){
				lazy[d]+=k;
			}
			else{
				for(int j=d;j<=n;j+=d){
					add(j,k);
				}
			}
		}
		else{
			cin>>l>>r;
			ll ans=query(r)-query(l-1);
			for(int i=1;i<lk;i++){
				ans+=(r/i-(l-1)/i)*lazy[i];
			}
			cout<<ans<<endl;
		}
	}
	return 0;
}

好的,这是一个比较简单的数题,可以用矩阵快速幂求解。以下是 C++ 代码实现: ```c++ #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int MAXN = 2; const int MOD = 1e9 + 7; struct Matrix { LL m[MAXN][MAXN]; Matrix() { memset(m, 0, sizeof(m)); } Matrix operator * (const Matrix& b) const { Matrix c; for (int i = 0; i < MAXN; ++i) { for (int j = 0; j < MAXN; ++j) { for (int k = 0; k < MAXN; ++k) { c.m[i][j] = (c.m[i][j] + m[i][k] * b.m[k][j]) % MOD; } } } return c; } } base, res; Matrix qpow(Matrix a, int b) { Matrix ans; for (int i = 0; i < MAXN; ++i) { ans.m[i][i] = 1; } while (b) { if (b & 1) { ans = ans * a; } a = a * a; b >>= 1; } return ans; } LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } int main() { LL a, b, n; cin >> a >> b >> n; if (n == 1) { cout << a << endl; } else if (n == 2) { cout << b << endl; } else { base.m[0][0] = base.m[0][1] = base.m[1][0] = 1; res = qpow(base, n - 2); LL ans = gcd(a * res.m[0][0] % MOD + b * res.m[1][0] % MOD, b * res.m[1][0] % MOD + b * res.m[1][1] % MOD); cout << ans << endl; } return 0; } ``` 在这段代码中,我们定义了一个 `Matrix` 结构体,它表示一个 $2\times2$ 的矩阵。其中重载了 `*` 运算符,实现了矩阵乘法。 然后,我们定义了一个矩阵快速幂函数 `qpow`,用于求解矩阵的 $n$ 次方。 最后,在 `main` 函数中,我们通过快速幂求出矩阵 $base$ 的 $n-2$ 次方,然后根据题目要求求出 $\gcd(F_N, F_{N+1})$ 并输出即可。 需要注意的是,当 $n=1$ 或 $n=2$ 时,直接输出 $a$ 或 $b$ 即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

要用bug来打败bug

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值