1. 程式人生 > >hdu2588+3501【尤拉函式】

hdu2588+3501【尤拉函式】

這倆題目都算是尤拉函式的應用極致了,直接上題目

 Calculation 2


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
題意:求1-n之間與n不互質的數的和

思路:暴力求解是行不通的,尤拉函式是求與n互質的數的個數,如何求和是個問題

先給出和的公式:sum(n)=n/2*phi[n]

九章算術裡的更相減損法       ↓↓↓↓↓↓↓

簡略的證明一下:gcd(n,k)=gcd(k,n-k);

使k<n並且gcd(n,k)==1,所以存在gcd(n,n-k)也為1,所以小於n的範圍為之內,出現k和n-k兩組,求所有k的和,即(n/2)*phi[n]

蓋提只需要求出1-n的和來,減去與n互質的數的和即可,最後要mod1000000007

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
#define mod 1000000007
#define ll long long
ll get_phi(ll n)
{
    ll ans=n;
    for(int i=2;i<=(double)sqrt(n*1.0);i++)
    {
        if(n%i==0)
            ans=ans*(1.0-1.0/i);
        while(n%i==0)
            n=n/i;
    }
    if(n>1)
        ans=ans*(1.0-1.0/n);
    return ans;
}
int main()
{
    ll n;
    while(cin>>n)
    {
        if(n==0)
            break;
        ll sum=(n-1)*n/2;
        ll ans=sum-get_phi(n)*n/2;
        cout<<ans%mod<<endl;
    }
    return 0;
}

GCD


The greatest common divisor GCD(a,b) of two positive integers a and b,sometimes written (a,b),is the largest divisor common to a and b,For example,(1,2)=1,(12,18)=6. 
(a,b) can be easily found by the Euclidean algorithm. Now Carp is considering a little more difficult problem: 
Given integers N and M, how many integer X satisfies 1<=X<=N and (X,N)>=M.InputThe first line of input is an integer T(T<=100) representing the number of test cases. The following T lines each contains two numbers N and M (2<=N<=1000000000, 1<=M<=N), representing a test case.OutputFor each test case,output the answer on a single line.Sample Input
3
1 1
10 2
10000 72
Sample Output
1
6
260
題意:給n和m,x是1-n之間的數,問你gcd(x,n)>=m的數有多少

思路:剛開始是想從m開始,找到第一個gcd(x,n)>=m的數,然後篩去他的倍數,標記,重複此操作,但是資料太大,不好實現標記

尤拉函式又發揮了

如果m是1,那麼所有的<=n都滿足條件直接出n

m不是1,現x<=n,那假設n=p*d,x=q*d,且gcd(x,n)==d,所以p,q互質,那麼現在問題轉化成了找p使得p*d==n並且d>=m的個數

首先p是n的因子,這個坑的地方就在於n=p*p的時候,留意不要多加

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
using namespace std;
#define mod 1000000007
#define ll long long
ll get_phi(ll n)
{
    ll ans=n;
    for(int i=2;i<=(double)sqrt(n*1.0);i++)
    {
        if(n%i==0)
            ans=ans*(1.0-1.0/i);
        while(n%i==0)
            n=n/i;
    }
    if(n>1)
        ans=ans*(1.0-1.0/n);
    return ans;
}
int main()
{
    int t;
    cin>>t;
    ll n,m;
    while(t--)
    {
        cin>>n>>m;
        ll ans=0;
        if(ans==1)
        {
            cout<<get_phi(n)<<endl;
        }
        else
        {
        for(ll i=1;i<=sqrt(n);i++)
        {
            if(n%i==0)
            {
                if(i>=m)
                    ans+=get_phi(n/i);
                if(i*i!=n&&n/i>=m)
                    ans+=get_phi(i);
            }
        }
        cout<<ans<<endl;
    }
    }
    return 0;
}
可以,貼完程式碼的這一瞬見斷了電。。。。。。。。。。。。

相關推薦

hdu2588+3501函式

這倆題目都算是尤拉函式的應用極致了,直接上題目 Calculation 2Given a positive integer N, your task is to calculate the sum of the positive integers less than N wh

hdu 5728 PowMod數論函式降冪遞迴取模積性函式

【連結】 http://acm.hdu.edu.cn/showproblem.php?pid=5728 【題意】 n是無平方因子的數 定義k=∑mi=1φ(i∗n) mod 1000000007,求K^k^k^k......%p 【思路】 先尤拉性質求出k

nyoj-1007 GCD函式

GCD 時間限制:1000 ms  |  記憶體限制:65535 KB 難度:3 輸入 The first line of input is an integer T(T<=100) representing the number o

hdu 5728 PowMod數論函式降冪遞迴取模積性函式

【連結】 【題意】 n是無平方因子的數 定義k=∑mi=1φ(i∗n) mod 1000000007,求K^k^k^k......%p 【思路】 先尤拉性質求出k,再用尤拉降冪,A^B=A^B%phi(C)+phi(C)  (mod C)求出答案 ∑(i=1~

函式(小於或等於n的數中與n互質的數的數目)

【尤拉函式】 在數論,對正整數n,尤拉函式是少於或等於n的數中與n互質的數的數目。此函式以其首名研究者尤拉命名,它又稱為Euler's totient function、φ函式、尤拉商數等

HDU 1695 GCD 容斥質因數分解函式

題意:給定區間[a,b]和[c,d]和k,求出x∈[a,b],y∈[c,d],使得gcd(x,y)==k的個數,給定a=c=1。 分析:對於滿足gcd(x,y)==k的x,y的值,都有x,y是k的倍數,且x,y互質。因此可以將b,d各除以k,得到的新的b,d中找出互質的對

POJ 3696 The Luckiest Number函式+快速冪+快速乘

Chinese people think of '8' as the lucky digit. Bob also likes digit '8'. Moreover, Bob has his own lucky number L. Now he wants to constr

“盛大遊戲杯”第15屆上海大學程式設計聯賽 J函式 約數函式之和為本身

膜一下將帶給你好運 釋出時間: 2017年7月9日 18:17   最後更新: 2017年7月9日 21:05   時間限制: 1000ms   記憶體限制: 128M 描述 尤拉函式ϕ(n)被定義1~n中與n互質的數的個數。例如ϕ(5)=4,因為1,2,3,4

hdu5152 A Strange Problem數論降冪線段樹單點修改好題

【連結】 http://acm.hdu.edu.cn/showproblem.php?pid=5152 【題意】 給你一個長度為n的序列,有m個操作,3種操作: 1. 給你l,r,輸出l-r的和。 2. 修改操作,x,把a[x]->修改為x^a[x] 3. 加操作。l,

hdu5152 A Strange Problem數論降冪線段樹單點修改好題

【連結】 【題意】 給你一個長度為n的序列,有m個操作,3種操作: 1. 給你l,r,輸出l-r的和。 2. 修改操作,x,把a[x]->修改為x^a[x] 3. 加操作。l,r,x,l-r區間加x 輸出結果對2333333取模。  【思路】 重點在

Codeforces Round #508 (Div. 2) E. Maximum Matching

Step1 Problem: 給你 n 個塊,每個塊左右兩邊有顏色,中間是塊的權值,如果不同的塊的邊顏色一樣,那麼它們可以合併成新的一塊。 例:兩個塊分別是 c1 v1 c2, c2 v2 c1,那麼這兩個塊可以變成 c1 v1+v2 c1,或者

BZOJ 3884上帝與集合的正確用法降冪

題目: 做法: 尤拉降冪:ax≡axmodϕ(p)+ϕ(p)(modp)(x≥p) 證明貼個地址:地址雖然似乎是爬蟲搞出來的但是再也找不到了原出處了. 有了尤拉降冪,可以設f(p)=2222...modp 帶入尤拉降

hdu5883 每個點都有權值,要求按照路或者通路走一遍是的使得權值異或值最大

Alice is planning her travel route in a beautiful valley. In this valley, there are N lakes, and M rivers linking these lakes. Alice wants to start her

hdu2588 GCD (函式

GCD 題意:輸入N,M(2<=N<=1000000000, 1<=M<=N), 設1<=X<=N,求使gcd(X,N)>=M的X的個數。  (文末有題) 題解一: 當M==1時,顯然答案為N。 當M!=1。  X是

bzoj4802函式

題目連結 想瞎搞過去的可能就只有我一個。。。。。 正解Pollard_rho演算法(啥rho演算法?) Pollard_rho演算法是專門解決這類大數質因數分解的,當然還要搭配Miller_Rabin判素數(推薦一篇入門博文) 先質因數分解,然後按 φ(i

模板函式

#include <iostream> using namespace std; int n; int ans; int main() { cin >> n; int last = ans = n; for(int i = 2; i * i <= n; i+

複習函式

首先讓我們來複習以下尤拉函式的概念。 寫作\(phi(i)\),表示小於\(i\)的與\(i\)互質的數的個數 特殊的,\(phi(1)=1\); 根據定義我們可以得到其推導方法。 對於任意的\(i∈[2,INF]\),\(i\)都可以被拆分為\(p1^{c1}*p2^{c2}*..

51nod函式之和(數論,杜教篩)

文章目錄 題目 分析 一個性質 嘗試遞推 分塊 打表 線性篩尤拉函式 一個性質 線性篩 程式碼 題目 12

題解洛谷P2568(bzoj2818)GCD 函式+字首和

題目連結 題目描述 給定整數N,求1<=x,y<=N且Gcd(x,y)為素數的數對(x,y)有多少對. 輸入輸出格式 輸入格式: 一個整數N 輸出格式: 答案 輸入輸出樣例 輸入樣例#1: 4 輸出樣例#1: 4 說明 對於樣例(2,2),(2,4)