zoukankan      html  css  js  c++  java
  • Hdu 4407 Sum

    Sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 897    Accepted Submission(s): 259
    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
     
    
    
    Source
     
    
    
    Recommend
    zhoujiaqi2010
    参考思路
    http://www.cnblogs.com/Lyush/archive/2012/09/22/2698448.html

    #include <iostream> #include <map> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <algorithm> using namespace std; bool h[700]; int p[200]; void prime() { int cnt=1; int i,j; p[0]=2; for(i=2;i<=700;i+=2) h[i]=1; for(i=3;i<=700;i+=2) if(!h[i]) { p[cnt++]=i; for(j=i*i;j<=700;j+=i) h[j]=1; } } int gcd(int a,int b) { int r; while(r=a%b){a=b;b=r;} return b; } __int64 solve(__int64 n,__int64 pp) { if(n==0) return 0; int i,j; int rc[10],cnt=0; int m=(int)sqrt(pp*1.0); for(i=0;p[i]<=m;i++) { if(pp%p[i]==0) rc[cnt++]=p[i]; while(pp%p[i]==0)pp=pp/p[i]; if(pp==1) break; } if(pp>1) rc[cnt++]=pp; int w=(1<<cnt); int js; __int64 k=0; __int64 ans=0,cj; for(i=1;i<w;i++)//关键点、将容斥原理的关系转换成二进制、这个还是第一次接触、纪念下 { cj=1; for(js=j=0;j<cnt;j++) if(i&(1<<j)) { js++; cj*=rc[j]; } k=n/cj; if(js&1) ans+=((cj+cj*k)*k)/2; else ans-=((cj+cj*k)*k)/2; } return ans; } map <int,int> mp; map <int,int>::iterator it; int main() { prime(); int T; int n,m; __int64 op,x,y,p; __int64 sum; scanf("%d",&T); while(T--) { scanf("%d %d",&n,&m); while(m--) { scanf("%I64d",&op); if(op==1) { scanf("%I64d %I64d %I64d",&x,&y,&p); sum=(y-x+1)*(x+y)>>1; for(it=mp.begin();it!=mp.end();it++) 此题关键点是m很小、才1000 if(it->first>=x) { if(it->first>y) break; sum-=gcd(it->first,p)==1?it->first:0; sum+=gcd(it->second,p)==1?it->second:0; } sum-=(solve(y,p)-solve(x-1,p)); printf("%I64d\n",sum); } else { scanf("%I64d %I64d",&x,&y); mp[x]=y; } } mp.clear(); } return 0; }
  • 相关阅读:
    oracle表连接------&gt;排序合并连接(Merge Sort Join)
    内存损坏问题的演示样例及分析
    HTML5 Canvas中9宫格的坑
    hive udaf 用maven打包运行create temporary function 时报错
    Re-installation failed due to different application signatures.
    UVA 6480 Zombie Invasion(模拟退火)
    POJ3436 ACM Computer Factory 【最大流】
    android图像处理系列之三--图片色调饱和度、色相、亮度处理
    android图像处理系列之四--给图片添加边框(上)
    android图像处理系列之五--给图片添加边框(中)
  • 原文地址:https://www.cnblogs.com/372465774y/p/2731497.html
Copyright © 2011-2022 走看看