2.1.1 最小公倍数
include<iostream>
using namespace std;
int gcd(int x,int y)
{
if (!x || !y) return x>y ? x : y;
for (int t;t=x%y;x=y,y=t);
return y;
}
int main()
{
int a,b;
while (cin>>a>>b)
cout<<a*b/gcd(a,b)<<endl;
return 0;
}
2.1.2 How many prime numbers
Miller-Rabin加二进制优化
#include<iostream>
#define bignum unsigned long long
bignum mulmod(bignum a,bignum b,bignum c)
{
bignum cnt=0,temp=a;
while(b)
{
if(b&1) cnt=(cnt+temp)%c;
temp=(temp+temp)%c;
b>>=1;
}
return cnt;
}
//求a^b%c,再次将b写成二进制形式,例如:3^7=3^1*3^2*3^4;
bignum powmod(bignum a,bignum b,bignum c)
{
bignum cnt=1,temp=a;
while(b)
{
if(b&1) cnt=mulmod(cnt,temp,c);//cnt=(cnt*temp)%c;
temp=mulmod(temp,temp,c);//temp=(temp*temp)%c;
b>>=1;
}
return cnt;
}
//Miller-Rabin测试n是否为素数,1表示为素数,0表示非素数
int pri[10]={2,3,5,7,11,13,17,19,23,29};
bool Miller_Rabin(bignum n)
{
if(n<2) return 0;
if(n==2) return 1;
if(!(n&1)) return 0;
bignum k=0,m;
m=n-1;
while(m%2==0) m>>=1,k++;//n-1=m*2^k
for(int i=0;i<10;i++)
{
if(pri[i]>=n) return 1;
bignum a=powmod(pri[i],m,n);
if(a==1) continue;
int j;
for(j=0;j<k;j++)
{
if(a==n-1) break;
a=mulmod(a,a,n);
}
if(j<k) continue;
return 0;
}
return 1;
}
using namespace std;
int main()
{
int n;
while (cin>>n)
{
int ans=0,x;
for (int i=0;i<n;i++)
{
cin>>x;
if (Miller_Rabin(x)) ans++;
}
cout<<ans<<endl;
}
return 0;
}
2.1.3 Cake
#include<iostream>
using namespace std;
int gcd(int x,int y)
{
if (!x || !y) return x>y ? x : y;
for (int t;t=x%y;x=y,y=t);
return y;
}
int main()
{
int p,q;
while (cin>>p>>q)
{
int ans=0,g;
g=gcd(p,q);
p=p/g; q=q/g;
ans=(p+q-1)*g;
cout<<ans<<endl;
}
return 0;
}
2.1.4 又见GCD
#include<iostream>
using namespace std;
int main()
{
int pri[1000],pn=0;
bool f[1000];
for (int i=0;i<1000;i++) f[i]=true;
for (int i=2;i<1000;i++)
if (f[i])
{
pri[pn++]=i;
for (int j=i+i;j<1000;j+=i) f[j]=false;
}
int n;
cin>>n;
for (int i=0;i<n;i++)
{
int a,b,aa,c;
cin>>a>>b;
aa=a/b;
c=0;
while (aa%pri[c]==0) c++;
c=pri[c]*b;
cout<<c<<endl;
}
return 0;
}
2.1.5 七夕节
现在回顾自己以前的代码才发现真是丑啊,不过还是发以前的,面对真实的自己
include<iostream>
int f[500001];
using namespace std;
int main()
{
int t;
f[1]=0;
for (int i=2;i<=500000;i++) f[i]=1;
for (int i=2;i<=250000;i++)
for (int j=i+i;j<=500000;j+=i)
f[j]+=i;
cin>>t;
for (int tt=0;tt<t;tt++)
{
int n;
cin>>n;
cout<<f[n]<<endl;
}
return 0;
}
2.1.6 找新朋友
#include<iostream>
using namespace std;
int f[32768];
bool q[32768];
int main()
{
for (int i=0;i<32768;i++) {f[i]=i; q[i]=true;}
for (int i=2;i<32768;i++)
if (q[i])
for (int j=i;j<32768;j+=i)
{
f[j]=f[j]*(i-1)/i;
q[j]=false;
}
int t;
cin>>t;
for (int tt=0;tt<t;tt++)
{
int n;
cin>>n;
cout<<f[n]<<endl;
}
return 0;
}
2.1.7 The area
虽然我高数很挫,不过这种还是没问题的
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
double x1,x2,x3,y1,y2,y3;
double s,h,k,a;
cout<<fixed<<setprecision(2);
cin>>n;
while(n--)
{
cin>>x1>>y1>>x2>>y2>>x3>>y3;
h = x1; k = y1;
a = (y2-k)/((x2-h)*(x2-h));
s = (a*(x3-h)*(x3-h)*(x3-h))/3 - a*(x2-h)*(x2-h)*(x2-h)/3 + k*(x3-x2);
s -= (y3+y2)*(x3-x2)/2;
cout<<s<<endl;
}
return 0;
}
2.1.8 小数化分数2
据说是小学奥数,给小学奥数跪了
#include<iostream>
#include<string>
int gcd(int x,int y)
{
if (!x || !y) return x>y ? x : y;
for (int t;t=x%y;x=y,y=t);
return y;
}
using namespace std;
int main()
{
int n,i,j;
cin>>n;
for (int j=0;j<n;j++)
{
string st;
cin>>st;
int len=st.length();
int a=0,aa=1,b=0,bb=0;
i=2;
while (st[i]!='(' && i<len)
{
a=a*10+st[i]-'0';
aa*=10;
i++;
}
if (i<len)
{
b=a;
i++;
while (st[i]!=')')
{
b=b*10+st[i]-'0';
bb=bb*10+9;
i++;
}
}
if (bb==0) {b=a; bb=aa;}
else {b=b-a; bb=bb*aa;}
int g;
g=gcd(b,bb);
cout<<b/g<<"/"<<bb/g<<endl;
}
return 0;
}