How hdu 1695 hdu 4135 is used to calculate the number of mutual quality with r in the 1-n range.

Source: Internet
Author: User

Method:

First, the prime factor of n is decomposed, and each prime factor is recorded separately. Then the number of non-interconnectivity with a certain prime factor in the obtained range is n/r (I). Assume that r (I) is a qualitative factor of r.

Assuming there are only three quality factors, the total number of non-interphysical should be p1 + p2 + p3-p1 * P2-P1 * p3-p2 * p3 + p1 * p2 * p3, and the principle of rejection, you can go to Baidu encyclopedia to view related content

Pi represents n/r (I), that is, the number of numbers that do not interact with a certain quality factor.

When there are more quality factors, they can be solved by State compression. If the binary bit is 1, this quality factor is obtained. If there is an odd number of 1, it is added, and vice versa.

View Code

#include<vector>
#include<cstdio>
using namespace std;
__int64 solve(int r,__int64 n){
vector<int> p;
int i;
for(i=2;i*i<=r;i++){
if(r%i==0){
p.push_back(i);
while(r%i==0){
r/=i;
}
}
}
if(r>1) p.push_back(r);
__int64 sum=0;
for(int num=1;num<(1<<p.size());num++){
__int64 mult=1,ones=0;
for(i=0;i<p.size();i++){
if(num&(1<<i)){
ones++;
mult*=p[i];
}
}
if(ones%2) sum+=n/mult;
else sum-=n/mult;
}
return n-sum;
}
int main(){
int t,c;
__int64 a,b;
__int64 cases=1;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d%d",&a,&b,&c);
printf("Case #%I64d: %I64d\n",cases++,solve(c,b)-solve(c,a-1));
}
return 0;
}

Hdu 1695

View Code

#include<vector>#include<cstdio>using namespace std;int prm[50000];int tot=0;bool flag[100010];void init(){    int i,j;    memset(flag,false,sizeof(flag));    for(i=2;i<=100000;i++)    {        for(j=2*i;j<=100000;j+=i)        {            flag[j]=true;        }    }    for(i=2;i<=100000;i++)    {        if(!flag[i])            prm[++tot]=i;    }}__int64 solve(int r,__int64 n){    int p[50];    int cnt=0;    int i;    if(!flag[r])    {        if(r>1)            p[cnt++]=r;    }    else     {        for(i=1;i<=tot;i++)        {            if(r%prm[i]==0)            {                p[cnt++]=prm[i];                while(r%prm[i]==0)        r/=prm[i];                if(!flag[r])                {                    if(r>1)                    {                        p[cnt++]=r;r=1;                    }                    break;                }            }        }if(r>1) p[cnt++]=r;    }    __int64 sum=0;    for(int num=1;num<(1<<cnt);num++){        __int64 mult=1,ones=0;        for(i=0;i<cnt;i++){            if(num&(1<<i)){                ones++;                mult*=p[i];            }        }        if(ones%2) sum+=n/mult;        else sum-=n/mult;    }    return n-sum;}int main(){    int t;    init();    __int64 a,b,c,d,e;    __int64 cases=1;    scanf("%d",&t);    while(t--){        scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&e);        if(e==0||b<e||d<e)         {            printf("Case %I64d: 0\n",cases++);            continue;        }         a = b < d ? b : d;                 b = b > d ? b : d;                 a /= e;                 b /= e;        __int64 ans=0;        for(int i=1;i<=a;i++)        {            if(i>1)            ans+=solve(i,b)-solve(i,i-1);            else ans+=b;        }        printf("Case %I64d: %I64d\n",cases++,ans);    }    return 0;}

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.