zoukankan      html  css  js  c++  java
  • 数论

    Visible Lattice Points
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5636   Accepted: 3317

    Description

    A lattice point (xy) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (xy) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (xy) with 0 ≤ xy ≤ 5 with lines from the origin to the visible points.

    Write a program which, given a value for the size, N, computes the number of visible points (xy) with 0 ≤ xy ≤ N.

    Input

    The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.

    Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.

    Output

    For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.

    Sample Input

    4
    2
    4
    5
    231

    Sample Output

    1 2 5
    2 4 13
    3 5 21
    4 231 32549

    Source

     

     

    Mean: 

     在第一象限中,输入一个n,然后要你统计在(0<=x<=n,0<=y<=n)的范围内,有多少可视点。

    所谓的可视点,即:从(0,0)出发到达(x1,y1),中间未与任何整点相交的点。

    analyse:

     通过分析,我们会发现:只要x和y互质,那么(x,y)就是可视点。我们只要求得[0,0]~[x,y]内满足x和y互质的点(x,y)的个数,那么问题就可迎刃而解。欧拉函数就是用来解决小于n的数中有多少个数与n互质。

    Time complexity:O(n)

    Source code:

    // Memory   Time
    // 1347K     0MS
    // by : Snarl_jsb
    // 2014-09-12-22.35
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    #include<string>
    #include<climits>
    #include<cmath>
    #define N 1000010
    #define LL long long
    using namespace std;
    
    int gcd(int a,int b){
    	return b?gcd(b,a%b):a;
    }
    
    inline int lcm(int a,int b){
    	return a/gcd(a,b)*b;
    }
    
    int eular(int n)    ////求1..n-1中与n互质的数的个数
    {
    	int ret=1,i;
    	for (i=2;i*i<=n;i++)
    		if (n%i==0){
    			n/=i,ret*=i-1;
    			while (n%i==0)
    				n/=i,ret*=i;
    		}
    	if (n>1)
    		ret*=n-1;
    	return ret;
    }
    
    int main()
    {
    //    freopen("C:\Users\ASUS\Desktop\cin.cpp","r",stdin);
    //    freopen("C:\Users\ASUS\Desktop\cout.cpp","w",stdout);
        int t,Cas=1;
        cin>>t;
        while(t--)
        {
            int n;
            cin>>n;
            LL ans=0;
            for(int i=1;i<=n;i++)
            {
                ans+=eular(i);
            }
            printf("%d %d %d
    ",Cas++,n,ans*2+1);
        }
        return 0;
    }
     
    

      

  • 相关阅读:
    与人相处的二十四条黄金法则(转载)
    User.UserType: NoLogin 不是SQL Server 2005 的有效选项 的解决方案
    腾讯QQ的验证码,很黄很暴力
    具有Ajax自动建议功能的TextBox控件(附源码)
    30+英文电子书免费下载网站,跟喜欢看英文电子书的朋友分享
    Ajax实现多任务指示器(附源码)
    批量替换数据库记录内容的一条SQL语句
    MAC地址为FFFFFFFFFFFF,接受数据包为0,局域网电脑不能上网的解决办法
    不小了,该谈朋友结婚了,年前能实现吗?
    WordPress中J.parentNode’为空或不是对象的解决方法
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3969260.html
Copyright © 2011-2022 走看看