zoukankan      html  css  js  c++  java
  • poj3090 Visible Lattice Points [欧拉函数]

    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

    一个点(x,y)能被射中的充分必要条件是x,y互质,是不是很像欧拉函数?
    经过容斥可以得到,若用sum表示1...n的phi值,ans=sum*2+2-1=sum*2+1
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<iostream>
     4 using namespace std;
     5 #define dbg(x) cout<<#x<<" = "<<x<<endl
     6 
     7 const int maxn=1005;
     8 
     9 bool check[maxn];
    10 int prime[maxn],phi[maxn],n,dat[maxn],phisum[maxn];
    11 int ans=0,cnt=0,T;
    12 
    13 void getphi(){
    14     phi[1]=1;
    15     for(int i=2;i<=n;i++){
    16         if(!check[i]){
    17             prime[++cnt]=i;
    18             phi[i]=i-1;
    19         }
    20         for(int j=1;j<=cnt&&prime[j]*i<=n;j++){
    21             check[i*prime[j]]=1;
    22             if(i%prime[j])  phi[i*prime[j]]=phi[i]*(prime[j]-1);
    23             else{
    24                 phi[i*prime[j]]=phi[i]*prime[j];
    25                 break;
    26             }
    27         }
    28     }
    29     for(int i=1;i<=n;i++)  phisum[i]=phisum[i-1]+phi[i];
    30 }
    31 
    32 int main(){
    33     scanf("%d",&T);
    34     for(int i=1;i<=T;i++){
    35         scanf("%d",&dat[i]);
    36         n=max(dat[i],n);
    37     }
    38     getphi();
    39     for(int i=1;i<=T;i++){
    40         printf("%d %d %d
    ",i,dat[i],(phisum[dat[i]]<<1)+1);
    41     }
    42     return 0;
    43 }
  • 相关阅读:
    前端知识之HTML内容
    数据库之表操作
    初识数据库
    Python连接MySQL数据库之pymysql模块使用
    并发编程之协程
    个人库
    Apache conf配置文件 allow deny order files directory location解释,re(正则表达式)入门速成
    Arduino在vscode中输出乱码解决方案及解释
    UOJ Judgement Failed惨痛教训
    vscode导出插件列表
  • 原文地址:https://www.cnblogs.com/ZYBGMZL/p/7271650.html
Copyright © 2011-2022 走看看