链接:

http://vj.acmclub.cn/contest/view.action?cid=168#problem/B

Coprimes

时限:250MS     内存:4096KB     64位IO格式:%I64d & %I64u

问题描述

For given integer N (1<=N<=104) find amount of positive numbers not greater than N that coprime with N. Let us call two positive integers (say, A and B, for example) coprime if (and only if) their greatest common divisor is 1. (i.e. A and B are coprime iff gcd(A,B) = 1).

Input

Input file contains integer N.

Output

Write answer in output file.

Sample Input

9

Sample Output

6

初等数论里的欧拉公式:

  欧拉φ函数:φ(n)是所有小于n的正整数里,和n互素的整数的个数。n是一个正整数。

  欧拉证明了下面这个式子:

  如果n的标准素因子分解式是p1^a1*p2^a2*……*pm^am,其中众pj(j=1,2,……,m)都是素数,而且两两不等。则有

  φ(n)=n(1-1/p1)(1-1/p2)……(1-1/pm)

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue> using namespace std; #define INF 0x3f3f3f3f
#define N 11000 int a[N], vis[N], cnt; void IN()
{
int i, j;
cnt = ; memset(a, , sizeof(a));
memset(vis, , sizeof(vis)); for(i=; i<N; i++)
{
if(!vis[i])
{
a[cnt++] = i;
for(j=i+i; j<N; j+=i)
vis[j] = ;
}
}
} int main()
{
int n; IN(); while(scanf("%d", &n)!=EOF)
{
int i=, aa[N]={}, bnt = , m; m = n;
while(a[i]<=m)
{
if(m%a[i]==)
{
m /= a[i];
if(a[i]!=aa[bnt-])
aa[bnt++] = a[i];
}
else
i++;
} for(i=; i<bnt; i++)
n = n-n/aa[i]; printf("%d\n", n);
} return ;
}

我醉了,其实可以这么简单

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue> using namespace std; #define INF 0x3f3f3f3f
#define N 11000 int gcd(int a, int b)
{
return b==?a:gcd(b, a%b);
} int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int sum = ;
for(int i=; i<=n; i++)
{
if(gcd(i, n)==)
sum ++;
}
printf("%d\n", sum);
} return ;
}

(欧拉公式 很水) Coprimes -- sgu -- 1002的更多相关文章

  1. 【jQuery基础学习】09 jQuery与前端(这章很水)

    这章主要是将如何将jQuery应用到网站中,或者说其实就是一些前端知识,对于我这种后端程序来说其实还是蛮有用的. 关于网站结构 文件结构 前端文件分三个文件夹放 images文件夹用来存放将要用到的图 ...

  2. 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便

    A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. FZU 2205 据说题目很水

    2205 据说题目很水 Accept: 199    Submit: 458Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem Descr ...

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

    题目大意:给你一个正整数N,求出来不超过N 的并且与N互质的正整数的个数. 就是一个大水题~~~ 代码: #include<stdio.h> #include<string.h> ...

  5. hdu 3295 模拟过程。数据很水

    An interesting mobile game Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Ja ...

  6. FZUOJ 2205 据说题目很水 (无三元环图最大边数)

    Problem Description Sunday最近对图论特别感兴趣,什么欧拉回路什么哈密顿回路,又是环又是树.在看完一本书后,他对自己特别有信心,便找到大牛牛犇犇,希望他出一题来考考自己. 在遥 ...

  7. 2753:走迷宫(dfs+初剪)//可以说是很水了。。。

    总时间限制:  1000ms 内存限制:  65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最少需要走多少步(数 ...

  8. POJ 2080 Calendar(很水的模拟)

    刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...

  9. 一年前的很水的渣网页(第一次html试水)

    <!doctype html> <html lang="zh-cn"> <base target="_blank" /> & ...

随机推荐

  1. 正则表达式在JS中的使用

    <script type="text/javascript"> /** *正则表达式在js中的第一种使用方式: * RegExp 通过构造器去使用正则表达式 需要对反斜 ...

  2. samtools软件的使用

    1)samtools简介--------------------------------------------------------------------------背景:前面我们讲过sam/b ...

  3. blast及其格式输出简介

    1)blast产生背景 双序列比对可以采用是基于动态规划算法的Needleman-Wunsch(NW)和Smith-Waterman algorithm(SW)算法,虽然精度高,但计算消耗大.当与数据 ...

  4. hdoj1069 Monkey and Banana(DP--LIS)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1069 思路: 由题意,显然一种block可能有6种形式,且一种形式最多使用一次,因此最多有30×6=1 ...

  5. Java的线程同步

    synchronized获取的锁是对象,而不是函数或语句块. 项目结构 资源类 import java.util.concurrent.TimeUnit; public class myResourc ...

  6. centos7,Python2.7安装request包

    1.安装epel扩展源:“sudo yum install epel-release” 2.安装python-pip:“sudo yum install python-pip” 3.升级pip:“su ...

  7. java和数据结构的面试考点

    目标:不要有主要的逻辑错误.2遍以内bug free.注意代码风格 不要让面试官觉得不懂规矩 Java vs C++ Abstract class vs interface  pass by refe ...

  8. 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:

    1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...

  9. [转]Win7下PEiD已停止工作

    转载自:http://www.programlife.net/peid-stop-working-under-win7.html PEID是一个很不错的工具,可查壳,查壳PE信息,借助插件还可以做一些 ...

  10. MVC FormCollection 无法获取值的问题

     把action定义为[HttpPost],并且ajax.beginform中ajaxoption中定义为Post,在提交表单时就可以获取FormCollection的值了.httpGet或者后台不定 ...