A-伪素数
思路:这题主要是考察素数的判定方法。首先要确保不是素数,然后还满足a^p==a mod p
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int prime(int n){ //素数判定
int count=0;
for(int i=2;ii<=n;i++){
if(n%i==0){
count=1;
break;
}
}
if(count) return 0;
else
return 1;
}
int main(){
ll a,p;
while(cin>>p>>a&&a&&p){
if(prime(p)){
printf("no\n");
continue;
}
//判断a^p % p 是否等于 a
ll ans=1; //初始化
ll sum=a;
ll mod=p; //储存mod
while(p) {
if(p&1){
ans=ans
a%mod;
}
a=aa%mod;
p>>=1;
}
if(ans==sum)
printf("yes\n");
else
printf("no\n");
}
return 0;
}
E-最右边的数字
1.这是我第一次的错误答案
当时没看见数据范围那么大
就没想到用前两天讲的快速幂
然后自己反思了一下
用快速幂就ac了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
int t;
int N;
cin>>t;
while(t--){
cin>>N;
int n=pow(N,N);
int m;
m=n%10;
cout<<m<<endl;
}
return 0;
}
2.正确版
#include <iostream>
#include <cmath>
using namespace std;
int quick_mod(int a,int m)
{
int b=1;
while(m>0)
{
if(m&1)
{
b=b</cmath></iostream></cstdlib></cmath></algorithm></cstring></cstdio></iostream>
a%10;
}
m>>=1;
a=a*a%10;
}
return b;
}
int main()
{
int t,n,ans;
cin>>t;
while(t--)
{
cin>>n;
int tmp=n%10;
ans=quick_mod(tmp,n);
cout<<ans<<endl;
}
return 0;</algorithm></iostream>

}
F - 人见人爱A^B
#include<stdio.h>
int main()
{
int a,n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0)
break;
if(n==0&&m!=0)
printf("0\n");
if(n!=0&&m==0)
printf("1\n");
if(n!=0&&m!=0)
{
int s=n;
a=m-1;
while(a--)
{
s=sn;
s=s%1000;
}
printf("%d\n",s);
}
}
return 0;
}
I - Can you solve this equation?
思路:首先想到的是二分法
#include<iostream>
using namespace std;
double f(double x)
{
return 8</iostream>
pow(x,4)+7pow(x,3)+2pow(x,2)+3*x+6;
}
double dao(int y,double l,double h)
{
double mid=(l+h)/2;
if(h-l>=10e-7)
{
if(f(mid)==y)
{
return mid;
}
if(f(mid)>y)
return dao(y,l,mid);
if(f(mid)<y)
return dao(y,mid,h);
}
return mid;
}
int main()
{
int n,y;
cin>>n;
while(n--)
{
cin>>y;
if(y<f(0)||y>f(100))
cout<<"No solution!"<<endl;
else
{ printf("%0.4lf\n",dao(y,0,100));
}
}
return 0;
}