Sum(hdu4407)
Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2974 Accepted Submission(s): 843
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".
3 3
2 2 3
1 1 3 4
1 2 3 6
0
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<queue>
7 #include<map>
8 typedef long long LL;
9 using namespace std;
10 bool prime[400005];
11 int ans[400005];
12 queue<int>que;
13 int cm[400005];
14 int ma[400005];
15 int flag[2000];
16 int gcd(int n,int m);
17 map<int,int>my;
18 int main(void)
19 {
20 int i,j,k;
21 int p,q;
22 int n,m;
23 int x,y;
24 int t;
25 for(i=2; i<=2000; i++)
26 {
27 if(!prime[i])
28 {
29 for(j=i; i*j<=400000; j++)
30 {
31 prime[i*j]=true;
32 }
33 }
34 }
35 int cnt=0;
36 for(i=2; i<=400000; i++)
37 {
38 if(!prime[i])
39 {
40 ans[cnt++]=i;
41 }
42 }
43 scanf("%d",&k);
44 while(k--)
45 { my.clear();
46 scanf("%d %d",&p,&q);
47 int bt=1;
48 for(i=0; i<=p; i++)
49 ma[i]=i;
50 while(q--)
51 {
52 scanf("%d",&n);
53 if(n==1)
54 {
55 scanf("%d %d %d",&x,&y,&t);
56 int r=0;
57 int yy=0;
58 int xy=t;
59 if(x>y)swap(x,y);
60 while(t>1)
61 {
62 if(r==0&&t%ans[yy]==0)
63 {
64 r=1;
65 que.push(ans[yy]);
66 t/=ans[yy];
67 }
68 else if(r==1&&t%ans[yy]==0)
69 {
70 t/=ans[yy];
71 }
72 else
73 {
74 r=0;
75 yy++;
76 }
77 }
78 x-=1;
79 int ak=0;
80 while(!que.empty())
81 {
82 cm[ak++]=que.front();
83 que.pop();
84 }
85 LL sum1=0;
86 LL sum2=0;
87 for(i=1; i<=(1<<ak)-1; i++)
88 {
89 int nn=0;
90 LL dp=1;
91 for(j=0; j<ak; j++)
92 {
93 if(i&(1<<j))
94 {
95 nn++;
96 dp*=(LL)cm[j];
97 }
98 }
99 if(nn%2)
100 {
101 LL ct=y/dp;
102 LL qt=x/dp;
103 LL ct1=ct+1;
104 LL qt1=qt+1;
105 sum1+=dp*ct1*ct/2;
106 sum2+=dp*qt1*qt/2;
107 }
108 else
109 {
110 LL ct=y/dp;
111 LL qt=x/dp;
112 LL ct1=ct+1;
113 LL qt1=qt+1;
114 sum1-=dp*ct1*ct/2;
115 sum2-=dp*qt1*qt/2;
116 }
117 } LL xc=1+x;LL yc=1+y;
118 LL ap1=(long long )(xc)*(long long)x/2;
119 LL ap2=(long long)(yc)*(long long)y/2;
120 ap2-=sum1;
121 ap1-=sum2;
122 ap2-=ap1;
123 for(j=1; j<bt; j++)
124 {
125 if(flag[j]>=x+1&&flag[j]<=y)
126 {
127 int cp=gcd(xy,ma[flag[j]]);
128 int cq=gcd(xy,flag[j]);
129 if(cq==1)
130 ap2-=flag[j];
131 if(cp==1)
132 ap2+=ma[flag[j]];
133 }
134 }
135 printf("%lld\n",ap2);
136 }
137 else
138 {
139 scanf("%d %d",&x,&t);
140 if(my[x]==0)
141 {my[x]=1;flag[bt]=x;bt++;}
142 ma[x]=t;
143 }
144 }
145 }
146 return 0;
147 }
148 int gcd(int n,int m)
149 {
150 if(m==0)
151 return n;
152 else if(n%m==0)
153 return m;
154 else return gcd(m,n%m);
155 }
Sum(hdu4407)的更多相关文章
- hdu4407 Sum 容斥原理
XXX is puzzled with the question below: 1, 2, 3, ..., n (1<=n<=400000) are placed in a line. T ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
随机推荐
- Scrapy爬虫框架的安装和使用
Scrapy是一个十分强大的爬虫框架,依赖的库比较多,至少需要依赖的库有Twisted 14.0.lxml 3.4和pyOpenSSL 0.14.在不同的平台环境下,它所依赖的库也各不相同,所以在安装 ...
- 学习java 7.11
学习内容: 泛型定义格式:<类型> 优点:把运行时期的问题提前到编译期间:避免了强制类型转换 泛型方法:public class Fanxing { public <T> ...
- CentOS7 安装配置RocketMQ --主从模式(master-slave)异步复制
机器信息 192.168.119.129 主 192.168.119.128 从 配置host[两台机器] vim /etc/hosts 添加 192.168.119.129 rocketmq-nam ...
- 【Reverse】DLL注入
DLL注入就是将dll粘贴到指定的进程空间中,通过dll状态触发目标事件 DLL注入的大概流程 https://uploader.shimo.im/f/CXFwwkEH6FPM0rtT.png!thu ...
- ORACLE中dual用法详解
基本上oracle引入dual为的就是符合语法1. 我们先从名称来说,dual不是缩写词,本身就是完整的单词.dual名词意思是对数,做形容词时是指二重的,二元的.2. Oracle中的dual表是一 ...
- 集合类——Map集合、Properties属性文件操作
1.Map集合 Collection集合的特点是每次进行单个对象的保存,若要对一对对象来进行保存就只能用Map集合来保存.即Map集合中一次可以保存两个对象,且这两个对象的关系是key = value ...
- Linux基础命令---sendmail发送邮件
sendmail sendmail是postfix中的一个发送邮件的代理程序,它负责发送邮件到远程服务器,并且可以接收邮件.sendmail在发送邮件的时候,默认从标砖输入读取内容,以".& ...
- linux下把一个用户从某个组中删除,而不删除用户
查看当前用户/登录用户 基本语法 whoami / who am I 用户组 介绍 类似于角色,系统可以对有共性的多个用户进行统一的管理. 新增组 语法 groupadd 组名 案例演示 添加test ...
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- win10 安装xmemcache及使用
一.下载链接 链接:https://pan.baidu.com/s/14J6Vc8TBEZeRoMp2MgcydQ 提取码:3q8r 二.安装 安装: memcached -d install 卸载 ...