Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4278 Accepted Submission(s): 1227
Problem Description
XXX is puzzled with the question below:
1, 2, 3, ..., n (1<=n<=400000) are placed in a line. There are m (1<=m<=1000) operations of two kinds.
Operation 1: among the x-th number to the y-th number (inclusive), get the sum of the numbers which are co-prime with p( 1 <=p <= 400000).
Operation 2: change the x-th number to c( 1 <=c <= 400000).
For each operation, XXX will spend a lot of time to treat it. So he wants to ask you to help him.
Input
There are several test cases.
The first line in the input is an integer indicating the number of test cases.
For each case, the first line begins with two integers --- the above mentioned n and m.
Each the following m lines contains an operation.
Operation 1 is in this format: "1 x y p".
Operation 2 is in this format: "2 x c".
Output
For each operation 1, output a single integer in one line representing the result.
Sample Input
1 3 3 2 2 3 1 1 3 4 1 2 3 6
Sample Output
7 0
Source
2012 ACM/ICPC Asia Regional Jinhua Online
Recommend
zhoujiaqi2010 | We have carefully selected several similar problems for you: 6408 6407 6406 6405 6404
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define read(x,y) scanf("%d%d",&x,&y)
#define ll long long
int gcd(int x,int y) {return (y==0)?x:gcd(y,x%y);}
const int maxn =4e5+5;
/*
题目大意:给定一个连续序列1到n,
支持两种操作,查询和修改。
对于每次查询,给出其和答案。
对于修改,由于次数非常少,
用map存储一下即可,
其余的就是裸的容斥定理了,
先筛出上界范围内的素数,
并且求出每个数的素数集合,
这样就可以进行DFS容斥了。
求个数方便容斥,求和道理也是一样,
自己手动模拟下也可以发现,
求和也是有规律的,具体见代码的容斥方法。
*/
int n,m;
int op,x,y,z;
///筛素因子
vector<int> prim[maxn];
void sieve()
{
int vis[maxn];
for(int i=2;i<maxn;i++)
{
if(vis[i]) continue;
for(int j=i;j<maxn;j+=i)
{
prim[j].push_back(i);///筛素因子
vis[j]=1;
}
}
}
ll ret=0;
void dfs(int cnt,int num,int pos,int ub,int loc)///ub是范围上界,loc是
{
if(pos>=prim[loc].size()) return ;
int t=ub/num;
if(cnt&1) ret=ret-(ll)num*t*(t+1)/2;
else ret=ret+(ll)num*t*(t+1)/2;
for(int i=pos+1;i<prim[loc].size();i++)
if(num*prim[loc][i]<=ub) dfs(cnt+1,num*prim[loc][i],i,ub,loc);
}
ll solve(int x,int z)
{
ret=(ll)x*(x+1)/2;
for(int i=0;i<prim[z].size();i++)
{
dfs(1,prim[z][i],i,x,z);
}
return ret;
}
/*
100
100 100
1 1 3 4
*/
map<int,int> mp;
map<int,int>::iterator it;
int main()
{
sieve();
int t;scanf("%d",&t);
while(t--)
{
mp.clear(); scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%d",&x,&y,&z);
ll ans=solve(y,z)-solve(x-1,z);
///其余的特判
for(it=mp.begin();it!=mp.end();it++)
{
int tx=(it->first),ty=(it->second);
///cout<<tx<<" "<<ty<<endl;
if(tx<=y && tx>=x)
{
if(gcd(ty,z)==1) ans+=ty;
if(gcd(tx,z)==1) ans-=tx;///容斥下
}
}
printf("%lld\n",ans);
}
else
{
scanf("%d%d",&x,&y);
mp[x]=y;
}
}
}
return 0;
}