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

来源:互联网 发布:六小龄童 杨洁 知乎 编辑:程序博客网 时间:2024/06/05 12:43

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
21 10 23 15 5
 

Sample Output
Case #1: 5Case #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 __int64LL 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
21 12 3
 

Sample Output
15
 

Source
2009 Multi-University Training Contest 3 - Host by WHU

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

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<vector>#include<math.h>#define LL __int64using 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;}


0 0
原创粉丝点击