Project Euler 516 5-smooth totients (数论)
题目链接:
https://projecteuler.net/problem=516
题目:
\(5\)-smooth numbers are numbers whose largest prime factor doesn't exceed \(5\).
\(5\)-smooth numbers are also called Hamming numbers.
Let S(L) be the sum of the numbers \(n\) not exceeding \(L\) such that Euler's totient function \(φ(n)\) is a Hamming number.
S(\(100\))=\(3728\).
Find S(\(10^{12}\)). Give your answer modulo \(2^{32}\).
题解:
因为:
如果 \(n\) 是质数,则\(φ(n)=n-1\)
如果 \(n\) 是质数,则\(φ(n^k) = n^k *(n-1)\)
如果 \(x\) 和 \(y\) 互质,则\(φ(xy) = φ(x) * φ(y)\)
而且,\(5\)-smooth number 可以表示成 \(2^a 3^b 5^c\)
那么我们可以容易得到:
题目要求的就是:\(n = 2^a 3^b 5^c \cdot p_1 \cdot p_2 \cdot \dotso \cdot p_k\)。
其中,\(p_i = 2^{a_i} 3^{b_i} 5^{c_i} + 1\)。
直接预处理所有 \(10^{12}\) 内的 \(p_i\),然后暴搜所有可能的乘积。
把这些可能的结果相加起来就是答案。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e8;
const int mod = 1e9;
const ll limit = 1e12;
int isprime(ll x)
{
if(x<=1)return 0;
for(ll i = 2;i * i <= x; i++) {
if(x % i == 0) {
return 0;
}
}
return 1;
}
ll ans = 0;
std::vector<ll> v;
// n = 2^a * 3^b * 5^c * p_1 *p_2*...*p_k
// where p_i = 2^a_i * 3^b_i * 5^ c_i + 1
//generate all the possible products and sum them
void dfs(ll id,ll now,ll upper)
{
// (now) value is a part of products
if(v[id] > upper || id == v.size()) {
// std::cout << "now="<< now << '\n';
//ll sum = 0;
// if(lim==1e12) {
// std::cout << "id=" << id << '\n'; // 546
// }
for(ll x = now ; x <= limit ; x *= 2LL) {
for(ll y = 1 ; x*y <= limit ; y *= 3LL) {
for(ll z = 1 ; x*y*z <= limit; z *= 5LL) {
ans += (int)(x*y*z); //a little bug , need to change ll into interger
// sum += (int)(x*y*z);
// std::cout << "cal=" << (x*y*z) << '\n';
}
}
}
// std::cout <<"sum=" << sum << '\n';
return;
}
dfs(id + 1, now, upper);
dfs(id + 1, now * v[id], upper / v[id]);
}
int main(int argc, char const *argv[]) {
for(ll x = 1; x <= limit; x *= 2LL) {
for(ll y = 1; x*y <= limit; y *= 3LL) {
for(ll z = 1 ; x*y*z <= limit; z *= 5LL) {
// if n is a prime, call it "good" prime
// phi(n) = n - 1 = 2 ^ k_1 * 3^k_2 * 5^k_3
// ==> n = 2 ^ k_1 * 3^k_2 * 5^k_3 + 1
if(isprime(x*y*z + 1)) {
// 2 ^ k_1 * 3^k_2 * 5^k_3 + 1
v.push_back(1LL*(x*y*z + 1));
}
}
}
}
sort(v.begin(),v.end());
// std::cout << "size=" << v.size() << '\n';
// std::cout << "finish!" << '\n';
// std::cout << '\n';
// 3LL means that Skip 2, 3, 5
dfs(3LL,1LL,limit);
ans = ans % (1LL<<32);
std::cout << ans << '\n';
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
return 0;
}
Project Euler 516 5-smooth totients (数论)的更多相关文章
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- project euler 169
project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...
- 【Project Euler 8】Largest product in a series
题目要求是: The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × ...
- Project Euler 第一题效率分析
Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...
- Python练习题 049:Project Euler 022:姓名分值
本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...
- Python练习题 048:Project Euler 021:10000以内所有亲和数之和
本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...
随机推荐
- PHP抓取网页内容的几种方法
方法1: 用file_get_contents 以get方式获取内容 <?php $url='http://www.domain.com/?para=123'; $html = file_get ...
- sed的一些tricks
1.sed -f xx.sed input_file 可以将一系列操作放在一个xx.sed脚本里执行 ``` #!/bin/sed -f ``` 2.在匹配字符串后面或行尾添加内容 在text后面添加 ...
- 深入理解Core Data
留给我这忘事精看 Core Data 是什么? 大概八年前,2005年的四月份,Apple 公布了 OS X 10.4,正是在这个版本号中 Core Data 框架公布了.那个时候 YouTube 也 ...
- 关于getinstalledpackages參数的分析。
此blog不写API的使用方法仅仅分析此參数的知识点. 今天学习安卓突然学习到了getinstalledpackages()的方法获取到安装应用信息 ,他接收一个int flags的值.然后在网上查询 ...
- 转战Androidstudio之项目eclipse迁移
新项目開始,决定转战as战场,(是应为听说了太多关于as的夸赞我才来的),期间各种不爽不适应历历在目啊.闲话少说,项目迁移開始 1.Eclipse迁出 当然直接从Eclipse迁出是有条件的,ADT必 ...
- hdoj-1593-find a way to escape【数学题】
find a way to escape Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 数据持久化(六)之Using CoreData with MagicalRecord
第五节里面,我介绍了CoreData的配置和主要的增删改查,可能非常多人会认为用它真繁琐.这里,我再介绍网上大神对它进行了人性化封装的第三方MagicalRecord,正如FMDB对sqlite进行了 ...
- php实现简单算法3
php实现简单算法3 这篇文章主要介绍了PHP经典算法集锦,整理了各种常见的算法,包括排序.查找.遍历.运算等各种常见算法原理与实现技巧,需要的朋友可以参考下 1.首先来画个菱形玩玩,很多人学C时在书 ...
- 42.cnpm不是内部命令的解决方案:配置环境变量
转自:https://blog.csdn.net/u014540814/article/details/78777961
- 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)
A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...