题意:给定一个数 n,问你0<= a <=n, 0 <= b <= n,有多少个不同的最简分数。

析:这是一个欧拉函数题,由于当时背不过模板,又不让看书,我就暴力了一下,竟然AC了,才2s,题目是给了3s,很明显是由前面递推,前面成立的,后面的也成立,

只要判定第 i 个有几个,再加前 i-1 个就好,第 i 个就是判断与第 i 个互质的数有多少,这就是欧拉函数了。

代码如下:

这是欧拉函数的。

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <stack>
  15. using namespace std;
  16.  
  17. typedef long long LL;
  18. typedef pair<int, int> P;
  19. const int INF = 0x3f3f3f3f;
  20. const double inf = 0x3f3f3f3f3f3f;
  21. const double PI = acos(-1.0);
  22. const double eps = 1e-8;
  23. const int maxn = 10000 + 5;
  24. const int mod = 1e9;
  25. const char *mark = "+-*";
  26. const int dr[] = {-1, 0, 1, 0};
  27. const int dc[] = {0, 1, 0, -1};
  28. int n, m;
  29. inline bool is_in(int r, int c){
  30. return r >= 0 && r < n && c >= 0 && c < m;
  31. }
  32. int ans[maxn];
  33. int phi[maxn];
  34.  
  35. void init(){
  36. memset(phi, 0, sizeof(phi));
  37. phi[1] = 1;
  38. for(int i = 2; i <= 10000; ++i) if(!phi[i])
  39. for(int j = i; j <= 10000; j += i){
  40. if(!phi[j]) phi[j] = j;
  41. phi[j] = phi[j] / i * (i-1);
  42. }
  43.  
  44. ans[2] = 3;
  45. for(int i = 3; i <= 10000; ++i)
  46. ans[i] = ans[i-1] + phi[i];
  47. }
  48.  
  49. int main(){
  50. init();
  51. int T; cin >> T;
  52. while(T--){
  53. scanf("%d %d", &m, &n);
  54. printf("%d %d\n", m, ans[n]);
  55. }
  56. return 0;
  57. }

  

这是我暴力的:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <stack>
  15. using namespace std;
  16.  
  17. typedef long long LL;
  18. typedef pair<int, int> P;
  19. const int INF = 0x3f3f3f3f;
  20. const double inf = 0x3f3f3f3f3f3f;
  21. const double PI = acos(-1.0);
  22. const double eps = 1e-8;
  23. const int maxn = 100 + 5;
  24. const int mod = 1e9;
  25. const char *mark = "+-*";
  26. const int dr[] = {-1, 0, 1, 0};
  27. const int dc[] = {0, 1, 0, -1};
  28. int n, m;
  29. inline bool is_in(int r, int c){
  30. return r >= 0 && r < n && c >= 0 && c < m;
  31. }
  32. int ans[10005];
  33.  
  34. int main(){
  35. ans[1] = 2; ans[2] = 3;
  36. for(int i = 3; i <= 10000; ++i){
  37. int cnt = 0;
  38. for(int j = 1; j <= i/2; ++j){
  39. if(__gcd(j, i) == 1) ++cnt;
  40. }
  41. ans[i] = ans[i-1] + 2*cnt;
  42. }
  43. int T; cin >> T;
  44. while(T--){
  45. scanf("%d %d", &m, &n);
  46. printf("%d %d\n", m, ans[n]);
  47. }
  48. return 0;
  49. }

  

UVaLive 7362 Farey (数学,欧拉函数)的更多相关文章

  1. poj2478 Farey Sequence (欧拉函数)

    Farey Sequence 题意:给定一个数n,求在[1,n]这个范围内两两互质的数的个数.(转化为给定一个数n,比n小且与n互质的数的个数) 知识点: 欧拉函数: 普通求法: int Euler( ...

  2. 【BZOJ4173】数学 欧拉函数神题

    [BZOJ4173]数学 Description Input 输入文件的第一行输入两个正整数 . Output 如题 Sample Input 5 6 Sample Output 240 HINT N ...

  3. POJ2478 Farey Sequence —— 欧拉函数

    题目链接:https://vjudge.net/problem/POJ-2478 Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K To ...

  4. poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)

    http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...

  5. NOIP模拟:切蛋糕(数学欧拉函数)

    题目描述  BG 有一块细长的蛋糕,长度为 n. 有一些人要来 BG 家里吃蛋糕, BG 把蛋糕切成了若干块(整数长度),然后分给这些人. 为了公平,每个人得到的蛋糕长度和必须相等,且必须是连续的一段 ...

  6. poj2478 Farey Sequence 欧拉函数的应用

    仔细看看题目,按照题目要求 其实就是 求 小于等于n的 每一个数的 欧拉函数值  的总和,为什么呢,因为要构成 a/b 然后不能约分  所以 gcd(a,b)==1,所以  分母 b的 欧拉函数值   ...

  7. hdu1787 GCD Again poj 2478 Farey Sequence 欧拉函数

    hdu1787,直接求欧拉函数 #include <iostream> #include <cstdio> using namespace std; int n; int ph ...

  8. UVA12995 Farey Sequence [欧拉函数,欧拉筛]

    洛谷传送门 Farey Sequence (格式太难调,题面就不放了) 分析: 实际上求分数个数就是个幌子,观察可以得到,所求的就是$\sum^n_{i=2}\phi (i)$,所以直接欧拉筛+前缀和 ...

  9. 【转】UVALive 5964 LCM Extreme --欧拉函数

    题目大意:求lcm(1,2)+lcm(1,3)+lcm(2,3)+....+lcm(1,n)+....+lcm(n-2,n)+lcm(n-1,n)解法:设sum(n)为sum(lcm(i,j))(1& ...

随机推荐

  1. [LeetCode#247] Strobogrammatic Number II

    Problem: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked a ...

  2. 解析Android开发优化之:从代码角度进行优化的技巧

    下面我们就从几个方面来了解Android开发过程中的代码优化,需要的朋友参考下   通常我们写程序,都是在项目计划的压力下完成的,此时完成的代码可以完成具体业务逻辑,但是性能不一定是最优化的.一般来说 ...

  3. iOS开发:视图生命周期

    iOS应用的视图状态分为以下几种 在viewcontroller的父类UIViewController中可以看到如下代码,通过重写不同的方法对操作视图渲染. @available(iOS 2.0, * ...

  4. LA 3510 (置换 循环分解) Pixel Shuffle

    思路挺简单的,题目中的每个命令(包括命令的逆)相当于一个置换. 用O(n2k)的时间复杂度从右往左求出这些置换的乘积A,然后求m使Am = I(I为全等置换) 还是先把A分解循环,m则等于所有循环节长 ...

  5. span元素定义宽高度

    由于span是行内元素,不可能有高度和宽度的,在span标签里添加内容,可以撑出来宽高,想要定义宽高必须转话成块级元素. span{ display:block; } 或者使用 span{ displ ...

  6. malloc、free的使用

    一.malloc()和free()的基本概念以及基本用法: 1.函数原型及说明: void *malloc(long NumBytes):该函数分配了NumBytes个字节,并返回了指向这块内存的指针 ...

  7. 转载RabbitMQ入门(6)--远程调用

    远程过程调用(RPC) (使用Java客户端) 在指南的第二部分,我们学习了如何使用工作队列将耗时的任务分布到多个工作者中. 但是假如我们需要调用远端计算机的函数,等待结果呢?好吧,这又是另一个故事了 ...

  8. 【转】有趣的Autolayout示例-Masonry实现

    原文网址:http://tutuge.me/2015/05/23/autolayout-example-with-masonry/ 好久没有写Blog了,这段时间有点忙啊=.=本文举了3个比较有“特点 ...

  9. Delphi DecodeDate和EncodeDate 拆分和聚合时间函数的用法

    SysUtilsprocedure DecodeData(Date: TDateTime; var Year, Month, Day: Word);DecodeDate打断TdateTime成为年月日 ...

  10. IOS 多级列表展开控件

    项目中实现了一个可以多级展开的列表控件.每次展开都是互斥的,就是说,展开一个cell 就会关闭其他展开的层. 可以呈现的效果如下图.第一个图片是应用中实现的效果.第二个是Demo中的效果.如果有新的需 ...