数论分块

数论分块

分块整除

例题

G-几番烟雾,只有花难护

向上取整

注意相减时要加上模数再取模

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
#define db(x) cout<<x<<" "<<endl;
#define _db(a,n) for(int i=1;i<=n;i++) cout<<a[i]<<" ";cout<<endl;
#define mem(a) memset(a,0, sizeof(a))
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define per(i,r,l) for(int i=r;i>=l;i--)
const int M=998244353;
int fp(int b,int p){
	int res=1;
	while(p){
		if(p&1) res=res*b%M;
		b=b*b%M;
		p>>=1;
	}
	return res;
}
int s=fp(6,M-2)%M;
int f(int n){
	return((n*(n+1)%M*(n*2+1)%M)%M*s)%M;
}
void solve(){
	int n;cin>>n;
	int ans=0;
	for(int l=1,r,k;l<=n;l=r+1){
		k=(n+l-1)/l;
		if(k==1) r=n;
		else r=(n-1)/(k-1);
		ans+=k*(f(r)-f(l-1)+M)%M;//注意相减时要加上模数再取模
	}
	cout<<(ans+M)%M<<endl;
}
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
	int t;cin>>t;while(t--)
	solve();
	return 0;
}



根号分治

复习更新

P3396 哈希冲突 - 洛谷

#include <iostream>
#include<bits/stdc++.h>
#include <algorithm>
#include <cstring>
using namespace std;
#define endl '\n'
#define int long long
#define debug(x) cout<<x<<" "<<endl;
#define _debug(a,n) for(int i=0;i<n;i++) cout<<a[i]<<" ";cout<<endl;
#define mem(a) memset(a,0, sizeof(a))
const int N=2e5+5;
int a[N],ans[400][400];
signed main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
	int n,m,x,y;cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	int s=sqrt(n);
	for(int i=1;i<=n;i++){
		for(int p=1;p<=s;p++){
			ans[p][i%p]+=a[i];//模p,余k 
		}
	}	
	while(m--){
		char cmd;
		cin>>cmd>>x>>y;
		if(cmd=='A'){
			if(x<=s){
				cout<<ans[x][y]<<endl;
			} 
			else{
				int res=0;
				for(int i=y;i<=n;i+=x){
					res+=a[i];
				}
				cout<<res<<endl;
			}
		}
		else {
			for(int i=1;i<=s;i++){
				ans[i][x%i]+=y-a[x];	
			}
			a[x]=y;
		}
	}
	return 0;
}

posted @ 2024-03-15 19:58  mono_4  阅读(14)  评论(0编辑  收藏  举报