zoukankan      html  css  js  c++  java
  • C

    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.

    InputFor 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.OutputFor each test case, you should print the sum module 1000000007 in a line.Sample Input

    3
    4
    0

    Sample Output

    0
    2

    给定一个正整数N,你的任务是计算小于N的正整数的和,这些正整数不是共圆到N的。

    输入

    对于每个测试用例,有一行包含一个正整数N(1≤N≤1000000000)。最后一个测试用例后面有一行包含一个0。

    输出

    对于每个测试用例,您应该在一行中打印sum模块1000000007。

    样例输入

    3.

    4

    0

    样例输出

    0

    2

    思路:本来以为这道题会是欧拉函数只不过返回的不是欧拉数,而是GCD(n,i)大于一的和,结果WA了

    想了想,以6为例

    1 2 3 4 5 6 其中 2 3 4 都是满足题意的,但是欧拉函数不会走4,他只会走它所有的质因子

    没有办法,只有看大佬题解,借鉴后看到了 求互质的数字和可以直接利用 n * enler( n) / 2求解,而此题要求的是非互质的,同样可以使用这个性质

    于是就一个欧拉函数,轻松AK,看到大佬用容斥写,哎还是见识太短,学的太少,小菜鸟今天又是队伍倒一。。。。。。。。

    #include <iostream>
    #include <cstdio>
    #include <string>
    #include <algorithm>
    #include <map>
    #define Mod 1000000007
    using namespace std;
    typedef long long ll;
    const  ll N = 1000000+10;
    int elh[N];
    int a;
    ll Euler(ll n)
    {
        ll res =n;
        for(int i=2;i<=n/i;i++)
        {
            if(n%i==0)
            {
                res = res-res/i;
            }
            while(n%i==0)n/=i;
        }
        if(n>1)res =res- res/n;
        return res%Mod;
    }
    int main()
    {
        while(~scanf("%lld",&a)&&a)
        {
            cout <<(a*(a-1-Euler(a))/2)%Mod<<endl;
        }
        return 0;
    }
    你的脸上风淡云轻,谁也不知道你的牙咬得有多紧。你走路带着风,谁也不知道你膝盖上仍有曾摔过的伤的淤青。你笑得没心没肺,没人知道你哭起来只能无声落泪。要让人觉得毫不费力,只能背后极其努力。我们没有改变不了的未来,只有不想改变的过去。
  • 相关阅读:
    IronRuby:元编程特性【method_missing】的使用
    DNN(DotNetNuke) 3.0感官刺激零距x接触!!! :)
    (MS SQL)如何实现相关文章功能(多关键字匹配)改进版
    谁有微软认证,如MCSD,MCDBA,MCXX等等,马上告诉我
    开源代码2004/12/25 codeproject
    开源代码2004/1220-PDF格式/文件相关
    强烈推荐一个超酷的跨平台、支持多数据库的数据库管理工具
    (MS SQL)如何实现相关文章功能(多关键字匹配)
    DotNetNuke(DNN)从入门到进阶(1)-怎样写自己的模块
    推荐开源代码2004/12/17
  • 原文地址:https://www.cnblogs.com/wangzhe52xia/p/11394134.html
Copyright © 2011-2022 走看看