题目链接:

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 (数论)的更多相关文章

  1. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  2. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  3. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  4. 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 ...

  5. project euler 169

    project euler 169 题目链接:https://projecteuler.net/problem=169 参考题解:http://tieba.baidu.com/p/2738022069 ...

  6. 【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 × ...

  7. Project Euler 第一题效率分析

    Project Euler: 欧拉计划是一系列挑战数学或者计算机编程问题,解决这些问题需要的不仅仅是数学功底. 启动这一项目的目的在于,为乐于探索的人提供一个钻研其他领域并且学习新知识的平台,将这一平 ...

  8. Python练习题 049:Project Euler 022:姓名分值

    本题来自 Project Euler 第22题:https://projecteuler.net/problem=22 ''' Project Euler: Problem 22: Names sco ...

  9. Python练习题 048:Project Euler 021:10000以内所有亲和数之和

    本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable ...

随机推荐

  1. Innodb锁的类型

    Innodb锁的类型 行锁(record lock) 行锁总是对索引上锁,如果某个表没有定义索引,mysql就会使用默认创建的聚集索引,行锁有S锁和X锁两种类型. 共享锁和排它锁 Innodb锁有两种 ...

  2. 小米开源便签Notes-源码研究(1)-导出功能整体思路

    NotesListActivity是入口Activity. 响应菜单事件,我的手机是"左键菜单".如果菜单项的ID是"R.id.menu_export_text" ...

  3. 【Henu ACM Round#17 B】USB Flash Drives

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 排序,逆序.贪心选较大的就好. [代码] #include <bits/stdc++.h> #define ll lon ...

  4. C#实现窗口拖动时各个控件同比自己主动放缩大小

    实现方式主要是利用panel控件为主题.对于每一个控件的大小位置和字体这几个属性进行记录. 然后依据窗口改变的大小同一时候放缩. 简要过程例如以下: 1 创建C#窗口程序项目. 2  Panel放置到 ...

  5. Kinect开发 —— 基础知识

    转自:http://www.cnblogs.com/yangecnu/archive/2012/04/02/KinectSDK_Application_Fundamentals_Part2.html ...

  6. HDU 2633 Getting Driving License(模拟)

    Getting Driving License Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  7. Markdown最简单常用的语法

    1,文本强调: 文本倾斜,*我是倾斜的文本* 文本加粗,**我是加粗的文本** 文本删除线,~~带删除线的文本~~ 2,链接,分为行内式与参数式,参数式多用于在文章中多次使用相同的链接 行内式写法:[ ...

  8. Nim游戏算法实现

  9. 47.Android 自己定义PopupWindow技巧

    47.Android 自己定义PopupWindow技巧 Android 自己定义PopupWindow技巧 前言 PopupWindow的宽高 PopupWindow定位在下左位置 PopupWin ...

  10. crmjs区分窗口是否是高速编辑

    有时候,我们须要区分打开的窗口是否是高速编辑页面,在上面做一些逻辑处理: 窗口上面附加的js代码: function loadFrom() {     var formType = Xrm.Page. ...