Aria's Loops

https://www.hackerrank.com/contests/101hack41/challenges/arias-loops

可以看我以前的笔记,http://www.cnblogs.com/liuweimingcprogram/p/6091396.html

或者我在discusses的题解

I have my thought in this problem The first thing in the editorial is this problem problem:

count how many hello world

  for (int i = 1; i <= n; ++i)   

    for (int j = i + 1; j <= n; ++j)     

      for (int k = j + 1; k <= n; ++k)       

        cout << "hello world" << endl;

observe i < j < k <= n. so this ans is from 1....n select 3 different number,build a triple.The ans will be C(n, 3)

think more deeply, let define xi be the distance between xi and x(i - 1) so x1 >= 1 because the number start from one.and then x2 >= 1 && x3 >= 1 because they can not be the same. so the ans is the ans of how many solution of x1 + x2 + x3 <= n this is a very classics problem.Go to the internet searching.

and now this problem can be solve using the second method x1 >= 1 && x2 >= 1 && x3 >= 2 && x4 >= 3 ...... so the ans is C(n - 1 - (k * k - 3 * k) / 2, k); may be my English is very poor so thay you can not understand. I feel so sorry

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
#define MY "H:/CodeBlocks/project/CompareTwoFile/DataMy.txt", "w", stdout
#define ANS "H:/CodeBlocks/project/CompareTwoFile/DataAns.txt", "w", stdout


#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
LL n, k;
const int MOD = 1e9 + 7;
LL quick_pow (LL a,LL b,LL MOD) {
    //求解 a^b%MOD的值
    LL base=a%MOD;
    LL ans=1; //相乘,所以这里是1
    while (b) {
        if (b&1) {
            ans=(ans*base)%MOD; //如果这里是很大的数据,就要用quick_mul
        }
        base=(base*base)%MOD;    //notice
        //注意这里,每次的base是自己base倍
        b>>=1;
    }
    return ans;
}

LL C (LL n,LL m,LL MOD) {
    if (n<m) return 0; //防止sb地在循环,在lucas的时候
    if (n==m) return 1;
    LL ans1 = 1;
    LL ans2 = 1;
    LL mx=max(n-m,m); //这个也是必要的。能约就约最大的那个
    LL mi=n-mx;
    for (int i = 1; i <= mi; ++i) {
        ans1 = ans1*(mx+i)%MOD;
        ans2 = ans2*i%MOD;
    }
    return (ans1*quick_pow(ans2,MOD-2,MOD)%MOD); //这里放到最后进行,不然会很慢
}
LL Lucas (LL n,LL m,LL MOD) {
    LL ans=1;
    while (n && m && ans) {
        ans=ans*C(n%MOD,m%MOD,MOD)%MOD;
        n /= MOD;
        m /= MOD;
    }
    return ans;
}


void work() {
    cin >> n >> k;
    LL N = n - 1 - ((k * k - 3 * k) / 2);
    if (N < 0) cout << 0 << endl;
    else cout << Lucas(N, k, MOD) << endl;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    work();
    return 0;
}
View Code

我知道这里不用lucas,但是自己的模板,一复制就是这样,而且这样用lucas复杂度不会增加。

原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6092691.html