hdu 4135 Co-prime +hdu 2841 Visible Trees(容斥原理)

本文介绍两道算法题目:一是计算指定范围内与给定数互素的整数数量;二是计算从特定位置能看到的树木数量。通过数学原理如容斥原理及预处理技巧来解决这两类问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Co-prime

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


Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
 

Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 10 15) and (1 <=N <= 10 9).
 

Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
 

Sample Input
  
2 1 10 2 3 15 5
 

Sample Output
  
Case #1: 5 Case #2: 10
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
 

Source

The Third Lebanese Collegiate Programming Contest


题目大意:在区间[A,B]中计算出与N互素的数的数量。

思路:因为直接算互素有点麻烦,所以可以先算不互素的,然后总的数减去不互素的,剩下就是答案了。对于不互素的数求法,可以先算出N的质因数xn,然后在[A,B]中寻找xn的倍数,这就是不互素的数目了。但是直接减去是错误的,因为这中间会有重复。

举个例子:假如N的质因数有2和3,在[A,B]中寻找时可以找到6为2和3的倍数,这样就有2个了,实际上一个就够了。

这里就要用容斥原理了:在上述过程中要把为6的倍数去掉。


#include<stdio.h>
#include<string.h>
#include<math.h>
#define LL __int64
LL p[100];
LL dfs(LL n,LL b,LL x,LL k)
{
    LL i,j,ans=0;
    for(i=x;i<=k;i++)
    {
        ans+=b/p[i]-dfs(n,b/p[i],i+1,k);
    }
    return ans;
}
int main()
{
  int T,t;
  LL a,b,n,i,j,k,x,ans;
  scanf("%d",&T);
  t=0;
  while(T--)
  {
      t++;
      k=0;
      ans=0;
   scanf("%I64d%I64d%I64d",&a,&b,&n);
        x=n;
        for(i=2;i<=sqrt(n);i++)
        {
            if(x%i==0){
                while(x%i==0)x=x/i;
                p[++k]=i;
            }
        }
        if(x>1)p[++k]=x;
       
        ans=b-a+1-dfs(n,b,1,k)+dfs(n,a-1,1,k);
        printf("Case #%d: %I64d\n",t,ans);
  }    
  return 0;
}


Visible Trees

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


Problem Description
There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.

If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
 

Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)
 

Output
For each test case output one line represents the number of trees Farmer Sherlock can see.
 

Sample Input
  
2 1 1 2 3
 

Sample Output
  
1 5
 

Source

这题跟上面那题一样,只是要先预处理出来,否则会TLE。

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#define LL __int64
using namespace std;
int n,m;
struct node{
    LL num[105];
}p[100005];
vector<int>ps[100005];
int len[100005];
void initi(){
    LL i,j,x,k=0;

    for(i=1;i<=100000;i++)
    {
        x=i;k=0;
        for(j=2;j<=sqrt(i);j++)
        {
            if(x%j==0){
            while(x%j==0)
            x=x/j;
            ps[i].push_back(j);
            
        }
        }
        if(x>1)ps[i].push_back(x);
        
    }
}
LL dfs(LL s,LL b,LL x,LL k)
{
    LL ans=0;
    int i;
    for(i=x;i<k;i++)
    {
        ans+=b/ps[s][i]-dfs(s,b/ps[s][i],i+1,k);
    }
    return ans;
}
int main()
{
  int T,t;
  LL x,ans;
  int i,j,k;
  initi();
  scanf("%d",&T);
  t=0;
  
  while(T--)
  {
      t++;
      k=0;
      ans=0;
   scanf("%d%d",&n,&m);
 
    for(i=1;i<=m;i++)
    {
  //  printf("%I64d\n",len[i]);
        ans+=n-dfs(i,n,0,ps[i].size());
    }
        printf("%I64d\n",ans);
  }    
  return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值