1. 程式人生 > >uva 10375 (唯一分解定理+篩素數)

uva 10375 (唯一分解定理+篩素數)


 Choose and divide

Description
The binomial coefficient C(m,n) is defined as

                                   m!
             C(m,n) =  -----------
                               n!(m-n)!
Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s).
The Input
Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000 with p>=q and r>=s.
The Output

For each line of input, print a single line containing a real number with 5 digits of precision in the fraction, giving the number as described above. You may assume the result is not greater than 100,000,000.
Sample Input
10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
Output for Sample Input

0.12587
505606.46055
1.28223
0.48996
2.00000

3.99960

題意很簡單: 求C(p,q)/C(r,s);

思路:由於10^4,也不能for迴圈,去求C.所以先約分,利用唯一分解定理。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#define bug(a) cout<<a<<"--->\n";
using namespace std;

typedef unsigned long long ULL;

int prime[10001];
bool valid[10001];
int tot;
int e[10000];
int e_cnt;

typedef long long LL;

void get_prime()
{
    tot=0;
    memset(valid,true,sizeof valid);
    for(int i=2;i<10000;i++)
    {
        if(valid[i])
            prime[++tot]=i;
        for(int j=1;j<=tot&&i*prime[j]<10000;j++)
        {
            valid[i*prime[j]]=false;
            if(i%prime[j]==0) break;
        }
    }
}

void prime_break(int n,int d)
{

    for(int i=1;i<=tot;i++)
    {
        while(n%prime[i]==0)
        {
            n/=prime[i];
            e[i]+=d;
            if(i>e_cnt) e_cnt=i;
        }    
        if(n==1)
            return;
    }
}

void solve(int n,int d)
{
    for(int i=1;i<=n;i++)
        prime_break(i,d);
}

int main()
{
    get_prime();
    int p,q,r,s;
    while(scanf("%d%d%d%d",&p,&q,&r,&s)!=EOF)
    {
        memset(e,0,sizeof e);
        solve(p,1);
        solve(r-s,1);
        solve(s,1);
        solve(p-q,-1);
        solve(q,-1);
        solve(r,-1);
        double res=1.0;
        for(int i=1;i<=e_cnt;i++)
            if(e[i])
                res*=pow(prime[i],e[i]);

        printf("%.5lf\n",res);
    }

    return 0;
}


相關推薦

uva 10375 唯一分解定理+素數

 Choose and divide DescriptionThe binomial coefficient C(m,n) is defined as                                    m!              C(m,n) =  

UVA - 10375唯一分解定理

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using nam

【ZCMU1796】wjw的數學題唯一分解定理+排列組合

題目連結 1796: wjw的數學題 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 70  Solved: 25 [Submit][Status][Web Board] Description

zcmu 1796 wjw的數學題 唯一分解定理+排列組合

【題目】 Problem B: wjw的數學題 Time Limit: 2 Sec  Memory Limit: 128 MB Submit: 69  Solved: 24 [Submit][Status][Web Board] Description Wjw rec

uva 10375 唯一分解定理 法求素數【數論】

唯一分解理論的基本內容: 任意一個大於1的正整數都能表示成若干個質數的乘積,且表示的方法是唯一的。換句話說,一個數能被唯一地分解成質因數的乘積。因此這個定理又叫做唯一分解定理。 舉個栗子:50=(2^1)*(5^2)  題目一般的思路就是要把素數表打出來,eg上面的例子 e

UVa 10375 - Choose and divide唯一分解定理

ide clas 數組 AI AS lin buffered ring buffere 鏈接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_p

UVA - 10791 分解質因數唯一分解定理

參考https://www.cnblogs.com/scau20110726/archive/2013/01/18/2866101.html   題意(就是因為讀錯題意而wa了一次):給一個數字n,範圍在[1,2^23-1],這個n是一系列數字的最小公倍數,這一系列數字的個數

LightOJ 1341 Aladdin and the Flying Carpet唯一分解定理

void 都是 scanf esp for space tar sqrt lld http://lightoj.com/volume_showproblem.php?problem=1341 題意:給你矩形的面積(矩形的邊長都是正整數),讓你求最小的邊大於等於b的矩形的個

LIghtOJ-1341-Aladdin and the Flying Carpet 唯一分解定理

原題連結: It’s said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned

hdu 5108 Alexandra and Prime Numbers唯一分解定理

【唯一分解定理】 任意一個大於1的正整數都能表示成若干個質數的乘積,且表示的方法是唯一的。換句話說,一個數能被唯一地分解成質因數的乘積。因此這個定理又叫做唯一分解定理。 公式:n = P1^a1 *

Light oj 1341 Aladdin and the Flying Carpet 唯一分解定理

It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the fi

ACM-ICPC 2018瀋陽網路賽G題唯一分解定理、容斥原理

A sequence of integer \lbrace a_n \rbrace{an​} can be expressed as: \displaystyle a_n = \left\{ \begin{array}{lr} 0, & n=0\\ 2, &am

算數基本原理唯一分解定理

唯一分解定理: 任意一個大於0的正整數都能被表示成若干個素數的乘積且表示方法是唯一的;整理可以將相同素數的合併 X=p1^a1*p2^a2……pn^an; p1..pn 為素數 數X的因子數

牛課 等式唯一分解定理求因子個數

#include<cstdio> #include<cmath> using namespace std; int n, ans=1; int main(){ int t;

算術基本定理唯一分解定理

                                           算術基本定理 算術基本定理: 每個大於1的正整數N都可以表示成素數之積的形式 :  N=p1^a1*p2^a2*p3^a3...(pi代表素數,ai代表指數) d(n)是n的正因子

UVa10375 選擇與除法唯一分解定理

解析:其中primes是前1000個素數,e陣列存放每個素數對應的指數。 程式碼例項: #include<iostream> #include<cstring> #inc

定理】算術基本定理唯一分解定理

大蒟蒻來水貼了! 算術基本定理(唯一分解定理) 一句話:      任何大於1的自然數,都可以唯一分解成有限個質數的乘積 例如對於大於1的自然數n, 這裡Pi均為質數,其指數ai是正

算術基本定理唯一分解定理 -- 分解素因子

算術基本定理:任何大於1的自然數,都可以唯一分解成有限個質數的乘積:FZU - 1075 這是一道裸題,程式碼可行性不考,反正網上大佬都是這麼敲的。。程式碼實現分解素因子:#include<ios

UVa10375唯一分解定理

例題10-3 選擇與除法(Choose and Divide, UVa10375) 已知C(m,n) = m!/(n!(m-n)!),輸入整數p, q, r, s(p≥q,r≥s,p,q,r,s≤10

LightOJ 1236 Pairs Forming LCM唯一分解定理

題目分析 思路:把n分解成素因數的形式n=p1^c1+p2^c2+…pm^cm 假設已找到一對(a,b)的lcm=n 有a=p1^d1+p2^d2+…pm^dm b=p1^e1