see

Description
问从点(0,0)能看到点(0,0)和(n,n)之间的矩形的多少个整数点,看到(x,y)代表点(0,0)和点(x,y)间没有其他整数点,如看不到(2,4)因为中间有点(1,2)
Input
一行一个正整数n
Output
一行一个数表示能看到多少个点
Sample Input
2
Sample Output
5
HINT
样例解释:能看到(0,1)(1,0)(1,1)(2,1)(1,2)5个点

数据范围:

对于20%的数据n<=1000
对于50%的数据n<=100000
对于100%的数据n<=10000000


非常典型的欧拉函数的应用.
不说话, 直接上代码(欧拉函数这种东西要背熟啊)

#include<iostream>
#include<string.h>
using namespace std;
/*
void phi_table() 
{  
     memset(phi,0,sizeof(phi));  
     phi[1] = 1;  
     for(int i = 2; i <= N; i++)  
          if(!phi[i])  
              for(int j = i; j <= N; j += i)  
              {  
                      if(!phi[j])  
                         phi[j] = j;  
                      phi[j] = phi[j] / i * (i - 1);             
              }     
}
*/
const int maxN = (int)1e7;
int phi[maxN + 1];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("see.in", "r", stdin);
    freopen("see.out", "w", stdout);
    #endif
    int n;
    cin >> n;
    memset(phi, 0, sizeof(phi));
    phi[1] = 1;
    for(int i = 2; i <= n; i ++)
        if(! phi[i])
            for(int j = i; j <= n; j += i)
            {
                if(! phi[j])
                    phi[j] = j;
                phi[j] = phi[j] / i * (i - 1);
            }
    long long ans = 0;
    for(int i = 1; i <= n; i ++)
        ans += phi[i];
    cout << ans * 2 + 1;
}
原文地址:https://www.cnblogs.com/ZeonfaiHo/p/6402858.html