寻找i*j=m的个数
2024-09-26 23:31:30
问题描述
3*3的矩阵内容。
1 2 3
2 4 6
3 6 9
即a[i][j](1<=i<=n,1<=j<=n)=i*j。
问一个这样n*n的矩阵里面,里面m出现的次数。
例如n为3,m为6.
那么出现的次数就是2
Input
输入正整数N,表示N例测试(N<=20)。接着输入n(n<=10^5),m(<=10^9)。
Output
对每组输入数据,输出m出现的次数。
Sample Input
2
3 6
3 3
Sample Output
2
2
#include <iostream>
#define N 100000
using namespace std; int main()
{
int t;
cin >> t;
while(t--){
int n;
cin >> n;
int m;
cin >> m;
int cnt=;
for(int i=;i<=n;i++){
if(m%i== && m/i<=n)
cnt++;
}
cout << cnt << endl;
}
return ;
}
P.S.很多人把这个题想的太复杂。如果矩阵的每个数都要看,时间复杂度是10^10*2*10,肯定会超时。
我们仔细想下这个题目,每行最多只有一个数。如果第i行。如果能找到i*j==m并且j<=n,那么就可以。
每行判断m%i是否为0,m/i就是j如果<=n,那么那个数就是m。cnt++。
最新文章
- angular2系列教程(四)Attribute directives
- 数组中pop()和reverse()方法调用
- actor concurrency
- java selenium针对多种情况的多窗口切换
- HDU 3966 Aragorn&#39;s Story 树链剖分+树状数组 或 树链剖分+线段树
- vijos2001 xor-sigma
- cos
- 关于Windows下mysql忘记root密码的解决方法
- (二)Knockout - ViewModel 的使用
- 一种解决h5页面背景音乐不能自动播放的方案
- Java synchronized 关键字的实现原理
- Python-MongoDB的驱动安装、升级
- spring-cloud-sleuth 和 分布式链路跟踪系统
- [Linux] Configure iSCSI on Linux5 (both target and initiator)
- Python pyYAML模块
- (lower_bound)find the nth digit hdu1597
- Wilcoxon-Mann-Whitney rank sum test
- 修改MyEclipse取消默认工作空间
- JQuery为textarea添加maxlength
- RecyclerView 作为聊天界面,被键盘遮挡的解决办法
热门文章
- 用matplotlib绘制漫画风格的图表
- 经验分享:如何系统学习 Web 前端技术?
- Interllij IDEA 使用Git工具
- jQuery插件开发,jquery插件
- 【转载】CoordinatorLayout源码解析
- Codeforces Round #307 (Div. 2) E. GukiZ and GukiZiana(分块)
- Python开发基础-Day4-布尔运算、集合
- codevs1033 蚯蚓的游戏问题 裸最小费用最大流,注意要拆点
- 【线段树+离散化】POJ2528-Mayor&#39;s posters
- bzoj 4627: [BeiJing2016]回转寿司