【Luogu】【关卡2-11】简单数学问题(2017年10月)【还差三道题】

火星人  

麦森数  

P1403 [AHOI2005]约数研究

f(n)表示n的约数个数,现在给出n,要求求出f(1)到f(n)的总和。

解答:有几个1做约数的个数 = n /1; 有几个2做约数的个数 = n /2; 有几个3做约数的个数 = n /3;

所以直接 对 n / i 求和就是答案。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cmath>
 5 using namespace std;
 6 
 7 int n;
 8 int main () {
 9     cin >> n;
10 
11     int  s =0;
12     for (int i = 1; i <= n; ++i) {
13         s += n / i;
14     }
15     cout << s << endl;
16 
17     return 0;
18 }
View Code

 

进制转换  

 

P1147 连续自然数和

对一个给定的自然数M,求出所有的连续的自然数段,这些连续的自然数段中的全部数之和为M。

例子:1998+1999+2000+2001+2002 = 10000,所以从1998到2002的一个自然数段为M=10000的一个解。

解答:直接枚举暴力能过...

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <vector>
 5 #include <cstring>
 6 #include <map>
 7 #include <climits>
 8 #include <algorithm>
 9 #include <cmath>
10 #include <sstream>
11 
12 using namespace std;
13 
14 int main() {
15 
16     int M, ans = 0;
17     cin >> M;
18     for (int first = 0; first <= M / 2; ++first) {
19         for (int second = first; second <= M; ++second) {
20             ans += second;
21             if (ans > M) {
22                 ans = 0;
23                 break;
24             }
25             if (ans == M) {
26                 cout << first << " " << second << endl;
27                 ans = 0;
28                 break;
29             }
30         }
31         ans = 0;
32     }
33     return 0;
34 }
View Code

 

P1029 最大公约数和最小公倍数问题

给出两个正整数的最大公约数x和最小公倍数y,求满足这样条件的正整数的对数。

解答:假设这两个正整数是p,q; 最大公约数,最小公倍数是x,y。那么有这么一条浅显的道理: p * q =  x * y

枚举p,用p计算q, 然后计算gcd是不是x。 gcd怎么写---必会。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int gcd(int small, int big) {
 5     if (small == 0) return big;
 6     return gcd(big % small, small);
 7 }
 8 
 9 int main() {
10     int x, y;
11     cin >> x >> y;
12     int ans = 0;
13     for (int i = x; i * i < x * y; ++i) {
14         int p, q;
15         if (y % i != 0) {
16             continue;
17         }
18         p = i, q = x * y / i;
19         //printf("p = %d, q = %d gcd[%d]\n", p ,q, gcd(p, q));
20         if (gcd(p, q) == x) {
21             ++ans;
22         }
23     }
24     cout << ans * 2 << endl;
25     return 0;
26 }
View Code

 

转载于:https://www.cnblogs.com/zhangwanying/p/7637018.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值