Hdu 4135 co-prime +hdu 2841 Visible Trees (Tolerant principle)

Source: Internet
Author: User
Tags greatest common divisor

Co-PrimeTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2263 Accepted Submission (s): 847


Problem Descriptiongiven A number N, you is asked to count the number of integers between a and B inclusive which is rel Atively Prime to N.
Integers is said to be co-prime or relatively prime if they has no common positive divisors other than 1 or, equival Ently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Inputthe first line on input contains T (0 < T <=) the number of test cases, each of the next T lines contains t Hree integers A, B, N where (1 <= A <= B <= 1015) and (1 <=n <= 109).
Outputfor each test case, print the number of integers between A and B inclusive which is relatively prime to N. Follow T He output format below.
Sample Input
21 10 23) 15 5

Sample Output
Case #1:5Case #2:Hint

Source

The third Lebanese collegiate programming Contest


The main idea: in the interval [A, a] to calculate the number of the number of N-reciprocity.

Idea: Because the direct calculation of mutual trouble, so you can first calculate the non-mutual, and then the total number minus the non-reciprocity, the rest is the answer. For the number of Xn, we can first calculate the mass factor of N, and then in [A, a] to look for multiples of xn, this is the number of non-reciprocal factors. But it is wrong to subtract directly, because there is a repetition in the middle.

For example: If the mass factor of N is 2 and 3, you can find a multiple of 6 for 2 and 3 in the [A, b], so there are 2, in fact one is enough.

Here is to use the principle of repulsion: in the above process to be 6 of the multiple removed.


#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 descriptionthere is many trees forming a m * n grid, the grid starts from (in). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.

If trees and Sherlock is in one line, Farmer Sherlock can only see the tree nearest to him.
Inputthe first line contains one integer t, represents the number of test cases. Then there is multiple test cases. For all test case there are one line containing the integers m and n (1≤m, n≤100000)
Outputfor each test case output one line represents the number of trees Farmer Sherlock can see.
Sample Input
21 12 3

Sample Output
15

Source2009 multi-university Training Contest 3-host by WHU

This question is the same as above, just to preprocess it first, otherwise it will be 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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Hdu 4135 co-prime +hdu 2841 Visible Trees (Tolerant principle)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.