zoukankan      html  css  js  c++  java
  • hdu 4407 (12年 金华) 容斥原理 + map +筛选质因子

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4407

    Sum

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1714    Accepted Submission(s): 478


    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
    分析:
    1:先求出 R- L 区间内与p 互质的数之和, 然后再对修改,处理一下就可以。
    2:求区间[1, n] 与 p 互质的 数之和, 用 容斥原理。
    3: 用map 保存一下 操作2, 就可以。
    代码如下:
    #include<iostream>
    #include<stdlib.h>
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<string>
    #include<queue>
    #include<algorithm>
    #include<map>
    #define N 400010
    using namespace std;
    typedef long long LL;
    map<int , int >mp;
    map<int , int>::iterator it;
    int p[N][15]={0}; // 二维记录p二维质因子
    int num[N]={0};  // 记录P二维质因子的个数
    int IsNotPrime[N]={0};
    LL ans;
    
    // 筛选得到 质因子
    void init()
    {
        int i,j;
        for(i=2 ; i<N ;i++)
        {
            if(!IsNotPrime[i])
            {
                for(j=i ; j<N; j+=i)
                {
                    IsNotPrime[j]=1;
                    p[j][num[j]++]=i;
                }
            }
        }
    }
    // 容斥原理,值为q , 当前点(质因子的下标号),树深,
    void dfs(int q, int now , int count , LL lcm ,int n)  // [1,n]与q 不互质的数之和
    {
        if(count >1)
            lcm=p[q][now]*lcm;
        int k=n/lcm;
        if(count&1)
        {
            ans-=(LL)lcm* k*(k+1)/2;   // 注意一定要强制转换, 因为这个原因ws好几次
        }
        else ans+= (LL)lcm* k*(k+1)/2;
        for(int i=now +1 ; i<num[q] ; i++)
            dfs(q, i , count+1 , lcm , n);
    }
    LL solve(int n , int q) // 求[1,n]与p不互质的数和
    {
        if(n<=0) return 0;
        ans= (LL)n*(n+1)/2;
        for(int i=0 ;i<num[q] ; i++)
            dfs(q,i,1,p[q][i],n);
        return ans;
    }
    // 容斥原理
    int gcd(int a,  int b)
    {
        return b==0?a:gcd(b,a%b);
    }
    
    int main()
    {
        int t,n,q,k,x,y,c;
        LL ret;
        scanf("%d",&t);
        init();
        while(t--)
        {
            scanf("%d%d",&n,&q);
            mp.clear();
            while(q--)
            {
                scanf("%d",&k);
                if(k==1)
                {
                    scanf("%d%d%d",&x,&y,&c);
                    ret=solve(y,c)-solve(x-1,c);
                    for(it=mp.begin(); it!=mp.end(); it++)
                    {
                        if((*it).first>=x && (*it).first<=y)
                        {
                            if(gcd(c, it->first) == 1) ret-=it->first;
                            if(gcd(c, it->second) ==1)  ret+=it->second;
                        }
                    }
                    printf("%I64d
    ",ret);
                }
                else
                {
                    scanf("%d%d",&x,&y);
                    mp[x]=y;
                }
            }
        }
        return 0;
    }
     
  • 相关阅读:
    操作系统第一天学习
    进制之间的转换
    git的使用
    Python 第二天学习(文件的处理)
    下载博客首页的博客列表
    获取所有的列表
    抓取指定博客的内容
    进程简介
    python 内置函数range和xrange
    关于read的例子和条件测试
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3606037.html
Copyright © 2011-2022 走看看