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 ...
随机推荐
- bzoj 2560: 串珠子【状压dp】
正难则反,设g[s]为集合s不一定联通的方案数,这个很好求,把边数+1乘起来即可,f[s]为s一定联通的方案数 f考虑容斥,就是g[s]-Σf[nw]*g[s^nw],nw是s的子集,这样就减掉了不联 ...
- oracle常用的一些查询命令
.查看所有用户 select * from dba_users; select * from all_users; select * from user_users; .查看用户或角色系统权限(直接赋 ...
- C#操作高低位
比如一个数 想把高位 与地位拆开,分别显示 可以用这个办法 x=(uint16)(x>>8) (高字节向右移动8位 相当于*256) X=(UINT16)(X &0X00FF) ...
- bzoj 4542 [Hnoi2016]大数 (坑)
题面 https://www.lydsy.com/JudgeOnline/problem.php?id=4542 题解 Code #include<bits/stdc++.h> using ...
- 组合数+容斥原理 UVALive 7040 Color(14西安F)
题目传送门 题意:n盆花涂色,相邻不能涂相同的颜色,从m中颜色选取k种颜色涂,保证正好有k种颜色 分析:从m中颜色选取k种就是C (m, k),然后第一个有k种选择,之后的都有k-1种选择,这样是不超 ...
- 在vue组件的stylus样式总 取消search类型的input按钮中默认样式
在vue组件的stylus样式中 取消search类型的input按钮中默认样式 记录一个坑 环境 Vue组件 使用了Stylus的CSS风格. 问题 input输入框使用了 type="s ...
- C/C++程序计时函数gettimeofday的使用
linux 环境下 用 clock_t发现不准. 换用 //头文件 #include <sys/time.h> //使用timeval start, end; gettimeofday ...
- CentOS 6.9:MySQL Daemon failed to start.
[root@Server_1 12:02:58 ~ 25]#service mysqld start MySQL Daemon failed to start.Starting mysqld: [ro ...
- 关于Control.Dispatcher.BeginInvoke卡界面
Control.Dispatcher.BeginInvoke里的逻辑由UI线程执行,如果内部包含耗时操作就会造成界面卡住. Action.BeginInvoke里的逻辑,将在一个新开的线程中执行,而不 ...
- FPGA内部RAM的初始化
Altera的RAM初始化文件格式是mif和hex. QuartusII自带的RAM初始化工具很方便产生初始化文件. Xilinx的RAM初始化文件格式是coe, 在vivado中软件会将coe文件变 ...