CodeForce 236B Easy Number Challenge(筛法求素数 + 整数因式分解)
生活随笔
收集整理的這篇文章主要介紹了
CodeForce 236B Easy Number Challenge(筛法求素数 + 整数因式分解)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目鏈接:http://codeforces.com/problemset/problem/236/B Easy Number Challenge time limit per test 2 seconds memory limit per test 256 megabytes
題意:首先定義d(x)表示x的因子個數(shù)。然后給出三個正整數(shù)a、b、c,求 #include <cstdio> #include <iostream> #include <cstring> #include <set> #include <cmath> using namespace std; typedef long long LL; const int Mod = 1073741824; int prime[1000000], vis[10000050];int pri_cnt; void get_prime() { // 篩法求素數(shù)int m = (int)sqrt(100000 + 1);pri_cnt = 0;memset(vis, 0, sizeof(vis));vis[0] = 1;vis[1] = 1;for(int i = 2; i <= m; ++i) {if(!vis[i]) {prime[pri_cnt++] = i;for(int j = i * i; j <= 1000005;j += i)vis[j] = 1;}} }int get_cnt(int x) { // 求x的因數(shù)有多少個if(x == 1) return 1;if(!vis[x]) return 2;int ans = 1;for(int i = 0; x != 1 && i < pri_cnt; ++i) {int cnt = 0;while(x % prime[i] == 0) {cnt++;x /= prime[i];}ans = ans * (cnt + 1);}return ans; }int main() {get_prime();int a, b, c;while(~scanf("%d%d%d", &a, &b, &c)) {int ans = 0;for(int i = 1; i <= a; ++i) {for(int j = 1; j <= b; ++j) {for(int k = 1; k <= c; ++k) {ans = (ans + get_cnt(i * j * k)) % Mod;}}}printf("%d\n", ans);}return 0; }
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
Let's denote?d(n)?as the number of divisors of a positive integer?n. You are given three integers?a,?b?and?c. Your task is to calculate the following sum:
Find the sum modulo?1073741824?(230).
InputThe first line contains three space-separated integers?a,?b?and?c?(1?≤?a,?b,?c?≤?100).
OutputPrint a single integer — the required sum modulo?1073741824?(230).
Examples input 2 2 2 output 20 input 5 6 7 output 1520 NoteFor the first example.
- d(1·1·1)?=?d(1)?=?1;
- d(1·1·2)?=?d(2)?=?2;
- d(1·2·1)?=?d(2)?=?2;
- d(1·2·2)?=?d(4)?=?3;
- d(2·1·1)?=?d(2)?=?2;
- d(2·1·2)?=?d(4)?=?3;
- d(2·2·1)?=?d(4)?=?3;
- d(2·2·2)?=?d(8)?=?4.
So the result is?1?+?2?+?2?+?3?+?2?+?3?+?3?+?4?=?20.
題意:首先定義d(x)表示x的因子個數(shù)。然后給出三個正整數(shù)a、b、c,求 #include <cstdio> #include <iostream> #include <cstring> #include <set> #include <cmath> using namespace std; typedef long long LL; const int Mod = 1073741824; int prime[1000000], vis[10000050];int pri_cnt; void get_prime() { // 篩法求素數(shù)int m = (int)sqrt(100000 + 1);pri_cnt = 0;memset(vis, 0, sizeof(vis));vis[0] = 1;vis[1] = 1;for(int i = 2; i <= m; ++i) {if(!vis[i]) {prime[pri_cnt++] = i;for(int j = i * i; j <= 1000005;j += i)vis[j] = 1;}} }int get_cnt(int x) { // 求x的因數(shù)有多少個if(x == 1) return 1;if(!vis[x]) return 2;int ans = 1;for(int i = 0; x != 1 && i < pri_cnt; ++i) {int cnt = 0;while(x % prime[i] == 0) {cnt++;x /= prime[i];}ans = ans * (cnt + 1);}return ans; }int main() {get_prime();int a, b, c;while(~scanf("%d%d%d", &a, &b, &c)) {int ans = 0;for(int i = 1; i <= a; ++i) {for(int j = 1; j <= b; ++j) {for(int k = 1; k <= c; ++k) {ans = (ans + get_cnt(i * j * k)) % Mod;}}}printf("%d\n", ans);}return 0; }
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的CodeForce 236B Easy Number Challenge(筛法求素数 + 整数因式分解)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForce 237C Prime
- 下一篇: 手把手带你手写SpringMVC,剑指优