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

vb 导出整数 科学计数法

Problem statement:

问题陈述:

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

给定两个正整数nm ,找出可以形成多少个大小为n的数组:

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

    数组的每个元素都在[1,m]范围内

  2. 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.

只有一行包含两个整数,分别为nm

Output:

输出:

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

打印创建数组的各种可能方式的数量。 由于输出可能很长,取模10 ^ 9 + 7

Constraints:

限制条件:

1<=n, m<=100

Example:

例:

    Input: 
n = 3, m = 2.
Output: 
8
Explanation:
{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: 
5
Explanation:
{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.

上面的问题是递归的一个很好的例子。 什么是递归函数,以及我们如何公式化。

Say,

说,

Let

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

F(n,m) =数组大小为n且范围为1到m的路径数

Now,

现在,

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

实际上,我们可以尝试从1m范围内选取每个元素,然后尝试对其他元素进行重复

So, the function can be written like:

因此,该函数可以这样写:

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 ,以便除数约束满足。

So, to describe the body of the function

因此,要描述功能的主体

Function NumberofWays(cur_index,lastelement,n,m)    
// formed the array completely
if(cur_index==n)
return 1;
sum=0
// any element in the range
for j=1 to m 
// if divisible then lastelement,
// j can be adjacent pair
if(j%lastelement==0 || lastelement%j==0)
// recur for rest of the elments
sum=(sum%MOD+ NumberofWays(cur_index+1,j,n,m)%MOD)%MOD; 
end if
end 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.

现在,上面的递归函数会生成许多重叠的子问题,这就是为什么我们使用自上而下的DP方法来存储已经计算出的子问题结果的原因。
下面是添加备忘录的实现。

Initiate a 2D DP array with -1

使用-1启动2D DP阵列

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

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
int dp[101][101];
int countarray(int index, int i, int n, int m)
{
// if solution to sub problem already exits
if (dp[index][i] != -1)
return dp[index][i];
if (index == n)
return 1;
int sum = 0;
//any element in the range
for (int j = 1; j <= m; j++) {
// if divisible then i,j can be adjacent pair
if (j % i == 0 || i % j == 0) {
// recur for rest of the elments
sum = (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 matrix
for (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 导出整数 科学计数法

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543817.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C4.5决策树算法概念学习

数据挖掘一般是指从大量的数据中自动搜索隐藏于其中的有着特殊关系性的信息的过程。 •分类和聚类•分类(Classification)就是按照某种标准给对象贴标签&#xff0c;再根据标签来区分归类&#xff0c;类别数不变。•聚类(clustering)是指根据“物以类聚”的原理&#xff0c;将本…

python修改y轴刻度_Python | Y轴刻度限制

python修改y轴刻度In some cases, we need to visualize our data within some defined range rather than the whole data. For this, we generally set the y-axis scale within a limit and this ultimately helps us to visualize better. Sometimes, it acts as zooming a…

em算法示例_带有示例HTML'em'标签

em算法示例<em>标签 (<em> Tag) <em> tag in HTML is used to display the text in emphasized form. <em> tag add semantic meaning to the text, text inside it is treated as emphasized text. HTML中的<em>标记用于以强调形式显示文本。 &…

联合使用 HTML 5、地理定位 API

查找并跟踪位置坐标以用在各种 Web 服务中 在这个由五个部分所组成的系列的第一部分中&#xff0c;您将接触到移动 Web 应用程序中最流行的新技术&#xff1a;地理定位。高端智能手机都内置 GPS&#xff0c;现在您将了解 Web 服务如何使用它。在本文中&#xff0c;您将学到如何…

list下界_下界理论

list下界下界理论 (Lower Bound Theory) Lower bound (L(n)) is a property of the specific problem i.e. the sorting problem, matrix multiplication not of any particular algorithm solving that problem. 下界(L(n))是特定问题(即排序问题)的矩阵&#xff0c;不是解决该…

Mac OSX 安装nvm(node.js版本管理器)

我的系统 1.打开github官网https://github.com/&#xff0c;输入nvm搜索,选择creationix&#xff0f;nvm&#xff0c;打开 2.找到Install script&#xff0c;复制 curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash . 3. 打开终端&#xf…

HTML中的类属性

The class attribute in HTML is used to specify or set a single or multiple class names to an element for an HTML and XHTML elements. Its majorly used to indicate a class in a style sheet. HTML中的class属性用于为HTML和XHTML元素指定或设置一个元素名称或多个类…

关于HTML5标签不兼容(IE6~8)

HTML5的语义化标签以及属性&#xff0c;可以让开发者非常方便地实现清晰的web页面布局&#xff0c;加上CSS3的效果渲染&#xff0c;快速建立丰富灵活的web页面显得非常简单。 比较常用的HTML5的新标签元素有&#xff1a; <header>定义页面或区段的头部&#xff1b;<na…

zzz,zzz,zz9_ZZZ的完整形式是什么?

zzz,zzz,zz9ZZZ&#xff1a;睡觉&#xff0c;无聊&#xff0c;累 (ZZZ: Sleeping, Bored, Tired) ZZZ is an abbreviation of "Sleeping, Bored, Tired". ZZZ是“睡觉&#xff0c;无聊&#xff0c;累了”的缩写 。 It is an expression, which is commonly used in …

uboot启动 及命令分析(3)

u-boot命令 先贴一个重要结构,位于uboot/include/command.h,这个结构代表每个uboot命令 struct cmd_tbl_s { char *name; /* Command Name */ int maxargs; /* maximum number of arguments*/ int repeatable;/* autorepeat allowed? */ /* Implem…

Mozilla开源了VR框架A-Frame

Mozilla创建并开源了A-Frame&#xff0c;这是一个用于在桌面浏览器、智能手机和Oculus Rift上创建VR场景的框架。\\A-Frame是一个在浏览器中创建VR体验的开源框架。该框架由Mozilla的MozVR团队创建和开发。A-Frame使用了一个在游戏开发中经常使用的“实体-组件&#xff08;Enti…

css网格_CSS网格容器

css网格CSS | 网格容器 (CSS | Grid Containers) There are numerous ways to display our list items or elements. For instance, we can display them in the navigation bar, in a menu bar and whatnot. Well, it would be right to say that there are many more such me…

c ++向量库_在C ++中对2D向量进行排序

c 向量库As per as a 2D vector is concerned its a vector of a 1D vector. But what we do in sorting a 1D vector like, 就2D向量而言&#xff0c;它是1D向量的向量 。 但是我们在对一维向量进行排序时所做的工作 sort(vector.begin(),vector.end());We cant do the same …

监听文本框数据修改,特别是微信等客户端直接选择粘贴修改

2019独角兽企业重金招聘Python工程师标准>>> // 手机号码信息加载验证 $(input).bind(input propertychange, function() { initPage.checkName(); }); 转载于:https://my.oschina.net/u/1579617/blog/550488

SSIA的完整形式是什么?

SSIA&#xff1a;主题说明一切 (SSIA: Subject Says It All) SSIA is an abbreviation of "Subject Says It All". SSIA是“主题说明一切”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is written in the subject of the mail…

服务器控件转换成HTML

服务器控件转换成HTML<asp:Label ID"Label1" runat"server" Text"I am label"><asp:Literal ID"Literal1" runat"server" Text"I am a literal"><asp:Panel ID"Panel1" runat"serv…

Eratosthenes筛

什么是Eratosthenes筛&#xff1f; (What is Sieve of Eratosthenes?) Sieve of Eratosthenes is an ancient algorithm of finding prime numbers for any given range. Its actually about maintaining a Boolean table to check for corresponding prime no. Eratosthenes的…

微信iOS多设备多字体适配方案总结

一、背景 2014下半年&#xff0c;微信iOS版先后适配iPad, iPhone6/6plus。随着这些大屏设备的登场&#xff0c;部分用户觉得微信的字体太小&#xff0c;但也有很多用户不喜欢太大的字体。为了满足不同用户的需求&#xff0c;我们做了全局字体设置功能&#xff0c;在【设置-通用…

python矩阵中插入矩阵_Python | 矩阵的痕迹

python矩阵中插入矩阵The sum of diagonal elements of a matrix is commonly known as the trace of the matrix. It is mainly used in eigenvalues and other matrix applications. In this article, we are going to find the trace of a matrix using inbuilt function nu…

TOP命令监视系统任务及掩码umask的作用

top 命令使用方法及參数。 top 选择參数 參数&#xff1a; -b 以批量模式执行。但不能接受命令行输入&#xff1b;-c 显示命令行&#xff0c;而不不过命令名。-d N 显示两次刷新时间的间隔&#xff0c;比方 -d 5&#xff0c;表示两次刷新间隔为5秒&#xff1b;-i 禁止显示空暇…