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.

 
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".
 
Output
For each operation 1, output a single integer in one line representing the result.
 
Sample Input

1
3 3
2 2 3
1 1 3 4
1 2 3 6

Sample Output
7
0
#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的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. 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 ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [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 ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. ActionScript通用开发框架

    ActionScript 3(简称as)自2006年诞生以来,出现了一大批很优秀框架.就我的知识领域,运用包括pureMVC.pushButton Engine(组件框架).Robotlegs.Ash ...

  2. LeetCode——Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  3. 不相交集python实现

    1.不相交集是解决等价关系的一种数据结构,执行合并和查找的速度都很快,M次执行合并和查找的执行时间为(M*logN). 在一个集合中.对于每一对元素(a,b),a,b∈S,对于关系R假设满足以下三个条 ...

  4. 通知中心 NSNotificationCenter

    NSNotificationCenter 通知中心提供了一种在程序内广播信息的途径,一个NSNotificationCenter对象本质上是一个通知分发表(notification dispatch ...

  5. C# List集合转Json字符串示例代码

    将list集合转换为Json字符串简单实现代码: public static string GetJosn(List<CalendarInfo> list) { string jsonSt ...

  6. HUD 2846 Repository

    /* 开始想耍小聪明 直接map搞 代码短 好理解 空间够 恩 很好 就是 map慢死了 T了 */ #include<iostream> #include<cstdio> # ...

  7. 打jar包的方法

    打jar包的方法是什么? java打jar包,引用其他.jar文件 java项目打jar包 将java源码打成jar包 maven打jar例子 打war包的方法是什么? Eclipse->项目右 ...

  8. 解决ajax请求cors跨域问题

    ”已阻止跨源请求:同源策略禁止读取位于 ***** 的远程资源.(原因:CORS 头缺少 'Access-Control-Allow-Origin').“ ”已阻止跨源请求:同源策略禁止读取位于 ** ...

  9. 2.Android Studio系列教程2——基本设置与运行

    原文链接:http://stormzhang.com/devtools/2014/11/28/android-studio-tutorial2/   一.项目结构   二.Android Studio ...

  10. 用MS自带的VS构建joint语句

    在其中一个表上,右键,选择"New Query",弹出"Add Table"对话框,将待joint的两个表Add,并选择相应字段,则会自动构建joint语句,其 ...