HDU 4407 Sum 解题报告(容斥原理)
来源:互联网 发布:apache 压缩配置 编辑:程序博客网 时间:2024/06/06 10:07
Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1710 Accepted Submission(s): 477
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.
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".
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
13 32 2 31 1 3 41 2 3 6
Sample Output
70
Source
2012 ACM/ICPC Asia Regional Jinhua Online
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;int change[1001];int newnum[1001];int changeNum;int prime[100010];int primeNum;LL co_prime_sum(int n, int m){ LL ans = (long long)m*(m+1)/2 - (long long)n*(n-1)/2; n--; for(int i=1;i<(1<<primeNum);i++) { int tmp = 1; bool flag = false; for(int j=0;j<primeNum;j++) if(i&(1<<j)) tmp *= prime[j], flag = !flag; if(flag) ans-=((long long)m/tmp*(m/tmp+1)/2 - (long long)(n/tmp)*(n/tmp+1)/2)*tmp; else ans+=((long long)m/tmp*(m/tmp+1)/2 - (long long)(n/tmp)*(n/tmp+1)/2)*tmp; } return ans;}int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}void work(){ changeNum = 0; int n,m; scanf("%d%d",&n,&m); while(m--) { int op; scanf("%d", &op); if(op==1) { int l,r,p; scanf("%d%d%d",&l,&r,&p); LL res = 0; for(int i=0;i<changeNum;i++) if(change[i]>=l && change[i]<=r) { if(gcd(change[i], p)==1) res-=change[i]; if(gcd(newnum[i], p)==1) res+=newnum[i]; } primeNum = 0; for(int i=2;i*i<=p;i++) if(p%i==0) { prime[primeNum++]=i; while(p%i==0) p/=i; } if(p>1) prime[primeNum++]=p; res += co_prime_sum(l,r); printf("%I64d\n",res); } else { int x,v; scanf("%d%d",&x,&v); bool has = false; for(int i=0;i<changeNum;i++) if(change[i]==x) { has = true; newnum[i] = v; } if(!has) { change[changeNum] = x; newnum[changeNum] = v; changeNum++; } } }}int main(){ int T; scanf("%d", &T); while(T--) work();}
0 0
- HDU 4407 Sum 解题报告(容斥原理)
- HDU 4407 Sum (容斥原理)
- HDU 4407 - Sum(容斥原理)
- hdu 4407 SUM(容斥原理)
- hdu 4407 Sum(容斥原理)
- 解题报告: HDU_4407 Sum 容斥+暴力
- HDU 4407 SUM 【数论,容斥原理】
- hdu 4407 Sum(容斥原理)
- [容斥原理] hdu 4407 Sum
- HDU-4407-Sum(容斥原理)
- [容斥原理] hdu 4407 Sum
- Hdu 4407 Sum(容斥原理)
- hdu 4407 Sum (容斥原理)
- HDU 4407 Sum(容斥原理)
- HDU 4407 Sum <容斥原理>
- HDU 4407 Sum (容斥原理)
- HDU 4135 Co-prime 解题报告(因式分解 + 容斥原理)
- HDU 1695 GCD 解题报告(欧拉函数 + 容斥原理)
- js 事件绑定函数与this的使用
- Javascript 面向对象编程
- VS2010/MFC编程入门之前言
- ssh框架的多线程问题
- Java异常的抛出 (转载)
- HDU 4407 Sum 解题报告(容斥原理)
- 计算机指令
- Balloon.1 —— 组件体系组成
- Windows下编译WebRTC过程 -----包含最新版本webrtc同步!
- 使用inotify监视Linux文件变化
- 通过stanford-postagger对英文单词进行词性标注
- c_str
- unity的安装及软件介绍
- 一个好的程序员养成攻略