Sum
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
#include <iostream>
#include <cstdio>
#include <map>
#define ll long long
#include <cstring>
using namespace std; const int N = ;
bool isprime[N];
map<int,int> mp;
int prime[],cnt;
int factor[];//素数因子 void Prime() //素数打表
{
cnt=;
memset(isprime,true,sizeof(isprime));
for(int i=; i<N; i++)
{
if(isprime[i])
{
for(int j=i+i; j<N; j+=i)
isprime[j]=false;
prime[cnt++]=i;
}
}
}
int gcd(int a,int b)
{
if(b==) return a;
return gcd(b,a%b);
} ll finds(int x,int n,int p) //容斥原理
{
int t,i,j,num,d,m=<<n;
ll ans=(ll)x*(x+)/;
for(i=; i<m; i++)
{
t=i;
j=num=;
d=;
while(t)
{
if(t&)
{
d*=factor[j];
num++;
}
j++;
t>>=;
}
n=x/d;
if(num&) ans-=(ll)d*(+n)*n/;
else ans+=(ll)d*(+n)*n/;
}
map<int,int>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++) //处理被改变了的数
{
if(it->first>x) continue;
if(gcd(it->first,p)==) ans-=it->first;
if(gcd(it->second,p)==) ans+=it->second;
}
return ans;
} int pri(int a) //求出a的素数因子
{
if(isprime[a])
{
factor[]=a;
return ;
}
int k=,i;
for(i=; i<cnt; i++)
{
if(a%prime[i]==) factor[k++]=prime[i];
while(a%prime[i]==) a/=prime[i];
if(a!=&&isprime[a])
{
factor[k++]=a;
return k;
}
}
return k;
} int main()
{
Prime();
int n,m,x,y,op,p,cs;
scanf("%d",&cs);
while(cs--)
{
scanf("%d%d",&n,&m);
mp.clear();
for(int i=; i<m; i++)
{
scanf("%d",&op);
if(op==)
{
scanf("%d%d%d",&x,&y,&p);
int num=pri(p);
printf("%I64d\n",finds(y,num,p)-finds(x-,num,p));
}
else
{
scanf("%d%d",&x,&p);
mp[x]=p;
}
}
} return ;
}
Sum的更多相关文章
- 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 ...
- [LeetCode] Sum of Left Leaves 左子叶之和
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- JVM调优之jstack找出最耗cpu的线程并定位代码
jstack可以定位到线程堆栈,根据堆栈信息我们可以定位到具体代码,所以它在JVM性能调优中使用得非常多.下面我们来一个实例找出某个Java进程中最耗费CPU的Java线程并定位堆栈信息,用到的命令有 ...
- C++获得系统路径
源码: [cpp] view plaincopy #include <Shlobj.h> #include <stdio.h> #include <locale.h> ...
- tomcat安全配置之禁用Directory Listing
什么是Directory Listing?通俗点讲,就是在webapp的目录下如果没有放置index.html或者类似的文件,如果从IE或者其它浏览器文章这个路径时,会惊喜的发现这个目录下的文件列表被 ...
- DHTML【1】
什么是DHTML呢?英文全称(Dynamic Hypertext Markup Language),即动态超文本标记语言,DHTML不是一个独立的新技术,而是多种技术的综合,它能使HTML变的更 ...
- [转] What is Ec/Io (and Eb/No)?
PS:http://www.telecomhall.com/what-is-ecio-and-ebno.aspx If someone asks you "Which Signal Leve ...
- Java基础知识强化96:Calendar类之获取任意年份的2月有多少天的案例
1. 分析: (1)键盘录入任意的年份 (2)设置日历对象的年月日 年:就是(1)输入的数据 月:是2(3月份) 日:是1 (3)把 ...
- codevs 1733 聪明的打字员 (Bfs)
/* Bfs+Hash 跑的有点慢 但是codevs上时间限制10s 也ok */ #include<iostream> #include<cstdio> #include&l ...
- MVC4数据访问EF查询linq语句的时候报错找不到表名问题
一天做项目的时候遇到这样的问题,MVC4用EF访问数据查询用linq语句的时候报错找不到表名:报错如下图: 研究了几种情况,最后还是没有找到正真的问题所在,不过可能是和路由解析问题有关,暂时还没有进行 ...
- gitHub项目框架使用排名
项目名称 项目简介 使用心得 1. react-native 这 个是 Facebook 在 React ...
- MySQL分支Percona,折腾中,先科普一下
官方网站:http://www.percona.com/ Percona 为 MySQL 数据库服务器进行了改进,在功能和性能上较 MySQL 有着很显著的提升.该版本提升了在高负载情况下的 Inno ...