poj3090欧拉函数求和

E - (例题)欧拉函数求和

Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

A lattice point (x, y) 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 (x, y) 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 (x, y) with 0 ≤ x, y ≤ 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 (x, y) with 0 ≤ x, yN.

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题目大意:给你一个n*n的网格,任意一点和(0,0)连线,可以组成一条直线,前面的点可以挡住后面的点,问你能看到的点到底有多少个思路分析:题目实际上就是问在这个网格上有多少种不同的斜率,边上的两点我们先不管,然后将整个正方形分成上三角和下三角两部分,由对称性,两边可以看到的点的数目肯定一样多,以下三角为例进行研究,我们会发现,对于所有能看到的点,他们有着一个共同的特征,那就是gcd(x,y)=1,若不为1,则他前面肯定有一个点挡住了这个点,那么本题就转变成了一个求欧拉函数和的简单题目,注意不要将分界线上的点加,记t=phi[1]+phi[2]+.......+phi[n],则ans=(t-1)*2+1+2=2*t+1代码:

#include<iostream>
#include<cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=1500;
int prime[maxn];
int phi[maxn];
bool check[maxn];
int tot;
void make_prime()
{
    phi[1]=1;
    memset(check,true,sizeof(check));
    tot=0;
    for(int i=2;i<=maxn;i++)
    {
        if(check[i])
        {
            prime[tot++]=i;
            phi[i]=i-1;
        }
        for(int j=0;j<tot&&i*prime[j]<=maxn;j++)
        {
            check[i*prime[j]]=false;
            if(i%prime[j]==0)
            {
                phi[i*prime[j]]=phi[i]*prime[j];
                break;
            }
            else phi[i*prime[j]]=phi[i]*(prime[j]-1);
        }
    }
}
int kase;
int main()
{
    int T;
     make_prime();
    scanf("%d",&T);
    kase=0;
    ll num;
    while(T--)
    {
        int n;
        scanf("%d",&n);
        ll ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=phi[i];
        }
        printf("%d %d %lld\n",++kase,n,ans*2+1);
    }
}

时间: 2024-12-09 23:07:03

poj3090欧拉函数求和的相关文章

NYOJ 570 欧拉函数求和【欧拉函数求和】

我只想说数据弱爆了,这也可以过 欧拉函数求和 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 题目描述很简单,求出 (PS:上面式子的意思是大于0小于n并且能整除n的所有d的欧拉函数值之和). 输入 每行一个数n(n<2^31),输入以文件结尾结束. 输出 每个结果占一行. 样例输入 1 2 12 样例输出 0 1 8 来源 rihkddd原创 上传者 rihkddd #include<stdio.h> int euler(int n) { int ret=

【BZOJ4805】欧拉函数求和(杜教筛)

[BZOJ4805]欧拉函数求和(杜教筛) 题面 BZOJ 题解 好久没写过了 正好看见了顺手切一下 令\[S(n)=\sum_{i=1}^n\varphi(i)\] 设存在的某个积性函数\(g(x)\) \[(g*\varphi)(i)=\sum_{d|i}g(d)\varphi(\frac{i}{d})\] \[\sum_{i=1}^n(g*\varphi(i))(i)\] \[=\sum_{i=1}^n\sum_{d|i}g(d)\varphi(\frac{i}{d})\] \[=\sum

BZOJ4805: 欧拉函数求和(杜教筛)

4805: 欧拉函数求和 Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 614  Solved: 342[Submit][Status][Discuss] Description 给出一个数字N,求sigma(phi(i)),1<=i<=N Input 正整数N.N<=2*10^9 Output 输出答案. Sample Input 10 Sample Output 32 HINT Source By FancyCoder 直接大力杜教筛

欧拉函数求和 解题报告

对正整数n,欧拉函数是小于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命名,它又称为Euler's totient function.φ函数.欧拉商数等.例如:φ(8) = 4(Phi(8) = 4),因为1,3,5,7均和8互质. S(n) = Phi(1) + Phi(2) + ...... Phi(n),给出n,求S(n),例如:n = 5,S(n) = 1 + 1 + 2 + 2 + 4 = 10,定义Phi(1) = 1.由于结果很大,输出Mod 1000000007的结

【BZOJ3944/4805】Sum/欧拉函数求和 杜教筛

[BZOJ3944]Sum Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1,ans2 Sample Input 6 1 2 8 13 30 2333 Sample Output 1 1 2 0 22 -2 58 -3 278 -3 1655470 2 题解: 粘自http://blog.csdn.net/skywalkert/article/details/

BZOJ 4805: 欧拉函数求和

Description 求\(\sum_{i=1}^n\varphi(i),n\leqslant 2\times 10^9\) Solution 杜教筛... 见上篇... Code /************************************************************** Problem: 4805 User: BeiYu Language: C++ Result: Accepted Time:1100 ms Memory:48172 kb ********

Bzoj4805: 欧拉函数求和

好久没写杜教筛了 练练手AC量刷起 # include <bits/stdc++.h> # define RG register # define IL inline # define Fill(a, b) memset(a, b, sizeof(a)) using namespace std; typedef long long ll; const int _(1e7 + 1); IL int Input(){ RG int x = 0, z = 1; RG char c = getchar

算法复习——欧拉函数(poj3090)

题目: Description A lattice point (x, y) 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 (x, y) does not pass through any other lattice point. For exa

POJ3090(SummerTrainingDay04-M 欧拉函数)

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7450   Accepted: 4536 Description A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible fr