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 ...
随机推荐
- 海尔的U+智慧生活操作系统
通过一个手机APP就能操控家庭内的不同品牌的家电家居设备.在连接Wifi的状态下,海尔智能路由器能够自动连接上家庭里的智能冰箱.智能洗衣机.智能空调.智能烤箱.空气盒子等设备端.在智能手机上下载海尔U ...
- 向量的叉积 POJ 2318 TOYS & POJ 2398 Toy Storage
POJ 2318: 题目大意:给定一个盒子的左上角和右下角坐标,然后给n条线,可以将盒子分成n+1个部分,再给m个点,问每个区域内有多少各点 这个题用到关键的一步就是向量的叉积,假设一个点m在 由ab ...
- repeater 分页显示数据
表名:ChinaStates 控件:Repeater 查询代码DA: public class ChinaStatesDA { private DataClassesDataContext Conte ...
- (转)SVN源码管理(上&下)
原文地址:http://www.cnblogs.com/IPrograming/archive/2012/12/15/SVN_1.html 使用SVN进行源码管理(上) 在原来的项目中使用的源码管理工 ...
- WindowsServer2012 取消密码策略
1.打开“组策略管理”界面:管理工具 >> 组策略管理,如果找不到该功能,则需要先安装,在“服务管理器”里面“添加新功能”.(或者:开始——运行——gpmc.msc) 2.依次打开“林”& ...
- 设计模式:模版模式(Template Pattern)
android中的Activity框架,View框架中大量的on函数基本上都应用到了Template模式,掌握这一模式对于理解这些框架大有裨益. 模版模式 又叫模板方法模式,在一个方法中定义一个算法的 ...
- CSS中的盒子模型详解
很多人对盒子模型搞晕头了,下面通过一个简单的代码来分析盒子模型的结构! 为了方便方便观看!在第一个div中画了一个表格,并将其尺寸设置成与div内容大小一样!且设置body的margin和paddin ...
- PL/SQL中的变量案例解析
1.标量: ag1: declare v_ename emp.ename%type;--自己称为单变量 begin select ename into v_ename from emp where e ...
- 【转】深入理解Java内存模型(六)——final
与前面介绍的锁和volatile相比较,对final域的读和写更像是普通的变量访问.对于final域,编译器和处理器要遵守两个重排序规则: 在构造函数内对一个final域的写入,与随后把这个被构造对象 ...
- nodejs+express 4.x笔记
4.x与3.x变化比较大,包括安装以及api 一:安装express4.x 1. npm install express -g //express modules2. npm install expr ...