题目:

Problem Description
Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1.
 
Input
For each test case, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.
 
Output
For each test case, you should print the sum module 1000000007 in a line.
 
Sample Input
3 4 0
 
Sample Output
0 2

欧拉函数模板:
先求出φn;
由gcd(n,i)=1得gcd(n,n-i)=1,则与n互素的数之和=φ(n)*n/2;
则ans=小于n的数之和-φ(n)*n/2(注意这里不能为“-φ(n)/2*n”或“-n/2*φ(n)”,先/2可能变成0)。
以下为代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long n,res,ans;
int main()
{
while(1)
{
scanf("%lld",&n);
if(!n)return 0;
res=n;
int nn=n;
for(int i=2;i*i<=nn;i++)
{
if(nn%i==0)
{
res-=res/i;//减去其中素因子i的倍数的个数 
// res=res/i*(i-1);
while(nn%i==0)nn/=i;//提走素因子i 
}
}
if(nn>1)res-=res/nn;//不能是res--!
// if(nn>1)res=res/n*(n-1);
ans=(n*(n-1)/2-n*res/2)%1000000007;
printf("%lld\n",ans);
}
return 0;
}

  

hdu3501Calculation 2——欧拉函数模板的更多相关文章

  1. UVA 10820 欧拉函数模板题

    这道题就是一道简单的欧拉函数模板题,需要注意的是,当(1,1)时只有一个,其他的都有一对.应该对欧拉函数做预处理,显然不会超时. #include<iostream> #include&l ...

  2. POJ 2407:Relatives(欧拉函数模板)

    Relatives AC代码 Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16186   Accept ...

  3. poj2407(欧拉函数模板)

    sqrt(n)复杂度 欧拉函数模板 #include <iostream> #include <cstdio> #include <queue> #include ...

  4. 数论 - 欧拉函数模板题 --- poj 2407 : Relatives

    Relatives Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11372   Accepted: 5544 Descri ...

  5. P2158 [SDOI2008] 仪仗队(欧拉函数模板)

    题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线所及的学生人数来判断队伍是否整齐(如下图 ...

  6. hdu1286 找新朋友 欧拉函数模板

    首先这一题用的是欧拉函数!!函数!!不是什么欧拉公式!! 欧拉函数求的就是题目要求的数. 关于欧拉函数的模板网上百度一下到处都是,原理也容易找,这里要介绍一下另一个强势模板. 在这一题的讨论里看到的. ...

  7. acwing 873. 欧拉函数 模板

    地址 https://www.acwing.com/problem/content/875/ 给定n个正整数ai,请你求出每个数的欧拉函数. 欧拉函数的定义 输入格式 第一行包含整数n. 接下来n行, ...

  8. 快速切题 sgu102.Coprimes 欧拉函数 模板程度 难度:0

    102. Coprimes time limit per test: 0.25 sec. memory limit per test: 4096 KB For given integer N (1&l ...

  9. XDU 1098 (欧拉函数模板题)

    原题链接,点击此处 欧拉函数:φ(N)表示对一个正整数N,欧拉函数是小于N且与N互质的数的个数 通式:φ(x) = x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/p ...

随机推荐

  1. Chrome自带恐龙小游戏的源码研究(四)

    在上一篇<Chrome自带恐龙小游戏的源码研究(三)>中实现了让游戏昼夜交替,这一篇主要研究如何绘制障碍物. 障碍物有两种:仙人掌和翼龙.仙人掌有大小两种类型,可以同时并列多个:翼龙按高. ...

  2. Struts2实现input数据回显

    /** 修改页面 */    public String editUI() {        //准备回显得数据        Role role = roleService.getById(id); ...

  3. python学习(三)数字类型示例

    奶奶的报了这个错,我以为可以像java中字符串加数字的嘛 Traceback (most recent call last):   File "./number.py", line ...

  4. erlang的随机数 及 random:uniform()函数

    每次调用会更新进程字典里的random_seed变量,这样在同一个进程内每次调用random:uniform()时,随机数种子都不同,所以生成的随机数都不一样(调用完random:uniform()后 ...

  5. Docker基础原理

    前言 Docker是一个开源的软件项目,让用户程序部署在一个相对隔离的环境运行,借此在Linux操作系统上提供一层额外的抽象,以及操作系统层虚拟化的自动管理机制.需要额外指出的是,Docker并不等于 ...

  6. 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】

    启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...

  7. hdu 4667 Building Fence < 计算几何模板>

    //大白p263 #include <cmath> #include <cstdio> #include <cstring> #include <string ...

  8. 淘宝(新浪)API获取IP地址位置信息

    package com.parse; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IO ...

  9. 消息队列activeMq 使用介绍

      深入浅出 消息队列 ActiveMQhttp://blog.csdn.net/jwdstef/article/details/17380471 一. 概述与介绍 ActiveMQ 是Apache出 ...

  10. 九度OJ 1145:Candy Sharing Game(分享蜡烛游戏) (模拟)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:248 解决:194 题目描述: A number of students sit in a circle facing their teac ...