vb 导出整数 科学计数法_可整数组的计数
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ù)n和m ,找出可以形成多少個大小為n的數(shù)組:
Each element of the array is in the range [1, m]
數(shù)組的每個元素都在[1,m]范圍內
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ù),分別為n和m 。
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<=100Example:
例:
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ù),以及我們如何公式化。
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
實際上,我們可以嘗試從1到m范圍內選取每個元素,然后嘗試對其他元素進行重復
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 functionNow 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方法來存儲已經計算出的子問題結果的原因。
下面是添加備忘錄的實現(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 functionC++ 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)金大獎總結
以上是生活随笔為你收集整理的vb 导出整数 科学计数法_可整数组的计数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php框架laravel_Laravel
- 下一篇: Java BigDecimal nega