(欧拉公式 很水) Coprimes -- sgu -- 1002
链接:
http://vj.acmclub.cn/contest/view.action?cid=168#problem/B
Coprimes
问题描述
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的更多相关文章
- 【jQuery基础学习】09 jQuery与前端(这章很水)
这章主要是将如何将jQuery应用到网站中,或者说其实就是一些前端知识,对于我这种后端程序来说其实还是蛮有用的. 关于网站结构 文件结构 前端文件分三个文件夹放 images文件夹用来存放将要用到的图 ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 236A,虽然很水,但有一个很简单的函数用起来方便
A. Boy or Girl time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- FZU 2205 据说题目很水
2205 据说题目很水 Accept: 199 Submit: 458Time Limit: 1000 mSec Memory Limit : 32768 KB Problem Descr ...
- Coprimes - SGU 102(求互质数,水)
题目大意:给你一个正整数N,求出来不超过N 的并且与N互质的正整数的个数. 就是一个大水题~~~ 代码: #include<stdio.h> #include<string.h> ...
- hdu 3295 模拟过程。数据很水
An interesting mobile game Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Ja ...
- FZUOJ 2205 据说题目很水 (无三元环图最大边数)
Problem Description Sunday最近对图论特别感兴趣,什么欧拉回路什么哈密顿回路,又是环又是树.在看完一本书后,他对自己特别有信心,便找到大牛牛犇犇,希望他出一题来考考自己. 在遥 ...
- 2753:走迷宫(dfs+初剪)//可以说是很水了。。。
总时间限制: 1000ms 内存限制: 65536kB 描述 一个迷宫由R行C列格子组成,有的格子里有障碍物,不能走:有的格子是空地,可以走.给定一个迷宫,求从左上角走到右下角最少需要走多少步(数 ...
- POJ 2080 Calendar(很水的模拟)
刚开始一直WA,才发现原来代码中两处减去年份.月份的天数的判断条件用的是>=,虽然最后考虑n=0要退回一天的情况,但还是WA.后来改成>的条件判断,省去了考虑n=0的麻烦,AC. 此题无非 ...
- 一年前的很水的渣网页(第一次html试水)
<!doctype html> <html lang="zh-cn"> <base target="_blank" /> & ...
随机推荐
- Hibernate DetachedCriteria实现
前段时间在做模糊查询,并利用数据库分页,DAO用hibernate实现,刚开始的时候 根据业务层的数据,拼hql语句进行查询,且不说要进行一些if判断,单从结构上来说, 底层的数据访问层依赖于业务层 ...
- 尴尬!Jsp内置对象
今天挺尴尬的,上网络安全课做错了ppt ,尴尬到头皮发麻. JSP内置对象 JSP内置对象是Web容器创建的一组对象,不使用new关就可以使用的内置对象. <%int[ ]value= {60, ...
- 读取数据库信息并生成表设计文档Word版本
1.参考C#代码 using Help.DBAccessLayer.Business; using Help.DBAccessLayer.Model.SqlGenerator; using Newto ...
- css中选择器
css中常用的选择器有: 1.元素选择器:h1{} 如<h1></h1> 2.类选择器:.test{}或者h1.test{} 如<h1 class="test ...
- 数据库索引&数据页
索引的好处 索引带来的益处可能很多读者会认为只是"能够提高数据检索的效率,降低数据库的IO成本". 确实,在数据库中表的某个字段创建索引,所带来的最大益处就是将该字段作为检索条件时 ...
- frm和ibd恢复sql文件的操作
情况:有mysql中data文件(仅仅一个数据库) 操作:frm和ibd恢复sql文件的操作 1.创建相同名字的库xxx 2.把ibdata1替换成原来的 3.把数据库xxx内内容全部替换为原来的 完 ...
- 元素的定位id和name
1.元素定位: 元素的定位是自动化测试的核心,要想操作一个元素,首先应该识别这个元素 webdriver提供了一系列的元素定位方法,常用的有以下几种 id name class name partia ...
- 第五章 二叉树(c)二叉树
- Windows phone Toast消息推送 学习笔记
简单介绍: Windows phone平台支持三种形式的推送通知: 1.Tile——也就是在Start屏幕程序平铺图标 2.Toast——创建一个显示在当前屏幕中的Toast弹出窗口 3.Raw——有 ...
- win2003上传文件限制
Windows2003系统下,上传较大的文件时,会出现“Request 对象 错误 'ASP 0104 : 80004005'”错误或者出现空白页面的时候,此时,不要轻易的去找程序的问题,有可能是wi ...