题目链接 : http://acm.tju.edu.cn/toj/showp3990.html

Coach Yu has lots of bags, every bag has its number. And what is more, he wants to send these bags to acm team-member. But he requires that if you get the bag and its number is b, you need to count how many number smaller than b and co-prime with b. If you can’t count it, he won’t send the bag to you.
However, G.D.R and Hacb learn the The Euler function to solve the problem. And they are so friendly that they like to share the function to you, so you can get the bag.
The Euler function is:
F(b) = b *( 1 - 1/p1) ( 1 - 1/p2)……(1-1/pn)
Notes: pi is the ith Prime factors of b, and there will be no such i and j which i<>j but pi=pj.
………….

  这道题的题目只有短短的几行,意思也很简单,就是求欧拉函数的值,算法的思想就是求出质因数然后算一下即可,但写起来却没有想象的那么简单,wa、超时各种错误,这道题写了好久,真的是短小精悍啊,也可以看出自己的算法和编程基础还是不过关。按照惯例先反思一下自己犯过的错误吧

  • 首先超时两次,自己竟然无知到对一个10^12大小的数枚举所有的质因子,并且还是在while循环里边,也就是每输入一次样例,都要重复的去判断是否为质数,以及求解质因数,感觉自己脑子瓦塔拉,预先将质数存入一个数组然后去算不就行了嘛,对于n以内的质数,下面这种方法效率貌似比枚举要高一些:
int enumPrime(int n) //将n以内的质数存入一个数组
{int p=0;for(int i=0;i<=n;i++){is_prime[i]=true;}is_prime[0]=is_prime[1]=false;for(int i=2;i<=n;i++){if(is_prime[i]){prime[p++]=i;for(int j=2*i;j<=n;j+=i){is_prime[j]=false;}}}return p;
}

另外还有一个小知识点就是10^12大小的数只需要找1e6范围内的指数即可,这个为什么我也不太懂,数很大时素数很稀疏?1e6之外只有它本身?

  • 还有一点,是一个数学素养方面的问题,自己不注意WA了好多次,这道题要求欧拉函数,也就是求(1-1/p)形式的积,但是1/p是个小数啊,所以不能直接这么算,用ans/p就好了,哎,真的是细节决定成败!
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int N=1e6+1;
bool is_prime[N];
int prime[N];
using namespace std;
int  sieve (int n)
{int p=0;for(int i=0;i<=n;i++){is_prime[i]=true;}is_prime[0]=is_prime[1]=false;for(int i=2;i<=n;i++){if(is_prime[i]){prime[p++]=i;for(int j=2*i;j<=n;j+=i){is_prime[j]=false;}}}return p;
}int main()
{long long b;int maxnum=1e6;int num = sieve(maxnum);while(~scanf("%lld",&b)&&b!=0){if(b==1){printf("0\n");continue;}long long euler=b;for(int i=0;i<num && prime[i]<=b;i++){if(b%prime[i]==0){euler=euler*(prime[i]-1)/prime[i];while(b%prime[i]==0)b=b/prime[i];}}if(b>1)euler=euler/b*(b-1);  printf("%lld\n",euler);}
}

上面这种解法就是从质因数的定义出发,通过找出一个合数的所有素数因子来求解质因子,实际上根本不需要求素数就可以求出质因子,像下面这样即可:

int enumprimefactor(int n){int temp=n,num=0;for(int i=2;i*i<=n;i++){if(temp%i==0)primefactor[num++]=i;while(temp%i==0)temp/=i;}if(temp>1)primefactor[num++]=temp;return num;
}
  • 因此这道题用上面的方法很容易就可解出来了,哎,想起了一句话:别人传说我20。
#include<cstdio>
using namespace std;
int main()
{long long  n;while(scanf("%lld",&n) && n){long long  temp=n;long long  ans=n;for(long long i=2;i*i<=temp;i++){if(temp%i==0){ans=ans-ans/i;  while(temp%i==0){temp=temp/i;    }}}if(temp>1)ans=ans-ans/temp;printf("%lld\n",ans);}return 0;
}

TOJ 3990.I guess the gift is a bag! II(质因数、欧拉函数好题)相关推荐

  1. T^TOJ - 1251 - 。◕‿◕。TMD - 欧拉函数 - 质因数分解

    http://www.fjutacm.com/Problem.jsp?pid=1251 想了很久,一开始居然还直接枚举因子d,计算重复了. 首先你要找与n的最大公因子大于m的x的个数. \[\sum\ ...

  2. ssl提高组周六备考赛【2018.10.27】

    前言 高三dalao试图混入其中 成绩 RankRankRank PersonPersonPerson ScoreScoreScore AAA BBB CCC 111 2017myself2017my ...

  3. Competitive Programming 3题解

    题目一览: Competitive Programming 3: The New Lower Bound of Programming Contests(1) Competitive Programm ...

  4. 欧拉定理、快速幂与逆元

    Color the necklace 时间限制:2000 ms  |  内存限制:65535 KB 难度:0 描述 As we all know, girls love necklaces, espe ...

  5. [HNCTF]crypto-wps

    文章目录 0x01概况 0x02题解 WEEK1 baBAbaseSEse A dictator littleprince baby_rsa 你想学密码吗? XXXOOORRR 爱妃 WEEK2 li ...

  6. 2021宁波市第四届网络安全大赛(练习平台)RSA部分

    挺久没有做过CTF了,明天就要打比赛了,临时抱佛脚,撸两道rsa,找找手感. 公钥文件泄露 题目给了两个文件:pub.key和flag.enc: ​ pub.key -----BEGIN PUBLIC ...

  7. Farey sequences

    n阶的法里数列是0和1之间最简分数的数列,由小至大排列,每个分数的分母不大于n. Stern-Brocot树(SB Tree)可以生成这个序列 {0/1,1/1} {0/1,1/2,1/1} {0/1 ...

  8. Team Silver_Bullet 训练记录

    Team members Vec Grunt henryrabbit To do list team:起队名 每周组队训练 补题 Vec: Grunt:切题,切题,切更多的题!做好切所有类型题的觉悟! ...

  9. 刷题常用模板 by flytosky2015

    声明模板: #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath& ...

最新文章

  1. [独家]网易遭遇****** 留下“装B”两字
  2. 项目经理升职了是啥_什么是升职率?
  3. python运行原理_Python线程池及其原理和使用(超级详细)
  4. [看书笔记]《深入java虚拟机》——java体系结构(二)
  5. 记录一次空指针异常(NullPointerException)的断点调试
  6. html 隐藏_HTML实战篇:纯css制作二级横向以及竖向菜单导航
  7. 令人期待的php7.4,PHP7.4新特性
  8. The prefix p for attribute p:message associated with an element type bean
  9. 设置finder窗口大小的5个小技巧!速看?
  10. 【Shiro第八篇】SpringBoot + Shiro使用Shiro标签
  11. Docker环境调优
  12. Graph Anomaly Detection with Deep Learning——基于属性图的节点异常检测
  13. 《个人信息安全规范》会让我们的信息更安全吗?
  14. 纸飞机飞行曲线matlab,从小到大只会做个纸飞机?关于折纸的「高端」技巧通通告诉你...
  15. JAVA基础篇-数据依赖性含义
  16. linux系统uuid 一样,linux系统product_uuid和product_serial有何区别
  17. HC-05-USB蓝牙模块绑定唯一的蓝牙模块
  18. Spartan6系列之时钟资源详解
  19. python的coupon_Python数据分析:拼多多优惠券使用预测
  20. Ubuntu Frp内网穿透+Samba 445端口

热门文章

  1. 掌握这两款软件,再无C盘不足烦恼
  2. 暴力破解时常说的万能密码是什么
  3. php程序yii是什么意思,Yii框架是什么
  4. 山海演武传·黄道·第一卷 雏龙惊蛰 第十章 天下英雄在谱中(上)
  5. 骡马视频,神马视频,带TV,带影院。不用数据库的搭建教程
  6. cobbler 一键自动化安装系统
  7. Unity3D学习之旅5-RPG游戏-更新与踩坑实录
  8. 阶梯网络Ladder Network
  9. 麻将胡牌算番-台州麻将
  10. 关于U盘启动操作系统《30天自制操作系统》