【数论·欧拉函数】SDOI2008仪仗队

题目描述

作为体育委员,C君负责这次运动会仪仗队的训练。仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如右图)。 现在,C君希望你告诉他队伍整齐时能看到的学生人数。

输入输出格式

输入格式:

共一个数N

输出格式:

共一个数,即C君应看到的学生人数。

输入输出样例

输入样例#1:

4

输出样例#1:

9

说明

【数据规模和约定】

对于 100% 的数据,1 ≤ N ≤ 40000

题解

首先,我们很容易发现,所有能看到的点都满足一点:

它的横纵坐标互质(C君在(0,0))

所以显然能看到的点的个数就是1~n-1的欧拉函数之和乘二加一

代码如下:

#include<iostream>
#include<cstdio>
using namespace std;

int n;
long long ans;
int phi[40005];

int main()
{
    scanf("%d",&n);
    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);         //等同于phi[j]=phi[j]*(i-1)/i
                                               //即为 phi[j]=j*(1-1/k1)(1-1/k2)....
            }
    }
    for(int i=1;i<n;++i)
        ans+=phi[i];
    printf("%lld",ans*2+1);
}
时间: 2024-08-02 02:51:39

【数论·欧拉函数】SDOI2008仪仗队的相关文章

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

欧拉函数性质与求法 [数论][欧拉函数]

n的欧拉函数值用符号φ(n)表示 欧拉函数的定义是,对于一个正整数n,小于n且与n互质的数的数目(包括1,特殊地,φ(1)=1 ). 设p1,p2,p3,...,pr为n的全部r个质因数,则有φ(n)=n*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pr). 显然,用这个方法来计算单个欧拉函数是可以求解的. 附上代码: 1 int get_phi(int x){ 2 int re=x; 3 for(int i=2;i*i<=x;i++) 4 if(x%i

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7693   Accepted: 2522 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

hdu1395 数论 欧拉函数

hdu1395 数论   欧拉函数对于给出的每一个n 求最小正整数 x 满足 2^x mod n = 1 1.如果给出的n 是偶数或者 1 则一定无解2.如果是奇数 首先根据欧拉定理 我们可知 phi(n)一定是满足要求的 然后答案一定是 phi( i ) 的因数 然后我们就可以 O(sqrt(phi(i))的时间内 枚举每个因数 然后快速幂验证就行了 1 #include <bits/stdc++.h> 2 using namespace std ; 3 4 const double eps

数论-欧拉函数

题目1 : 数论五·欧拉函数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho有时候会用密码写信来互相联系,他们用了一个很大的数当做密钥.小Hi和小Ho约定了一个区间[L,R],每次小Hi和小Ho会选择其中的一个数作为密钥. 小Hi:小Ho,这次我们选[L,R]中的一个数K. 小Ho:恩,小Hi,这个K是多少啊? 小Hi:这个K嘛,不如这一次小Ho你自己想办法算一算怎么样?我这次选择的K满足这样一个条件: 假设φ(n)表示1..n-1中与n互质的数的个

数论&#183;欧拉函数

欧拉函数$phi(n)$表示不超过$n$的正整数中与$n$互质的个数,并且有: $\varphi(n)= n\sum\limits_{p|n}(1-{\frac 1{p}})$ 显然有若$n$素数: $\varphi(n)=n-1$ 并且考虑$mp$,若$p$为素数,则对任意整数$k$: $(mp, k)\Leftrightarrow (m, k)$ 于是在每个模$p$的剩余系中有$\varphi(m)$个数与$mp$互质,因此: $\varphi(mp)=\varphi(m)\varphi(p

HDU1695-GCD(数论-欧拉函数-容斥)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5454    Accepted Submission(s): 1957 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y

数论 - 欧拉函数的运用 --- poj 3090 : Visible Lattice Points

Visible Lattice Points Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5636   Accepted: 3317 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

数论 - 欧拉函数模板题 --- poj 2407 : Relatives

Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Description Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if ther