湖南大学程序设计竞赛新生赛(重现赛)
A The GCD of Fibonacci Numbers
斐波那契数列,模拟一下就知道规律了,两个数的肥波值的最大公因数等于两个数的最大公因数的肥波值
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
ll a[50];
int main(void)
{
a[1]=1;
for(int i=2;i<=45;i++) a[i]=a[i-1]+a[i-2];
int n;cin>>n;
while(n--){
int a1,a2;
cin>>a1>>a2;
cout<<a[__gcd(a1,a2)]<<endl;
}
return 0;
}
B Xu1024’s treasure chest
神奇的25-1=49-25
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
int main(void)
{
int n;cin>>n;
if(n==0) {
cout<<"-1";return 0;
}
cout<<5*abs(n)<<" "<<7*abs(n);
return 0;
}
C Binbin’s treasure map
搜索,然后分块把值给储存起来
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
int n,m;
char a[505][505];
int x[4][2]={0,1,1,0,0,-1,-1,0};
int sum[250005];
int dfs(int i,int j){
int ans;
if(a[i][j]=='$') ans=1;
else ans=0;
a[i][j]='#';
for(int z=0;z<4;z++){
int x1=i+x[z][0],y1=j+x[z][1];
if(x1>=0&&x1<=n+1&&y1>=0&&y1<=m+1&&a[x1][y1]!='#'){
ans+=dfs(x1,y1);
}
}
return ans;
}
int main(void)
{
cin>>n>>m;
int i,j;
for(i=0;i<=n+1;i++)
for(j=0;j<=m+1;j++)
a[i][j]='.';
for(i=1;i<=n;i++){
for(j=1;j<=m;j++)
cin>>a[i][j];
}
int num=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i][j]!='#')
sum[num++]=dfs(i,j);
}
}
sort(sum,sum+num);
if(num>=2)
cout<<sum[num-1]+sum[num-2];
else cout<<sum[0];
return 0;
}
D CET and miniskirt
统计一下各个选项的个数,然后找出 num[i]>=n-ans[i] ,这个i如果有就只有一个,相减就是答案。
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
int main(void)
{
int n;
cin>>n;
int ans[5]={0};
for(int i=0;i<n;i++){
char temp;
cin>>temp;
if(temp=='A') ans[1]++;
else if(temp=='B') ans[2]++;
else if(temp=='C') ans[3]++;
else if(temp=='D') ans[4]++;
}
int num[5]={0};
for(int i=1;i<5;i++){
cin>>num[i];
}
int Ans=0;
for(int i=1;i<5;i++){
if(num[i]>=n-ans[i]) Ans=num[i]-n+ans[i];
}
if(Ans==0) cout<<"orz"<<endl;
else cout<<Ans<<endl;
return 0;
}
E Days passing
快速幂
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
string s[7]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
int qpow(int a,int b)
{
int ans=1;
while(b){
if(b&1){
ans=ans*a%7;
}
a=a*a%7;
b=b>>1;
}
return ans;
}
int main(void)
{
string a,n;
int m;
cin>>a>>n>>m;
int x=0;
for(int i=0;i<n.length();i++){
x=(x*10+n[i]-'0')%7;
}
int k=qpow(x,m);
for(int i=0;i<7;i++){
if(s[i]==a){
k+=i;
k%=7;
break;
}
}
cout<<s[k];
return 0;
}
F Kuangyeye’s Game
也就是排个序,看看斜率一不一样
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
struct node
{
ll x,y;
bool operator <(const node a)
{
if(x!=a.x)return x<a.x;
return y<a.y;
}
}a[1005];
int main(void)
{
int n;cin>>n;
for(int i=0;i<n;i++) cin>>a[i].x>>a[i].y;
if(n==1||n==2) cout<<"Yes";
else{
for(int i=2;i<n;i++){
ll k1=(a[i].y-a[i-1].y)*(a[i-1].x-a[i-2].x);
ll k2=(a[i].x-a[i-1].x)*(a[i-1].y-a[i-2].y);
if(k1!=k2){
cout<<"No\n";
return 0;
}
}
cout<<"Yes";
}
return 0;
}
G Buying Keys
模拟买硬币嘛,有余钱就再掏出10块钱。
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
struct node
{
ll x,y;
bool operator <(const node a)
{
if(x!=a.x)return x<a.x;
return y<a.y;
}
}a[1005];
int main(void)
{
int n;cin>>n;
int m = n / 10,a = n % 10;
while(a%3!=0 && m){
a += 10;
m--;
}
if(a%3 != 0) cout << "orz" << endl;
else cout << m*3 + a/3 << endl;
return 0;
}
H Dice
概率论
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
int main(void)
{
int n;n=read();
while(n--){
int a;
a=read();
double k=1.0;
while(a--){
k*=0.5;
}
printf("%.4f\n",1.0-k);
}
return 0;
}
I Ambulance
和输入的数无关,总数减去可以的数目就是答案
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
ll a[100005];
int main(void)
{
ll mod=1000000007;
int n;cin>>n;
int k;for(int i=1;i<=n;i++) cin>>k;
a[1]=0;a[2]=1;
ll num=2;
for(ll i=3;i<=n;i++){
num=num*i%mod;
a[i]=(i-1)*(a[i-1]+a[i-2])%mod;
}
if(n==1) cout<<1;
else cout<<( (num-a[n]+mod)%mod);
return 0;
}
J Fake Nim
只有当只有一堆糖果且为偶数的时候,A才会赢。
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
int main(void)
{
long long int n,x,y;
cin>>n;
if(n == 1) cin>>x;
else cin>>x>>y;
if(n == 1&&x % 2 == 0) cout<<"DaDa"<<endl;
else cout<<"TuTu"<<endl;
return 0;
}
L Special number
判断质数个数,把质数用英语说得很高雅
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
bool pan(int a)
{
for(int i=2;i*i<=a;i++){
if(a%i==0) return 0;
}
return 1;
}
int main(void)
{
int a,b;cin>>a>>b;
int ans=0;
for(int i=a;i<=b;i++){
if(pan(i)) ans++;
}
if(a==0) ans--;
cout<<ans;
return 0;
}
M XOR sum
遍历一遍1 到100,发现每4个就返回一次等于自己的数。
#include<bits/stdc++.h>
#define MAX_INT ((unsigned)(-1)>>1)
#define MIN_INT (~MAX_INT)
#define pi (4*atan(1.0))
#define ll long long
#define inf 0x3f3f3f3f
#define infmax 0x3f3f3f3f3f3f3f3f
using namespace std;
int read()
{
int c=0;int flag=1;
char s;
while((s=getchar())>'9'||s<'0')if(s=='-')flag=-1;
c=s-'0';
while((s=getchar())<='9'&&s>='0') c=c*10+s-'0';
return c*flag;
}
ll f(ll i)
{
ll x=0;
while(i%4!=3&&i){
x^=i;
i--;
}
return x;
}
int main(void)
{
ll a,b;cin>>a>>b;
cout<< (f(a-1)^f(b)) <<endl;
return 0;
}