日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

vb 导出整数 科学计数法_可整数组的计数

發(fā)布時間:2025/3/11 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 vb 导出整数 科学计数法_可整数组的计数 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

vb 導出整數(shù) 科學計數(shù)法

Problem statement:

問題陳述:

Given two positive integer n and m, find how many arrays of size n that can be formed such that:

給定兩個正整數(shù)nm ,找出可以形成多少個大小為n的數(shù)組:

  • Each element of the array is in the range [1, m]

    數(shù)組的每個元素都在[1,m]范圍內(nèi)

  • Any adjacent element pair is divisible, i.e., that one of them divides another. Either element A[i] divides A[i + 1] or A[i + 1] divides A[i].

    任何相鄰的元素對都是可分割的 ,即,其中一個元素對另一個元素 。 元素A [i]除以A [i + 1]A [i + 1]除以A [i]

  • Input:

    輸入:

    Only one line with two integer, n & m respectively.

    只有一行包含兩個整數(shù),分別為nm

    Output:

    輸出:

    Print number of different possible ways to create the array. Since the output could be long take modulo 10^9+7.

    打印創(chuàng)建數(shù)組的各種可能方式的數(shù)量。 由于輸出可能很長,取模10 ^ 9 + 7

    Constraints:

    限制條件:

    1<=n, m<=100

    Example:

    例:

    Input: n = 3, m = 2.Output: 8Explanation:{1,1,1},{1, 1, 2}, {1, 2, 1}, {1, 2, 2}, {2, 1, 1},{2,1,2}, {2,2,1}, {2,2,2} are possible arrays.Input: n = 1, m = 5.Output: 5Explanation:{1}, {2}, {3}, {4}, {5}

    Solution Approach:

    解決方法:

    The above problem is a great example of recursion. What can be the recursive function and how we can formulate.

    上面的問題是遞歸的一個很好的例子。 什么是遞歸函數(shù),以及我們?nèi)绾喂交?

    Say,

    說,

    Let

    F(n, m)?= number of ways for array size n and range 1 to m

    F(n,m) =數(shù)組大小為n且范圍為1到m的路徑數(shù)

    Now,

    現(xiàn)在,

    We actually can try picking every element from raging 1 to m and try recurring for other elements

    實際上,我們可以嘗試從1m范圍內(nèi)選取每個元素,然后嘗試對其他元素進行重復

    So, the function can be written like:

    因此,該函數(shù)可以這樣寫:

    Function: NumberofWays(cur_index,lastelement,n,m)

    So, to describe the arguments,

    因此,為了描述這些論點,

    cur_index is your current index and the last element is the previous index value assigned. So basically need to check which value with range 1 to m fits in the cur_index such that the divisibility constraint satisfies.

    cur_index是當前索引,最后一個元素是分配的前一個索引值。 因此,基本上需要檢查范圍在1到m之間的哪個值適合cur_index ,以便除數(shù)約束滿足。

    So, to describe the body of the function

    因此,要描述功能的主體

    Function NumberofWays(cur_index,lastelement,n,m) // formed the array completelyif(cur_index==n)return 1;sum=0// any element in the rangefor j=1 to m // if divisible then lastelement,// j can be adjacent pairif(j%lastelement==0 || lastelement%j==0)// recur for rest of the elmentssum=(sum%MOD+ NumberofWays(cur_index+1,j,n,m)%MOD)%MOD; end ifend for End function

    Now the above recursive function generates many overlapping sub-problem and that's why we use the top-down DP approach to store already computed sub-problem results.
    Below is the implementation with adding memoization.

    現(xiàn)在,上面的遞歸函數(shù)會生成許多重疊的子問題,這就是為什么我們使用自上而下的DP方法來存儲已經(jīng)計算出的子問題結(jié)果的原因。
    下面是添加備忘錄的實現(xiàn)。

    Initiate a 2D DP array with -1

    使用-1啟動2D DP陣列

    Function NumberofWays(cur_index,lastelement,n,m)// formed the array completelyif(cur_index==n)return 1;// if solution to sub problem already exitsif(dp[cur_index][lastelement]!=-1) return dpdp[cur_index][lastelement]; sum=0for j=1 to m // any element in the range// if divisible then lastelement,j can be adjacent pairif(j%lastelement==0 || lastelement%j==0)// recur for rest of the elmentssum=(sum%MOD+ NumberofWays(cur_index+1,j,n,m)%MOD)%MOD; end ifend forDp[curindex][lastelement]=sumReturn Dp[curindex][lastelement] End function

    C++ Implementation:

    C ++實現(xiàn):

    #include <bits/stdc++.h> using namespace std;#define MOD 1000000007int dp[101][101];int countarray(int index, int i, int n, int m) {// if solution to sub problem already exitsif (dp[index][i] != -1)return dp[index][i];if (index == n)return 1;int sum = 0;//any element in the rangefor (int j = 1; j <= m; j++) {// if divisible then i,j can be adjacent pairif (j % i == 0 || i % j == 0) {// recur for rest of the elmentssum = (sum % MOD + countarray(index + 1, j, n, m) % MOD) % MOD;}}dp[index][i] = sum;return dp[index][i]; }int main() {int n, m;cout << "Enter value of n:\n";cin >> n;cout << "Enter value of m:\n";cin >> m;// initialize DP matrixfor (int i = 0; i <= n; i++) {for (int j = 0; j <= m; j++) {dp[i][j] = -1;}}cout << "number of ways are: " << countarray(0, 1, n, m) << endl;return 0; }

    Output:

    輸出:

    Enter value of n: 3 Enter value of m: 2 number of ways are: 8

    翻譯自: https://www.includehelp.com/icp/count-of-divisible-array.aspx

    vb 導出整數(shù) 科學計數(shù)法

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的vb 导出整数 科学计数法_可整数组的计数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。