sgu 102 Coprimes

太水了, 我都不忍心发题解, 但毕竟是sgu上一道题, 我试试能不能一直这么写下去,就是求phi,上代码


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define N 10010
using namespace std;

int get_phi(int n)
{
int m = sqrt(n+0.5);
int ans = n;
for (int i = 2; i <= m; ++i)
if (n % i == 0)
{
ans = ans / i * (i-1);
while (n % i == 0) n /= i;
}
if (n > 1) ans = ans / n * (n-1);
return ans;
}

int main()
{
int n;
scanf("%d", &n);
printf("%d\n", get_phi(n));
}

sgu 102 Coprimes

时间: 2024-11-29 06:20:04

sgu 102 Coprimes的相关文章

SGU - 102 - Coprimes (简单数论!)

SGU - 102 Coprimes Time Limit: 250MS   Memory Limit: 4096KB   64bit IO Format: %I64d & %I64u Submit Status Description For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprime with N. Let us call two positi

(欧拉函数) SGU 102

A - Coprimes Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 102 Appoint description:  SUB_Contest  (2013-03-04) System Crawler  (2015-01-20) Description For given integer N (1<=N<=104) find

SGU 102

这是SGU 102的一份题解 题目要求找不大于N的自然数中与N互质的数,N的范围是1到10000. 由于N的范围小,可以用直接枚举的方法做,辗转相除求GCD. 但要考虑特殊情况:N=1时,1和它本身互质. 如果数据范围变大,如10^9,则不能用枚举. 用分解质因数的方法做. 用唯一分解定理: n>=2,设p1,p2,p3,......,pn是n的素因子,则不大于它的自然数中与它互质的有 n*(1-1/p1)*(1-1/p2)*......*(1-1/pn)个 n=1,n没有素因子,个数为1(即其

Coprimes - SGU 102(求互质数,水)

题目大意:给你一个正整数N,求出来不超过N 的并且与N互质的正整数的个数. 就是一个大水题~~~ 代码: #include<stdio.h> #include<string.h> int GCD(int m, int n) { if(n == 0) return m; return GCD(n, m%n); } int main() { int N, sum=0; scanf("%d", &N); for(int i=1; i<=N; i++) {

SGU 乱搞日志

SGU 100 A+B :太神不会 SGU 101 Domino: 题目大意:有N张骨牌,两张骨牌有两面有0到6的数字,能相连当且仅当前后数字相同,问能否有将N张骨牌连接的方案?思路:裸的欧拉回路,注意自环,连通 1 //sgu101 2 #include<iostream> 3 #include<cstdio> 4 #include <math.h> 5 #include<algorithm> 6 #include<string.h> 7 #i

【SGU】SGU每日练1&#183;Coprimes【数论】欧拉函数

先介绍欧拉函数及其相关定理吧: 1.欧拉函数:对于任意一个数X,将其分解质因数为X=(P1^b1)*(P2^b2)*(P3^b3)...... 则小于X的与X互质的数的个数N为N = X * (1 - 1 / p1) * (1 - 1 / p2)....... 显然对于质数p,Euler(p) = p - 1: 2.一个数的所有质因子之和是euler(n)*n/2: 3.a^Euler(n) % n = 1,这里可以用模运算来证明: 具体实现: 1.先筛素数 2.从2开始遍历素数,能整除则乘,然

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流

The Glorious Karlutka River =) Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 438 Appoint description: Description A group of Mtourists are walking along the Karlutka river. They want to cross

sgu Kalevich Strikes Back

这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 120100 6 using namespace std; 7 8 long long s[maxn]; 9 vector<int>g[maxn]; 10 i