码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-06 20:35:27      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:hdu   c语言   容斥原理   

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 <= 1015) and (1 <=N <= 109).
 

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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:hdu   c语言   容斥原理   

原文地址:http://blog.csdn.net/aaaaacmer/article/details/47319911

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!