3990 G. I guess the gift is a bag! II
来源:互联网 发布:淘宝优惠券去哪里领 编辑:程序博客网 时间:2024/05/16 18:03
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.
Input
A number b as describe . 0
#include <cstdio>#include <algorithm>#include <cstring>#include <iostream>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 (void){ int num=1e6; int p=sieve(num); long long b; while(~scanf("%lld",&b)&&b!=0) { if(b==1) { printf("0\n"); continue; } long long ans=b; for(int i=0;i<p&&prime[i]<=b;i++) { if(b%prime[i]==0) { ans=ans/prime[i]*(prime[i]-1); //1-(1/p1)=((p1-1)/p1) while(b%prime[i]==0) { b=b/prime[i];//把1e6之内的先筛掉 } } } if(b>1)//还有一个在1e6之外的数 { ans=ans/b*(b-1); } printf("%lld\n",ans); } return 0;}
0 0
- 3990 G. I guess the gift is a bag! II
- Toj 3990. I guess the gift is a bag! II
- 3985. I guess the gift is a bag!
- TOJ 3990.I guess the gift is a bag! II(质因数、欧拉函数好题)
- 3989. I guess the gift is a calculator! toj
- TOJ 3984 I guess the gift is an ipad!
- Toj 3984. I guess the gift is an ipad!
- life is a gift
- Today is a gift
- The gift is the present
- China Aishang Gift Bag
- Today is a gift , that's why we call it the present!
- SCU 4120 G: Guess The Earliest Time
- [2015-3-25] Everyday is a gift
- I Can Guess the Data Structure! UVA11995
- 11995 - I Can Guess the Data
- UVa11995 I Can Guess the Data Structure!
- UVA11995:I Can Guess the Data Structure
- mongdb 安装
- OpenMP First Demo
- java判断==null和isEmpty的区别
- Google Java编程风格指南
- mysql中limit 和in
- 3990 G. I guess the gift is a bag! II
- Helvetic Coding Contest 2016 C2. Brain Network (medium)
- Shiro权限说明
- Codeforces 429B Working out (dp 数字三角形变形)
- 经典进程的同步问题-生产者与消费者
- 在 Web 项目中应用 Apache Shiro
- SCAU Individual Contest #1 (未更新完)
- Ubuntu下为Firefox安装Adobe Flash Player
- 使用achartengine 画图表时属性设置