UVA 10820 Send a Table euler_phi功能

除1,1其他外国x,y不等于

为 x<y 案件 一切y有phi(y)组合

F[x]= phi(i) 2<=i<=x

结果为 2*F[x]+1

Problem A

Send a Table

Input: Standard Input

Output: Standard Output

When participating in programming contests, you sometimes face the following problem: You know how to calcutale the output for the given input values, but your algorithm is way too slow to ever pass the time limit. However hard you try, you just can‘t discover
the proper break-off conditions that would bring down the number of iterations to within acceptable limits.

Now if the range of input values is not too big, there is a way out of this. Let your PC rattle for half an hour and produce a table of answers for all possible input values, encode this table into a program, submit it to the judge, et voila: Accepted in
0.000 seconds! (Some would argue that this is cheating, but remember: In love and programming contests everything is permitted).

Faced with this problem during one programming contest, Jimmy decided to apply such a ‘technique‘. But however hard he tried, he wasn‘t able to squeeze all his pre-calculated values into a program small enough to pass the judge. The situation looked hopeless,
until he discovered the following property regarding the answers: the answers where calculated from two integers, but whenever the two input values had a common factor, the answer could be easily derived from the answer for which the input values were divided
by that factor. To put it in other words:

Say Jimmy had to calculate a function Answer(x, y) where x and y are both integers in the range [1, N].  When he knows Answer(x, y), he can easily derive Answer(k*x, k*y), where k is any integer from it by applying some simple calculations involving Answer(x,
y) and k. For example if N=4, he only needs to know the answers for 11 out of the 16 possible input value combinations: Answer(1, 1), Answer(1, 2), Answer(2, 1), Answer(1, 3), Answer(2, 3), Answer(3, 2), Answer(3, 1), Answer(1, 4), Answer(3, 4), Answer(4,
3) and Answer(4, 1). The other 5 can be derived from them (Answer(2, 2), Answer(3, 3) and Answer(4, 4) from Answer(1, 1), Answer(2, 4) from Answer(1, 2), and Answer(4, 2) from Answer(2, 1)). Note that the function Answer is not symmetric, so Answer(3, 2) can
not be derived from Answer(2, 3).

Now what we want you to do is: for any values of N from 1 upto and including 50000, give the number of function Jimmy has to pre-calculate.

Input

The input file contains at most 600 lines of inputs. Each line contains an integer less than 50001 which indicates the value of N. Input is terminated by a line which contains a zero. This line should not be processed.

Output

For each line of input produce one line of output. This line contains an integer  which indicates how many values Jimmy has to pre-calculate for a certain value of N.

Sample Input                               Output for Sample Input


2

5

0

         

3

19


Problem setter: Little Joey

Special Thanks: Shahriar Manzoor

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=55000;

long long int phi[maxn];

void phi_table()
{
    phi[1]=1LL;
    for(int i=2;i<maxn;i++)
    {
        if(!phi[i])
        {
            for(int j=i;j<maxn;j+=i)
            {
                if(!phi[j]) phi[j]=j;
                phi[j]=phi[j]/i*(i-1);
            }
        }
    }
    for(int i=3;i<maxn;i++)
    {
        phi[i]+=phi[i-1];
    }
}

int n;

int main()
{
    phi_table();
    while(scanf("%d",&n)!=EOF&&n)
    {
        if(n==1)
        {
            puts("1"); continue;
        }
        printf("%lld\n",1+phi[n]*2);
    }
    return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-10-05 05:05:01

UVA 10820 Send a Table euler_phi功能的相关文章

Uva 10820 Send a Table(欧拉函数)

对每个n,答案就是(phi[2]+phi[3]+...+phi[n])*2+1,简单的欧拉函数应用. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<list> #i

UVA 10820 Send a Table 数论 欧拉函数

题目链接: https://vjudge.net/problem/UVA-10820 题目描述: 给你一个N, N <= 50000, 让你寻找N之内互素数的个数 解题思路: 欧拉函数, 由于位置颠倒是两个解, 在小于N的范围内只有(1, 1)x, y相等, 其他的都是不等的, 所以我们只需要算phi(2) + phi(3) + ...... phi(n), 然后 * 2 + 1即可 代码: #include <iostream> #include <cstdio> #inc

UVA 10820 Send a Table

https://vjudge.net/problem/UVA-10820 题意: 有一张表 f[x][y],共x*y个数 现在要删去所有的 f[x*k][y*k] ,k>1 最后还剩多少个数 题意转化:有多少个二元组(x,y)满足 gcd(x,y)=1 若x<y,则 f[][y]=phi(y) 所以 ans= (2* Σ phi(y))+1   y∈[2,n] 加的1为(1,1) 线性筛出欧拉函数求和即可 #include<cstdio> #define N 50001 using

UVa 10820 (打表、欧拉函数) Send a Table

题意: 题目背景略去,将这道题很容易转化为,给出n求,n以内的有序数对(x, y)互素的对数. 分析: 问题还可以继续转化. 根据对称性,我们可以假设x<y,当x=y时,满足条件的只有(1, 1). 设f(n)为 集合S{(x, y) | x<y且x.y互素} 的个数,则所求答案为2f(n)+1 f(n)表达式为: ,其中φ(n)为欧拉函数 这里有欧拉函数的一些介绍 1 #include <cstdio> 2 3 const int maxn = 50000; 4 5 int ph

D - Send a Table (UVA - 10820)

- 题目大意 给出一个n,求从1~n中任意两个数互质的对数为多少,(a,b)和(b,a)算两对. - 解题思路 构造一个欧拉函数的方法,然后用一个数组去存储下标,因为(a,b)和(b,a)算两对,所以每一个都加两倍,但(1,1)很特殊,所以最后减一就行了. - 代码 #include<iostream> #include<cmath> #include<cstring> using namespace std; const int MAX = 600000; int p

jquery插件方式实现table查询功能

1.写插件部分,如下: ;(function($){ $.fn.plugin = function(options){ var defaults = { //各种属性,各种参数 } var options = $.extend(defaults, options); this.each(function(){ //功能代码 var _this = this; }); } })(jQuery); 附上一个例子: 1 ;(function($){ 2 $.fn.table = function(op

uva 11440 - Help Tomisu(欧拉功能)

题目链接:uva 11440 - Help Tomisu 题目大意:给定n和m,求从2~n.中的数x.要求x的质因子均大于m.问说x有多少个.答案模上1e9+7. 解题思路: (1)n!=k?m!(n≥m) (2) 假设有gcd(x,T)=1,那么gcd(x+T,T)=gcd(x,T)=1 题目要求说x的质因子必需要大于m,也就是说x不能包括2~m的因子,那么gcd(x,m!)=1,于是我们求出?(m!),小于m! 而且满足gcd(x,m!)=1的个数. 那么依据(2)可得从[m!+1, 2*m

uva 10820

欧拉函数..... #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int maxn=50000+10; int pp[maxn]; int sum[maxn]; int n; void ola() { memset(pp,0,sizeof(pp)); memset(sum,0,sizeof(sum

UVA10820 send a table

题意:对(x,y),1<=x,y<=n 求出有多少对(x,y)互质 题解:筛法跑一遍欧拉就好了 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; ll phi[50001]; void init() { for(int i=1;i<=50000;i++