zoukankan      html  css  js  c++  java
  • Hdu3501容斥原理

    题意:问小于n且不与n互质的数的和是多少。

    容斥原理求出和n互质的和,然后用 n*(n-1)/2 减以下,注意溢出。

    #pragma comment(linker,"/STACK:102400000,102400000") 
    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<queue>
    #include<stack>
    #include<list>
    #include<stdlib.h>
    #include<algorithm>
    #include<vector>
    #include<map>
    #include<cstring>
    #include<set>
    using namespace std;
    typedef long long LL;
    const LL mod = 1000000007;
    LL sum(LL n)// 可能越界
    {
        LL t = n; LL t1 = n+1;
        if(t&1) t1/=2; 
        else t/=2;
        return t*t1%mod;
    }
    
    LL ans;
    vector<LL> q;
    void dfs(LL x, LL pre, LL flag, LL key)
    {
        if (x == q.size()) {
        //  cout << pre << " JIi" << endl;
            if (flag) ans += pre*sum(key / pre);
            else ans -= pre*sum(key / pre);
            ans = (ans+mod)%mod;
         //   cout<<ans<<endl;
            return;
        }
        dfs(x + 1, pre, flag, key);
        dfs(x + 1, pre*q[x], flag ^ 1, key);
    }
    
    
    
    void gao(LL n)
    {
        q.clear();
        LL t = n;
        ans = 0;
        for (LL i = 2; i*i <= t; i++) {
            if (t%i) continue;
            while (t%i == 0) t /= i;
            q.push_back(i);
        }
        if (t > 1) q.push_back(t);
        dfs(0, 1, 1, n - 1);
        LL k = sum(n-1);
        k-=ans;
        k%=mod; // 可能超过mod
        if(k<0) k = (k+mod)%mod;
        cout << k << endl;
    }
    
    int main()
    {
        LL n;
        while (scanf("%I64d", &n)!=EOF && n ) {
            gao(n);
        }
        return 0;
    }
  • 相关阅读:
    VSFTP配置参数详解
    C语言---函数
    ios 学习计划
    读书笔记---金融学一<新国富论>
    读书笔记---人生规划一<斯坦福最受欢迎的人生规划课、像卡耐基一样经营人生、九型人格>
    网络基础
    swift中构造方法和Kvc
    swift中的懒加载
    private的用法
    extension
  • 原文地址:https://www.cnblogs.com/yigexigua/p/4728423.html
Copyright © 2011-2022 走看看