zoukankan      html  css  js  c++  java
  • Visible Trees

    Visible Trees

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

    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
    分析:由题可知只有gcd(x,y)=1才能被看见;
       所以枚举其中一维坐标,容斥求出与之互质的另一维坐标即可;
       预处理每个数的素因子;
       或使用莫比乌斯函数,也是容斥求解;
    代码1:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline void umax(ll &p,ll q){if(p<q)p=q;}
    inline void umin(ll &p,ll q){if(p>q)p=q;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t;
    bool vis[maxn];
    vi fac[maxn];
    void init()
    {
        for(int i=2;i<=maxn-10;i++)
        {
            if(vis[i])continue;
            for(int j=i;j<=maxn-10;j+=i)
            {
                vis[j]=true;
                fac[j].pb(i);
            }
        }
    }
    int gao(int x,int y)
    {
        int ret=0,num=fac[x].size();
        for(int i=1;i<(1<<num);i++)
        {
            int now=1,cnt=0;
            for(int j=0;j<num;j++)
            {
                if(i&(1<<j))
                {
                    cnt++;
                    now*=fac[x][j];
                }
            }
            if(cnt&1)ret+=y/now;
            else ret-=y/now;
        }
        return y-ret;
    }
    int main()
    {
        int i,j;
        init();
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&m,&n);
            ll ret=0;
            rep(i,1,m)
            {
                ret+=gao(i,n);
            }
            printf("%lld
    ",ret);
        }
        return 0;
    }
    代码2:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    using namespace std;
    inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline void umax(ll &p,ll q){if(p<q)p=q;}
    inline void umin(ll &p,ll q){if(p>q)p=q;}
    inline ll read()
    {
        ll x=0;int f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    int n,m,k,t,cnt,fac[maxn],ok[maxn];
    int x,y;
    bool vis[maxn];
    void init()
    {
        for(int i=2;i<=maxn-10;i++)
        {
            if(vis[i])continue;
            for(int j=i;j<=maxn-10;j+=i)
            {
                vis[j]=true;
                ++fac[j];
                if(j%((ll)i*i)==0)ok[j]=1;
            }
        }
    }
    int main()
    {
        int i,j;
        init();
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&x,&y);
            ll ret=(ll)x*y;
            rep(i,2,maxn-10)
            {
                if(i>x||i>y)break;
                if(ok[i])continue;
                ret+=(ll)(fac[i]&1?-1:1)*(x/i)*(y/i);
            }
            printf("%lld
    ",ret);
        }
        return 0;
    }
  • 相关阅读:
    tomcat 启动 报错Neither the JAVA_HOME nor the JRE_HOME environment variable is definedtemp
    tomcat linux 加入服务自动启动
    registered the JDBC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
    tomcat 8 加 struts2的 java.lang.NoSuchFieldException: resourceEntries
    tomcat Can't create cache file!
    tomcat 部署时修改服务器时间
    tomcat java变量环境设置
    scrapy 动态IP、随机UA、验证码
    scrapy xpath、正则表达式、css选择器
    Saltstack windows可视化操作(十四)
  • 原文地址:https://www.cnblogs.com/dyzll/p/6360078.html
Copyright © 2011-2022 走看看