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;
    }
     
    

      

  • 相关阅读:
    MyWebViewDemo【封装Webview常用配置和选择文件、打开相机、录音、打开本地相册的用法】
    PhotoPickerNewDemo【PhotoPicker0.9.12的个性化修改以及使用(内部glide版本号是4.1.1)】
    PhotoPickerDemo【PhotoPicker0.9.8的个性化修改以及使用(内部glide版本号是3.7.0)】
    轻松理解MYSQL MVCC 实现机制
    MySQL-InnoDB-MVCC多版本并发控制
    Mysql中MVCC的使用及原理详解
    wmi的作用
    Java中BIO,NIO,AIO的理解
    Netty--索引
    Netty 入门示例
  • 原文地址:https://www.cnblogs.com/crazyacking/p/3969260.html
Copyright © 2011-2022 走看看