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 ...
随机推荐
- SKPhysicsJointSliding类
继承自 NSObject 符合 NSCoding(SKPhysicsJoint)NSObject(NSObject) 框架 /System/Library/Frameworks/SpriteKit. ...
- Sql排名和分组排名
在很多时候,都有排名这个功能,比如排行榜,并且还需要分页的功能,一般可以再select的时候按照某一字段 oorder by XX desc,这样limit 查找就可以得到排名信息,但是有时候是需要多 ...
- c++ map与 qt QMap insert 区别
当插入相同key的字段时, c++ map 会保留原来的字段, QMap 则会取代原来的字段.
- Can't connect to local MySQL server through socket '/tmp/mysql.sock' (111)
遇着这个提示的话,如果本地只安装了一个mysql,这里写的很详细的 http://www.blogjava.net/asenyifei/articles/82575.html 看这里可以解决,如果本地 ...
- 在top命令下kill和renice进程
For common process management tasks, top is so great because it gives an overview of the most active ...
- 富文本 SpannableString Span
经典使用场景 SpannableStringBuilder needStartSSB = new SpannableStringBuilder("需要"); SpannableSt ...
- (转)关于c#中的事件
原文链接http://blog.csdn.net/joyhen/article/details/8500211 如有不明白的地方欢迎加QQ群14670545 探讨 最近在看委托,然后看到事件,以前一直 ...
- [linux常用命令]查看当前文件夹或该文件夹下文件(夹)的大小
du -sh *(星号表示当前所有文件夹)可以查看当前目录下各个文件夹的大小,-s表示只显示当前文件夹(不加-s你可以看到所有文件夹下的子文件夹的大小,太多了),-h表示以合适的大小查看.(可以用- ...
- vsftp关于"550 create directory operation failed"问题解决
前提: 昨天晚上配置好了vsftp, 但登陆后,除了浏览,什么也干不了.(如新建文件/文件夹, 删除文件, 重命名等都不可操作) 都是弹出 "550 create directory ope ...
- spark 操作hbase
HBase经过七年发展,终于在今年2月底,发布了 1.0.0 版本.这个版本提供了一些让人激动的功能,并且,在不牺牲稳定性的前提下,引入了新的API.虽然 1.0.0 兼容旧版本的 API,不过还是应 ...