题目:

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)。
以下为代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. using namespace std;
  5. long long n,res,ans;
  6. int main()
  7. {
  8. while(1)
  9. {
  10. scanf("%lld",&n);
  11. if(!n)return 0;
  12. res=n;
  13. int nn=n;
  14. for(int i=2;i*i<=nn;i++)
  15. {
  16. if(nn%i==0)
  17. {
  18. res-=res/i;//减去其中素因子i的倍数的个数 
  19. // res=res/i*(i-1);
  20. while(nn%i==0)nn/=i;//提走素因子i 
  21. }
  22. }
  23. if(nn>1)res-=res/nn;//不能是res--!
  24. // if(nn>1)res=res/n*(n-1);
  25. ans=(n*(n-1)/2-n*res/2)%1000000007;
  26. printf("%lld\n",ans);
  27. }
  28. return 0;
  29. }

  

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. ReentrentLock重入锁

    ReentrentLock lock=new ReentrentLock(); lock.lock(); //锁的代码 finally{ lock.unlock(); } ReentrentLock ...

  2. javascript调试常用工具讲解

    .Console命令详解,让调试js代码变得更简单 2.<Firebug入门指南>

  3. zeroMQ研究(转)

    偶尔一个机会,了解了下zeroMQ消息队列. 1  ZeroMQ概述 ZeroMQ是一种基于消息队列的多线程网络库,其对套接字类型.连接处理.帧.甚至路由的底层细节进行抽象,提供跨越多种传输协议的套接 ...

  4. Ubuntu/CentOS下源码编译安装Php 5.6基本参数

    先确认安装libxml2 apt-get install libxml2 libxml2-dev或者yum install libxml2 libxml2-dev ./configure --pref ...

  5. c++ 系统函数实现文件拷贝

    #include "stdafx.h" #include <string> #include<windows.h> #include<iostream ...

  6. linux 打印系统时间操作

    版权为个人所有,如需转载请说明出处.(东北大亨) http://www.cnblogs.com/northeastTycoon/p/5511498.html 1. 打开shell脚本 例子1:输出两天 ...

  7. Android 红色小圆球提示气泡 BadgeView

    今天给大家分享两个实用有简单的一个小圆球提示气泡: BadgeView 参考地址: https://github.com/qstumn/BadgeView;       个人地址:http://git ...

  8. 【BZOJ2510】弱题 期望DP+循环矩阵乘法

    [BZOJ2510]弱题 Description 有M个球,一开始每个球均有一个初始标号,标号范围为1-N且为整数,标号为i的球有ai个,并保证Σai = M. 每次操作等概率取出一个球(即取出每个球 ...

  9. 怎么利用jquery.form 提交form

    说明:开发环境 vs2012 asp.net mvc c# 利用jQuery.form.js提交form 1.HTML前端代码 <%@ Page Language="C#" ...

  10. 点聚-weboffice 6.0 (二)

    1.修订操作 //设置当前操作用户 function SetUserName() { try{ var webObj=document.getElementById("WebOffice1& ...