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=ansa%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;
}
J-子序列
做之前没了解过尺取法
想用指针去做、查完题解才了解的指针法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;</cstdlib></algorithm></cstring></cstdio></iostream>

const int N = 1e5 + 5;

int main()
{
int t;
cin >> t;
while(t--)
{
int a,b;//这里的b相当于尺子的长度
cin >> a >> b;
int x[N];
for(int i = 0;i<a;i++)//输入数据
{
cin >> x[i];
}
int ans = N;
int l = 0;//记录尺子左边
int sum = 0;
for(int i = 0;i<a;i++)
{
sum += x[i];
while(sum >= b)//大于尺子长度
{
ans = min(ans, i+1-l);//因为循环从0开始,所以这里要+1
sum -= x[l];//减去左边的一个数据
l++;
}
}
if(ans == N) cout << 0 << '\n';
else cout << ans << endl;
}
return 0;
}
H-派
用二分查找法能很快的找出值,不过要注意精度,直接输出时注意向下取整。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double pi=acos(-1.0);//3.1415926535897
int n,m;
double a[10010];
bool check(double mid)
{
int cnt=0;
for(int i=0;i<n;i++)
{
cnt+=(int)(a[i]/mid);//强制转化为int型
}
return cnt>=m;
}
int main()
{
int t,k;
cin>>t;
while(t--)
{
double r=0;
scanf("%d%d",&n,&m);
m++;
for(int i=0;i<n;i++)
{
scanf("%d",&k);
a[i]=pikk;
if(a[i]>r)
r=a[i];
}
double l=0;
for(int i=0;i<100;i++)
{
double mid=l+(r-l)/2;
if(check(mid))
l=mid;
else
r=mid;
}
printf("%.4f\n",r);
}
return 0;
}
G、
#include<iostream>
typedef long long ll;
using namespace std;
ll findd(ll x){
ll cnt=0;
while(x){
cnt+=x/5;//cnt-每次商的累加
x/=5;
}
return cnt;
}
int main()
{
int t,cnt = 1;
cin>>t;
while(t--)
{
ll a;
cin>>a;
ll l = 0,r = 5000000000,mid,ans;
while(r>=l)
{
mid = (l+r)/2;
if(findd(mid)>=a)
{
ans = mid;
r = mid-1;
}
else
{
l = mid+1;
}
}
if(findd(ans)!=a)//若不能找到
{
cout<<"Case "<<cnt<<": "<<"impossible"<<endl;
cnt++;
}
else
{
cout<<"Case "<<cnt<<": "<<ans<<endl;
cnt++;
}
}
return 0;
} </iostream></cmath></algorithm></iostream></cstdio>