UVa 10820 - Send a Table(欧拉函数)
链接:
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1761
题意:
有一道比赛题目,输入两个整数x、y(1≤x,y≤n),输出某个函数f(x,y)。
有位选手想交表(即事先计算出所有的f(x,y),写在源代码里),但表太大了,源代码超过了比赛限制,需要精简。
那道题目有一个性质,即很容易根据f(x,y)算出f(x*k, y*k)(k是任意正整数),这样有一些f(x,y)就不需要保存了。
输入n(n≤50000),你的任务是统计最简的表里有多少个元素。例如,n=2时有3个:(1,1), (1,2), (2,1)。
分析:
本题的本质是:输入n,有多少个二元组(x,y)满足:1≤x,y≤n,且x和y互素。
不难发现除了(1,1)之外,其他二元组(x,y)中的x和y都不相等。设满足x<y的二元组有f(n)个,那么答案就是2f(n)+1。
对照欧拉函数的定义,可以得到f(n)=phi(2)+phi(3)+…+phi(n)。
代码:
import java.io.*;
import java.util.*; public class Main {
static final int UP = 50000;
static int phi[] = new int[UP+5]; static void constant() {
for(int t = 2; t <= UP; t++) if(phi[t] == 0) {
for(int i = t; i <= UP; i += t) {
if(phi[i] == 0) phi[i] = i;
phi[i] = phi[i] / t * (t-1);
}
}
for(int i = 3; i <= UP; i++) phi[i] += phi[i-1];
} public static void main(String args[]) {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
constant(); while(true) {
int n = cin.nextInt();
if(n == 0) break;
System.out.println(2 * phi[n] + 1);
}
cin.close();
}
}
UVa 10820 - Send a Table(欧拉函数)的更多相关文章
- Uva 10820 Send a Table(欧拉函数)
对每个n,答案就是(phi[2]+phi[3]+...+phi[n])*2+1,简单的欧拉函数应用. #include<iostream> #include<cstdio> # ...
- UVa 10820 (打表、欧拉函数) Send a Table
题意: 题目背景略去,将这道题很容易转化为,给出n求,n以内的有序数对(x, y)互素的对数. 分析: 问题还可以继续转化. 根据对称性,我们可以假设x<y,当x=y时,满足条件的只有(1, 1 ...
- UVa10820 Send a Table[欧拉函数]
Send a TableInput: Standard Input Output: Standard Output When participating in programming contests ...
- uva 10820 (筛法构造欧拉函数)
send a table When participating in programming contests, you sometimes face the following problem: Y ...
- UVA 10820 - Send a Table 数论 (欧拉函数)
Send a Table Input: Standard Input Output: Standard Output When participating in programming contest ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...
- UVA 11426 GCD - Extreme (II) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
随机推荐
- MVVM 事件转命令1
EventToCommand 在WPF中,并不是所有控件都有Command,例如TextBox,那么当文本改变,我们需要处理一些逻辑,这些逻辑在ViewModel 中,没有Command如何绑定呢?这 ...
- A space or line break was encountered after the "@" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following
mvc 控制器调用分布视图出错,("A space or line break was encountered after the "@" character. Only ...
- 05.部分类 partial
namespace _06.部分类 { class Program { static void Main(string[] args) { } } /// <summary> /// 这个 ...
- MySQL 8.0 压缩包版安装方法
转自:https://blog.csdn.net/yangs_2012/article/details/80412016 注意: 操作系统:Windows 10 专业版(64位) MySQL版本:my ...
- 使用axis2调用webservice需要导入的依赖
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &l ...
- JSON 转 VO
需求 将获取的json数据直接转为vo 解决 利用net.sf.json.JSONObject的toBean() 确保json中的key值和vo中的字段名称一致 JSONObject jsonObje ...
- [转]Shared——Javascript中的call详解
call( ) 一.call的使用 call 方法第一个参数是作为函数上下文的对象,第二个参数是一个参数列表. var obj = { name: 'J' } function func(p1, p2 ...
- 关于div设置display: inline-block之后盒子之间间距的处理
当两个盒子都设置display: inline-block之后并且css也清除了默认样式 这时候会发现div盒子之间仍然存在间隙 将font-size清0间距就会取消
- mockito測試框架
1. code package com.springinaction.knights; import static org.mockito.Mockito.*; import org.junit.Te ...
- js获取当前日期和时间的代码
最佳答案 var myDate = new Date(); myDate.toLocaleDateString(): //获取当前日期myDate.toLocaleTimeString(); //获取 ...