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;
    }
    

      

  • 相关阅读:
    LeetCode Path Sum II
    LeetCode Longest Palindromic Substring
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Best Time to Buy and Sell Stock III
    LeetCode Binary Tree Maximum Path Sum
    LeetCode Find Peak Element
    LeetCode Maximum Product Subarray
    LeetCode Intersection of Two Linked Lists
    一天一个设计模式(1)——工厂模式
    PHP迭代器 Iterator
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5774449.html
Copyright © 2011-2022 走看看