HDU 6102 GCDispower(容斥原理+数论+树状数组)
来源:互联网 发布:网络在线教育迎着 编辑:程序博客网 时间:2024/06/15 19:11
Description
给出一个
Input
第一行一整数
Output
对于每次查询,输出查询结果
Sample Input
2
3 1
3 2 1
1 3
6 3
6 5 4 3 2 1
1 6
2 6
3 5
Sample Output
1
8
5
0
Solution
离线,固定右端点R,维护每一个左端点L的答案,对于当前加入的点
Code
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;namespace fastIO { #define BUF_SIZE 100000 //fread -> read bool IOerror=0; inline char nc() { static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE; if(p1==pend) { p1=buf; pend=buf+fread(buf,1,BUF_SIZE,stdin); if(pend==p1) { IOerror=1; return -1; } } return *p1++; } inline bool blank(char ch) { return ch==' '||ch=='\n'||ch=='\r'||ch=='\t'; } inline void read(int &x) { char ch; while(blank(ch=nc())); if(IOerror)return; for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0'); } inline void readc(char &x) { char ch; while(blank(ch=nc())); if(IOerror)return; x=ch; } #undef BUF_SIZE};using namespace fastIO;typedef long long ll;typedef pair<int,int>P;const int maxn=100001;int T,n,m,p[maxn],pos[maxn],num[maxn],temp[maxn];ll ans[maxn];vector<P>fact[maxn],q[maxn];vector<int>pri[maxn];int mark[maxn];struct BIT{ #define lowbit(x) (x&(-x)) ll b[maxn]; void init() { memset(b,0,sizeof(b)); } void add(int x,ll v) { while(x<=n) b[x]+=v,x+=lowbit(x); } ll sum(int x) { ll ans=0; while(x) ans+=b[x],x-=lowbit(x); return ans; }}bit;void init(int n=100000){ for(int i=2;i<=n;i++) if(!mark[i]) for(int j=i;j<=n;j+=i) mark[j]=1,pri[j].push_back(i); for(int i=1;i<=n;i++) { int N=1<<pri[i].size(); for(int j=0;j<N;j++) { fact[i].push_back(P(1,1)); for(int k=0;k<pri[i].size();k++) if(j&(1<<k)) fact[i][j].first*=pri[i][k],fact[i][j].second*=-1; } } }ll Count(int x){ ll ans=0; for(int i=0;i<fact[x].size();i++) ans+=num[fact[x][i].first]*fact[x][i].second; return ans;}void Deal(int x,int v){ for(int i=0;i<fact[x].size();i++) num[fact[x][i].first]+=v;}int main(){ init(); read(T); //scanf("%d",&T); while(T--) { bit.init(); //scanf("%d%d",&n,&m); read(n),read(m); for(int i=1;i<=n;i++) { read(p[i]); //scanf("%d",&p[i]); pos[p[i]]=i,q[i].clear(); } for(int i=1;i<=m;i++) { int l,r; read(l),read(r); //scanf("%d%d",&l,&r); q[r].push_back(P(l,i)); } for(int i=1;i<=n;i++) { int res=0; for(int j=2*p[i];j<=n;j+=p[i]) if(pos[j]<i)temp[++res]=pos[j]; sort(temp+1,temp+res+1); for(int j=res;j>=1;j--) { ll sum=Count(p[temp[j]]/p[i])*p[i]; bit.add(1,sum),bit.add(temp[j]+1,-sum); Deal(p[temp[j]]/p[i],1); } for(int j=1;j<=res;j++)Deal(p[temp[j]]/p[i],-1); for(int j=0;j<q[i].size();j++) ans[q[i][j].second]=bit.sum(q[i][j].first); } for(int i=1;i<=m;i++)printf("%I64d\n",ans[i]); } return 0;}
阅读全文
0 0
- HDU 6102 GCDispower(容斥原理+数论+树状数组)
- hdu 6102 GCDispower 树状数组+容斥
- HDU-6102 GCDispower(莫比乌斯函数+树状数组)
- HDU 4777 Rabbit Kingdom --容斥原理+树状数组
- HDU 5792 World is Exploding (容斥原理+离散化+树状数组)
- HDU 2841 Visible Trees (数论,容斥原理)
- HDU 4390 Number Sequence 数论-(容斥原理)
- hdu——3501(数论之容斥原理)
- HDU 5072 Coprime(数论+容斥原理)
- HDU 4135 Co-prime(容斥原理 + 基础数论)
- hdu 4407 Sum(容斥原理+数论)
- hdu 1695 数论+容斥原理
- HDU 1695 GCD 【数论,容斥原理】
- HDU 4407 SUM 【数论,容斥原理】
- Hdu 1695 GCD (数论 容斥原理)
- [BZOJ4361]isn(dp+树状数组+容斥原理)
- 容斥原理 数论
- HDU 5792 World is Exploding 2016多校赛第五场 树状数组+容斥原理
- 初识Qt
- spring的InitializingBean在dubbo中的使用
- 空草子·如何利用QT和opengl进入幻想乡
- Markdown练习
- 【机器学习】Andrew Ng——03线性代数知识回顾
- HDU 6102 GCDispower(容斥原理+数论+树状数组)
- python爬虫系列之爬取百度文库(二)
- c#两个对象,同属性赋值
- Linux export 命令
- Android Studio 去掉标题栏
- Codeforces 846D Monitor(二维前缀和)
- 剑指offer-3-Python实现二维数组的查找
- HDU
- 2017 Multi-University Training Contest No.4