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 ...
随机推荐
- python数据处理技巧一
字符串赋值(传参)技巧 Python中一般的字符串赋值的方式如下: variable = "Test" print "I just [%s] unit"%var ...
- django项目所遇问题总结
2. 关于设置static静态文件,样式失效问题 原因: 可能开启多个端口号,页面显示访问的不是已经设置了static的模板,所以,样式没有显示 3. models模型中gender字段的选择设置 c ...
- Flask--Python中常用的Web框架之一
Web框架 什么是框架? 协助开发者快速开发web应程序的一套功能代码 开发者只需要按照框架约定要求,在指定位置写上自己的业务逻辑代码即可 为什么要用web框架? 使用web框架的主要目的就是避免重复 ...
- .js控制一次加载一张图片,加载完后再加载下一张
js怎么控制一次加载一张图片,加载完后再加载下一张 (1)方法1 (1)方法2
- [Python] Handle Exceptions to prevent crashes in Python
Exceptions cause your application to crash. Handling them allows you to recover gracefully and keep ...
- RadioButton的check改变的时候
https://stackoverflow.com/questions/8095256/asp-net-radio-button-change You'll need to specify the a ...
- 利用日志文件恢复MYSQL数据库
利用日志文件恢复MYSQL数据库 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...
- Mvc异步
<h3>MVC 自带的yibu请求</h3> <%-- 1.要执行的方法,2.控制器,3.Ajax操作--%> <%using (Ajax.BeginForm ...
- 初识Oracle中的正则表达式
Oracle使用正则表达式离不开这4个函数: 1.regexp_like 2.regexp_substr 3.regexp_instr 4.regexp_replace
- 【2017 Multi-University Training Contest - Team 2】 Is Derek lying?
[Link]: [Description] 两个人都做了完全一样的n道选择题,每道题都只有'A','B','C' 三个选项,,每道题答对的话得1分,答错不得分也不扣分,告诉你两个人全部n道题各自选的是 ...