Tinh phi trong BIT
```
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define maxn (ll)(1e6+5)
- #define mod (ll)(1e9+7)
- ll BIT[maxn],a[maxn],n,q,i;
- ll phi[maxn],f[maxn];
- bitset<maxn> num;
- vector<ll> primes;
- void phi_cal(){
- phi[1]=1;
- for(ll i=2;i<maxn;i++){
- if(!num[i]){
- phi[i]=i-1;
- primes.push_back(i);
- }
- for(ll j=0;j<primes.size();j++){
- ll x = i*primes[j];
- if(x>=maxn) break;
- num.set(x);
- if(i%primes[j]==0){
- phi[x]=phi[i]*primes[j];
- break;
- }
- else{
- phi[x]=phi[i]*(primes[j]-1);
- }
- }
- }
- }
- void pre_cal(){
- for(ll i=1;i<maxn;i++){
- for(ll j=i,cnt=1;j<maxn;j+=i,cnt+=1){
- f[j]+=i*phi[cnt];
- }
- }
- }
- void update(ll index, ll value){
- for(;index<=n;index+=index&-index){
- BIT[index]+=value;
- BIT[index]%=mod;
- }
- }
- ll query(ll index){
- ll sum=0;
- for(;index>0;index-=index&-index){
- sum+=BIT[index];
- sum%=mod;
- }
- return sum;
- }
- int main(){
- phi_cal();
- pre_cal();
- cin>>n;
- for(i=1;i<=n;i++){
- cin>>a[i];
- update(i,f[a[i]]);
- }
- cin>>q;
- char t;
- ll fi,se;
- while(q--){
- cin>>t>>fi>>se;
- if(t=='C') cout<<(query(se)-query(fi-1)+mod)%mod<<'\n';
- if(t=='U') {
- update(fi,mod-f[a[fi]]);
- a[fi]=se;
- update(fi,f[a[fi]]);
- }
- }
- return 0;
- }
Nhận xét
Đăng nhận xét