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 ...
随机推荐
- 使用webpack2.0 搭建react.js项目
最近一段时间没有写react.js,发现webpack已经完全升级为2.0了,升级后导致以前的项目不能正常编译,只能重新实践一番 关于webpack2.0和1.x的区别概括起来就是tree shaki ...
- VS 同词高亮显示
Visual Studio 2010 选中高亮插件 - Highlight all occurrences of selected word Highlight all occurrences of ...
- Elasticsearch全文检索工具入门
Elasticsearch全文检索工具入门: 1.下载对应系统版本的文件 elasticsearch-2.4.0.zip 1.1运行elasticsearch-2.4.0\elasticsearch- ...
- flight学习笔记
Flight::db()-> getOne("select 1"); 返回结果:1 Flight::db()-> getRow ("select 1, 2 f ...
- 使用 WireShark 分析 TCP/IP 三次握手 和 四次挥手
TCP 三次握手 示意图 Wireshark 抓包注意事项 为了演示一个TCP三次握手建立连接的过程,我们通过 Chrome 访问一个网页. 已知 HTTP 协议就是建立在TCP链接上的 比如访问以下 ...
- flask之flask-login登陆验证(一)
这个模块能帮助我们做很多事,最常用到的是,登陆验证(验证当前用户是否已经登陆).记住我功能 一 安装 pip install flask-login 二 导入相关模块及对象并初始化 from flas ...
- 一台电脑启动多个tomcat
原文 http://dong-shuai22-126-com.iteye.com/blog/1763666 如果现在一台机器上已经部署了一个tomcat服务,无论这个tomcat是否已经注册为服务了, ...
- nginx关于 error_page指令详解.md
error_page指令解释 nginx指令error_page的作用是当发生错误的时候能够显示一个预定义的uri,比如: error_page 502 503 /50x.html; 这样实际上产生了 ...
- struts2 国际化语言转换
学习struts2,了解了使用struts2的配置文件可以走向国际化,实现页面的语言转换.我已中文和英文为例,简单的实现登录页面的国际化 废话不多说,上代码 一,login.jsp页面 使用s标签&l ...
- spring cglib 与 jdk 动态代理
1. 概述 JDK动态代理是利用java反射机制 生成一个实现接口的匿名类, 在调用具体方法前调用InvocationHandler来处理 Cglib动态代理是 利用asm开源包 把被代理类的clas ...