hdu4407Sum(容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=4407
Sum
Total Submission(s): 1551 Accepted Submission(s): 232
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.
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".
1
3 3
2 2 3
1 1 3 4
1 2 3 6
7
0
给定一个数x可以表示成p = p1i1p2i2...pnin;要求x,x+1,x+2,...y与x互质的数的和,等价于求x,x+1,x+2,...y与p=p1p2...pn互质。(p1,p2,...,pn为素数)
x,x+1,x+2,...y与p互质直接求不好求,所以可以反过来求,先求出与x不互质的数的和sum,然后ans=总的和-sum。
sum[[x,x+1,x+2,...y]与p不互质]=sum[[1,2,...y]与p不互质] - sum[[1,2,x-1]与p不互质],
考虑p的素因子pi,则[1..y]中与pi不互质的数的个数是[y/pi].
然而,如果我们单纯将所有结果相加,有些数就会被统计多次(被好几个素因子整除)。所以,我们要运用容斥原理来解决。
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwsAAAA/CAIAAAAgx8PLAAAOaElEQVR4nO2dPe6rOhPGXaS5Ek2a6Nb0V0LKBihYAA0boMgKkNhCxA5oWEAWQMMWvIW0lGyBtxgdxMuH8RcGcp5fdc6fYI8HYj+xx2PWAwAAAACA/4cdbQAAAAAAwOmAQgIAAAAAmAKFBAAAAAAwBQoJAAAAAGAKFBIAAAAAwBR9hcQ5t2iHXc5sGwAAAADOj75CYuy8809ntg0AAAAA5wcKCQAAAABgChQSAAAAAMCUvRRS13XP5/P5fHLOoyhijOV5rl2XXdusc2xjAQAAAGCdvRRSWZZd10VR9H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Vlq7NW6okLqz9dYZ4gbnue5TAjRWiHDXJFJ4QAAAIAtrMUhNU3zfr9pVeXwhae945BO1VhnTBq+eFW1EEnvSRYOAAAA2MKaQmKMyaerobPNh+FQ2wYZ26xztsY6Y9zwJEkmOY0WmxZF0WThbNF7ZVmKBdCl/QYAAOCK6A88Zz6M/cy2/QzjaR6TOKGu68SrlghCAgAA4B78NAc6DFnCaWPa8/nUTnid5/maojUvHAAAANADCgnoMAQS0RqiSRi14F7zwgEAAAA9oJAAAAAAAKZAIQEAAAAATIFCAgAAAACY8j/6U7IwN5IvcAAAAABJRU5ErkJggg==" alt="" />
http://zh.wikipedia.org/wiki/容斥原理
比如要求[1,2,...,10]和10不互质的和,10=2*5,与2不互质的有2,4,6,8,10,与5不互质的有5,10;但还要减去与2*5不互质10;每个与pi不互质的是一个等差数列,
所以等差数列求和即可。
(因为m最多1000,所以修改的数不多,所以后面对修改的数单独处理就ok了。)
第一步要求x的所有因子。
第二步运用容斥原理,奇数项加,偶数项减。
再枚举修改的值就差不多了。
n<=400000,所以ans可能会超int
int size;//因子个数
int yinzi[];
void get_yinzi(int n)
{
size = ;
if (!(n & ))//为偶数
{
yinzi[size++] = ;
while (n /= , !(n & ));
}
int i, sqrtn = (sqrt(1.0 * n) + 0.5);
for (i = ; i <= sqrtn; i += )
{
if (n % i == )
{
yinzi[size++] = i;
while (n /= i, n % i == );
}
if (n == )
return;
}
if (n > )
yinzi[size++] = n;
}
//详见大牛博客http://www.cnblogs.com/xin-hua/p/3213050.html
1 #define llt long long int
llt solve(int r)//容斥原理,二进制法,总共有2size-1项
{
llt sum = ;
int bits;//1的个数,奇数加,偶数减
int pi;//
int i, j = << size, t;
int k;//选中的第几个因子
for (i = ; i < j; i++)//看i的二进制表示,假设size=4,i=1011,表示yinzi[3]∩yinzi[1]∩yinzi[0]即pi = yinzi[3]*yinzi[1]*yinzi[0]
{
pi = ;
t = i;
k = bits = ;
while (t)
{
if (t & )
{
pi *= yinzi[k];
bits++;
}
k++;
t >>= ;
}
t = r / pi;
if (bits & )//等差数列求和
sum += 1ll * (pi + pi * t) * t / ;
else
sum -= 1ll * (pi + pi * t) * t / ;
}
return sum;
}
int gcd(int a, int b)
{
return b ? gcd (b, a % b) : a;
} llt sum(int n)
{
return 1ll * ( + n) * n / ;
}
map<int, int> mp;//使用map对修改的数进行操作
map<int,int>::iterator it;
int main()
{
int tests, n, m, i, op, x, y, p, c;
llt ans;
scanf("%d", &tests);
while (tests--)
{
mp.clear();
scanf("%d%d", &n, &m);
while (m--)
{
scanf("%d", &op);
if (op == )
{
ans = ;
scanf("%d%d%d", &x, &y, &p);
if (x > y)
swap(x, y);
get_yinzi(p);
for (it = mp.begin(); it != mp.end(); it++)
{
if (it->first != it->second && it->first >= x && it->first <= y)
{
ans -= gcd(it->first, p) == ? it->first : ;
ans += gcd(it->second, p) == ? it->second : ;
}
}
printf("%I64d\n", ans + sum(y) - sum(x - ) - solve(y) + solve(x - ));
}
else
{
scanf("%d%d", &x, &c);
mp[x] = c;
}
}
}
return ;
}
hdu4407Sum(容斥原理)的更多相关文章
- hdu4059 The Boss on Mars(差分+容斥原理)
题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设 则 为一阶差分. 二阶差分: n阶差分: 且可推出 性质: 1. ...
- hdu2848 Visible Trees (容斥原理)
题意: 给n*m个点(1 ≤ m, n ≤ 1e5),左下角的点为(1,1),右上角的点(n,m),一个人站在(0,0)看这些点.在一条直线上,只能看到最前面的一个点,后面的被档住看不到,求这个人能看 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- BZOJ 2440: [中山市选2011]完全平方数 [容斥原理 莫比乌斯函数]
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3028 Solved: 1460[Submit][Sta ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- HDU5838 Mountain(状压DP + 容斥原理)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5838 Description Zhu found a map which is a N∗M ...
- 【BZOJ-2669】局部极小值 状压DP + 容斥原理
2669: [cqoi2012]局部极小值 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 561 Solved: 293[Submit][Status ...
- HDU 2204Eddy's爱好(容斥原理)
Eddy's爱好 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- CF451E Devu and Flowers (隔板法 容斥原理 Lucas定理 求逆元)
Codeforces Round #258 (Div. 2) Devu and Flowers E. Devu and Flowers time limit per test 4 seconds me ...
随机推荐
- IO流案例:1.复制多级文件夹 2.删除多级文件夹
package copy; /* 需求:复制多级文件夹 复制d:\\itcast(包含文件和子文件夹)到模块目录下 分析: d:\\itcast a.txt b.txt javaweb a.xml b ...
- scikit-learning教程(二)统计学习科学数据处理的教程二
模型选择:选择估计量及其参数 得分和交叉验证的分数 如我们所看到的,每个估计者都会公开一种score可以判断新数据的拟合质量(或预测)的方法.越大越好. >>> >>&g ...
- iphone6,键盘收起,H5页面下面出现空白
- Retrofit实现PUT网络请求,并修改Content-Type
@FormUrlEncoded @PUT(Constant.BOSS_HX_CHANGE_PHONE_INTERVIEW) Call<ResponseHxResultBean> handl ...
- Centos 6.5安装MySQL-python
报错信息: Using cached MySQL-python-1.2.5.zip Complete output from command python setup.py egg_info: ...
- ABP教程(四)- 开始一个简单的任务管理系统 - 实现UI端的增删改查
接上一篇:ABP教程(三)- 开始一个简单的任务管理系统 – 后端编码 1.实现UI端的增删改查 1.1添加增删改查代码 打开SimpleTaskSystem.sln解决方案,添加一个“包含视图的MV ...
- jQuery中面向对象思想实现盒子内容切换
这里主要是模拟小米官网中的首页的内容模块实现的主要动态效果 布局:采用了bootstrap框架进行布局,及其其中的字体图标 html: <!-- 内容 --> <div class= ...
- spring @value 为什么没有获取到值
1.配置文件的路径没有扫描到 2.注解的bean 不是通过spring托管的.bean 要通过spring 注解,引用的时候要用@Autowired 自动注入的bean 不要用new 出来的bean ...
- ios---setContentOffset
UIView * farmeView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self. ...
- SVN与TFS自动同步脚本(很实用)
一直都在园子里看文章,因为各种原因懒得写文章.最近稍得空闲,把这几天的工作成果分享一下. 因为工作需要,开发人员使用Qt进行系统移动端的开发,Qt的版本控制却不提供连接TFS的设置,只有使用svn.没 ...