我是個對電腦科學有興趣的學生,我會貼上我的學習歷程及生活心情,也請大大們多多指教。 :)

2015年6月17日 星期三

[UVa] 10325 - The Lottery

題目網址: https://goo.gl/UwJqTP

題意:
(from luckycat)


解法:
排容原理,如 http://mobcs.blogspot.tw/2015/04/uva-10179-irreducable-basic-fractions.html
但是需要搭配最小公倍數 LCM。

TAG: Math

注意:

程式碼:

2 則留言:

  1. there is a very easy process.apply inclusion-exclusion principle & then backtrack.your code is so complicated.
    see my 10 line code.
    #include
    #define ll long long
    using namespace std;
    int ara[15];
    ll N,m;
    ll LCM(ll a,ll b)
    {
    return a/__gcd(a,b)*b;
    }
    ll backtrack(ll n,ll div)
    {
    if(n==m) return (N/div);
    return backtrack(n+1,div)-backtrack(n+1,LCM(div,ara[n]));
    }
    int main()
    {
    while(cin>>N>>m)
    {
    for(int i=0;i>ara[i];
    cout<<backtrack(0,1)<<endl;
    }
    return 0;
    }

    回覆刪除
    回覆
    1. Hi Khan Hasan:

      Thank you for your comment!
      Backtrack seems a better way to do inclusion-exclusion.
      I'll try it next time.
      But your code is incomplete in the comment.

      刪除

任何意見都樂意傾聽