zoukankan      html  css  js  c++  java
  • UVaLive 7362 Farey (数学,欧拉函数)

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

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

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

    代码如下:

    这是欧拉函数的。

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 10000 + 5;
    const int mod = 1e9;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int ans[maxn];
    int phi[maxn];
    
    void init(){
        memset(phi, 0, sizeof(phi));
        phi[1] = 1;
        for(int i = 2; i <= 10000; ++i) if(!phi[i])
            for(int j = i; j <= 10000; j += i){
                if(!phi[j])  phi[j] = j;
                phi[j] = phi[j] / i * (i-1);
            }
    
        ans[2] = 3;
        for(int i = 3; i <= 10000; ++i)
            ans[i] = ans[i-1] + phi[i];
    }
    
    int main(){
        init();
        int T;   cin >> T;
        while(T--){
            scanf("%d %d", &m, &n);
            printf("%d %d
    ", m, ans[n]);
        }
        return 0;
    }
    

      

    这是我暴力的:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <stack>
    using namespace std;
    
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 100 + 5;
    const int mod = 1e9;
    const char *mark = "+-*";
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    int n, m;
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int ans[10005];
    
    int main(){
        ans[1] = 2;  ans[2] = 3;
        for(int i = 3; i <= 10000; ++i){
            int cnt = 0;
            for(int j = 1; j <= i/2; ++j){
                if(__gcd(j, i) == 1) ++cnt;
            }
            ans[i] = ans[i-1] + 2*cnt;
        }
        int T; cin >> T;
        while(T--){
            scanf("%d %d", &m, &n);
            printf("%d %d
    ", m, ans[n]);
        }
        return 0;
    }
    

      

  • 相关阅读:
    人工智能正在跨越“恐怖谷”,未来或将善恶共存
    大数据可视化的途径
    数据科学家公司生存指南TOP30秘诀
    真真假假?专访七位AI专家,辨析医疗人工智能真伪
    大数据主要应用于哪些行业,应用价值是什么?
    【人工智能】 火爆的机器学习和人工智能,为何在金融业四处碰壁?
    大数据时代,你是否拥有「文科思维」?
    AI和企业管理
    刷脸新时代:我国人工智能技术世界领先 产业规模3600亿元
    交待给你的事办完了,就不能回个话么?
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5774449.html
Copyright © 2011-2022 走看看