zoukankan      html  css  js  c++  java
  • HDU3501_calculation2_欧拉函数求和

    /*
    *Time: 0 ms
    *题目大意:
    *        求1~n里面比n小,但是与n不互素的数的总和。
    *解题思路:
    *        利用欧拉函数即可求解,1~n比n小且与n互素的数的总和为
    *        sum(n) = n * phi(n) / 2;那么可以先求出1~n-1的总和,然后
    *        减去sum(n)即可。
    */
    View Code
     1 #include <iostream>
     2 #include <cmath>
     3 using namespace std;
     4 
     5 int jisuan(int x)
     6 {
     7     int i,res=x;
     8     for(i=2;i<(int)sqrt(x*1.0)+1;i++)
     9 
    10         if(x%i==0)
    11         {
    12             res=res/i*(i-1);
    13             while(x%i==0)
    14             x/=i;
    15         }
    16         if(x>1)
    17         res=res/x*(x-1);
    18         return res;
    19 }
    20 
    21 int main(void)
    22 {
    23     unsigned __int64 n;
    24     while(scanf("%I64d", &n), n)
    25     {
    26         unsigned __int64 sum = n * (n + 1) / 2 - n, res;
    27         res = sum - (n * jisuan(n) / 2);
    28         printf("%I64d\n", res % 1000000007);
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    JavaWeb WebBrowserTool KernelEngine
    类模板 C++快速入门45
    动态数组的使用
    动态数组的使用
    鱼C小甲鱼
    栈原理演示
    鱼C小甲鱼
    类模板 C++快速入门45
    delphi实例
    栈原理演示
  • 原文地址:https://www.cnblogs.com/cchun/p/2598393.html
Copyright © 2011-2022 走看看