UVALive 7362 Farey (欧拉函数)
2016-08-16 01:56
381 查看
大体题意:
告诉你n (n<=10000) 求解有多少个分数 满足分母 1 < b <= n 分子 a 与b 互质?
思路:
这种问法很显然的是欧拉函数!
定义:
欧拉函数f(n) 表示小于或等于n 的书中与n 互质的数的数目。
(1).f(1) = 1;
(2).若n 是素数p 的k次幂,f(n) = p^k - p^k-1 = (p-1)*p^(k-1)
(3).如果m 和n 互质,f(m*n) = f(m) * f(n);
所以整体思路就有了,直接枚举b 对b用欧拉函数即可!
告诉你n (n<=10000) 求解有多少个分数 满足分母 1 < b <= n 分子 a 与b 互质?
思路:
这种问法很显然的是欧拉函数!
定义:
欧拉函数f(n) 表示小于或等于n 的书中与n 互质的数的数目。
(1).f(1) = 1;
(2).若n 是素数p 的k次幂,f(n) = p^k - p^k-1 = (p-1)*p^(k-1)
(3).如果m 和n 互质,f(m*n) = f(m) * f(n);
所以整体思路就有了,直接枚举b 对b用欧拉函数即可!
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<vector> #include<set> #include<map> #include<stack> #include<cctype> #include<string> #include<iostream> #include<queue> #define fout freopen("out.txt","w","stdout"); #define mr make_pair #define fi first #define se second using namespace std; typedef long long ll; typedef unsigned long long LLU; const int maxn = 1000 + 5; const int mod = 1000000000; const double eps = 1e-8; const int inf = 0x3f3f3f3f; int gcd(int a,int b){ return !b?a:gcd(b,a%b); } int dp[10007]; int vis[10007]; vector<int>prime; int len_prime; void init(){ dp[1] = 2; int len = sqrt(10000) + 1; for (int i = 2; i <= len; ++i) if (!vis[i]){ for (int j = i*i; j < 10000; j+=i)vis[j] = 1; } for (int i = 2; i < 10000; ++i)if (!vis[i])prime.push_back(i); len_prime=(int)prime.size(); for (int i = 2; i <= 10000; ++i){ int ans = i; int n = i; for (int j = 0; j < len_prime; ++j){ int pri = prime[j]; if (n % pri == 0){ ans = ans/pri * (pri-1); while(n % pri == 0)n/=pri; } } // printf("ans = %d\n",ans); dp[i] = dp[i-1] + ans; } } int main(){ int T; init(); scanf("%d",&T); while(T--){ ll k,n; scanf("%lld %lld",&k,&n); printf("%lld %d\n",k,dp ); } return 0; }
相关文章推荐
- 【转】UVALive 5964 LCM Extreme --欧拉函数
- (模板题)UVALive 7362 Farey(欧拉函数)
- UVALIVE 7362(欧拉函数)
- POJ 3090 ZOJ 2777 UVALive 3571 Visible Lattice Points(用递推比用欧拉函数更好)
- UVALive 5964 LCM Extreme(数论 欧拉函数)
- UVaLive 7362 Farey (数学,欧拉函数)
- UVALive5964 LCM Extreme && UVA11426 GCD - Extreme (II) 欧拉函数筛法
- UVALive 6451:Tables(模拟 Grade D)
- uvalive 2326 - Moving Tables(区间覆盖问题)
- Image Is Everything UVALive - 2995
- uvalive 2326 - Moving Tables(区间覆盖问题)
- UVALive 6469 Deranged Exams
- UVALive - 5095 Transportation(拆边+费用流)
- HDU1397 POJ2909 UVA686 UVALive5674 ZOJ1657 Goldbach's Conjecture(II)【水题+打表】
- Uvalive 6428 A+B(扩展欧几里得算法)
- UVALive 3977 BFS染色
- UVALive 5220 Internet Bandwidth(最大流,模板题)
- UVA 10820 Send a Table [欧拉函数] [线性筛法]
- Paint the Roads UVALive - 2197
- uvalive2965(状态压缩)