Valentine's Day Round(HDU BC比赛)

Link:http://acm.hdu.edu.cn/showproblem.php?pid=5174


Ferries Wheel

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 258    Accepted Submission(s): 97


Problem Description
The Ferries Wheel is a circle,rounded by many cable cars,and the cars are numbered  1,2,3...K1,K  in order.Every cable car has a unique value and  A[i1]<A[i]<A[i+1](1<i<K) .

Valentine's Day Round(HDU BC比赛)_第1张图片
Today,Misaki invites  N  friends to play in the Ferries Wheel.Every one will enter a cable car. One person will receive a kiss from Misaki,if this person satisfies the following condition: (his/her cable car's value + the left car's value
% INT_MAX = the right car's value,the  1st  car’s left car is the  kth  car,and the right one is  2nd  car,the  kth  car’s left car is the  (k1)th  car,and the right one is the 1st  car.

Please help Misaki to calculate how many kisses she will pay,you can assume that there is no empty cable car when all friends enter their cable cars,and one car has more than one friends is valid.
 

Input
There are many test cases.
For each case,the first line is a integer  N(1<=N<=100)  means Misaki has invited  N  friends,and the second line contains  N  integers  val1,val2,...valN , the  val[i]  means the  ith  friend's cable car's value.
(0<=val[i]<=  INT_MAX).

The INT_MAX is  2147483647 .
 

Output
For each test case, first output Case #X: ,then output the answer, if there are only one cable car, print "-1";
 

Sample Input
       
       
       
       
3 1 2 3 5 1 2 3 5 7 6 2 3 1 2 7 5
 

Sample Output
       
       
       
       
Case #1: 1 Case #2: 2 Case #3: 3
Hint
In the third sample, the order of cable cars is {{1},{2}, {3}, {5}, {7}} after they enter cable car,but the 2nd cable car has 2 friends,so the answer is 3.
 

Source
Valentine's Day Round
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5177  5176  5175  5173  5172 
 

Statistic |  Submit |  Discuss |  Note

题意:有 N 个人去做摩天轮,每个人进一个缆车,问有多少人满足:【(他的缆车的值+左边车的值)%INT_MAX=右边缆车的值】.
解法:暴力,记录每个缆车出现的次数,排序去重,枚举缆车的值,判断是否满足那个等式即可。

AC代码1(直接模拟+离散化):

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
int aa[111],aaa[111];
struct node{
	int v;
	int id;
}a[111];
bool cmp(node a,node b)
{
	return a.v<b.v;
}
__int64 m[222],ans;
int main()
{
	int n,i,cnt,t;
	t=0;
	while(~scanf("%d",&n))
	{
		t++;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i].v);
			a[i].id=i;
		}
		sort(a+1,a+n+1,cmp);
		int nn=1;
		aa[a[1].id]=1;
		for(i=2;i<=n;i++)
		{
			if(a[i].v!=a[i-1].v)
			{
				nn++;
				aa[a[i].id]=nn;
			}
			else
			{
				aa[a[i].id]=nn;
			}
			
		}
		memset(aaa,0,sizeof(aaa));
		for(i=1;i<=n;i++)
		{
			aaa[aa[i]]++;
		}
		cnt=1;
		m[cnt]=a[1].v;
		for(i=2;i<=n;i++)
		{
			if(a[i].v!=a[i-1].v)
			{
				m[++cnt]=a[i].v;
			}
		}
		if(cnt==1)
		{
			ans=-1;
			printf("Case #%d: %d\n",t,ans);
		}
		else
		{
				ans=0;
		for(i=2;i<=cnt-1;i++)
		{
			if((m[i]+m[i-1])%2147483647==m[i+1])
			{
				ans+=aaa[i];
			}
		}
		if((m[cnt]+m[cnt-1])%2147483647==m[1])
		{
			ans+=aaa[cnt];
		}
		if((m[1]+m[cnt])%2147483647==m[2])
		{
			ans+=aaa[1];
		}
		printf("Case #%d: %I64d\n",t,ans);
		}
		
	}
	return 0;
}

代码2(用map容器记录数据):


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define mst(A,k) memset(A,k,sizeof(A))
typedef long long ll;
const ll MOD = 2147483647;
const double eps = 1e-8;
const int INF = 100000000;
const int MAX_N =100005;
const int MAX_M = 100005;
int main()
{
	int n,cnt,cas=0;
	ll a[105],k;
	map<ll,int>mp;
	while(scanf("%d",&n)!=EOF){
		mp.clear();
		cnt=0;
		cas++;
		for(int i=0;i<n;i++)
		{
			scanf("%I64d",&k);
			a[cnt]=k;
			mp[k]++;
			if(mp[k]==1)
			{
				cnt++;
			}
		}
		sort(a,a+cnt);
		int ans = 0;
		for(int i=0;i<cnt;i++){
			if((a[i]+a[(i+1)%cnt])%MOD ==a[(i+2)%cnt])
			{
				ans+=mp[a[(i+1)%cnt]];
			}
		}
		if(cnt==1)
		{
			ans=-1;
		}
		cout<<"Case #"<<cas<<": "<<ans<<"\n";
	}
	return 0;
}


Link:http://acm.hdu.edu.cn/showproblem.php?pid=5175

Misaki's Kiss again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 301    Accepted Submission(s): 81


Problem Description
After the Ferries Wheel, many friends hope to receive the Misaki's kiss again,so Misaki numbers them  1,2...N1,N ,if someone's number is M and satisfied the  GCD(N,M)  equals to  N  XOR  M ,he will be kissed again.

Please help Misaki to find all  M(1<=M<=N) .

Note that:
GCD(a,b)  means the greatest common divisor of  a  and  b .
A  XOR  B  means  A  exclusive or  B
 

Input
There are multiple test cases. 

For each testcase, contains a integets  N(0<N<=1010)
 

Output
For each test case, 
first line output Case #X:,
second line output  k  means the number of friends will get a kiss.
third line contains  k  number mean the friends' number, sort them in ascending and separated by a space between two numbers
 

Sample Input
       
       
       
       
3 5 15
 

Sample Output
       
       
       
       
Case #1: 1 2 Case #2: 1 4 Case #3: 3 10 12 14
Hint
In the third sample, gcd(15,10)=5 and (15 xor 10)=5, gcd(15,12)=3 and (15 xor 12)=3,gcd(15,14)=1 and (15 xor 14)=1
 

Source
Valentine's Day Round
 

Recommend
hujie   |   We have carefully selected several similar problems for you:   5177  5176  5173  5172  5171 
 

Statistic |  Submit |  Discuss |  Note

AC代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
#define mst(A,k) memset(A,k,sizeof(A))
typedef long long ll;
ll n,ans[10000],p[10000];
int m,c;
ll gcd(ll a,ll b){
	if(a<b) swap(a,b);
	ll c = a%b;
	while(c>0)
	{
		a=b;
		b=c;
		c=a%b;
	}
	return b;
}
int main()
{
	int t=0;
	while(~scanf("%I64d",&n)){
		m=c=0;
		for(ll i=1;i*i<=n;i++)
		{
			if(n%i==0){
				p[m++]=i;
				if(n/i!=i){
					p[m++]=n/i;
				}
			}
		}
		sort(p,p+m);
		for(int i=0;i<m;i++)
		{
			ll x=n^p[i];
			if(0<x&&x<=n&&gcd(x,n)==p[i]){
				ans[c++]=x;
			}
		}
		printf("Case #%d:\n%d\n",++t,c);
		sort(ans,ans+c);
		for(int i=0;i<c;i++)
		{
			if(i) putchar(' ');
			printf("%I64d",ans[i]);
		}
		puts("");
	}
	return 0;
}


BC#30 1002 解题思路

题意即求解出 M ,如果通过遍历所有的 m 来求解,算法复杂度是 O(n),会 TLE.

 枚举n的约数,将n的约数看成最大公约数,看是否存在0<m<n满足题意,也就是倒过来推 。
N^M=K,这里的M和K是一一对应的,唯一的M对应唯一的K,唯一的K对应唯一的M,而且异或运算满足逆运算,即N^K=M

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
using namespace std;
__int64 gcd(__int64 a,__int64 b)
{
	if(a<b)
	{
		swap(a,b);
	}
	__int64 c = a%b;
	while(c>0)
	{
		a=b;
		b=c;
		c=a%b;
	}
	return b;
}

int main()
{
	__int64 n,m,k,k2,ans[100011];
	int t=0,cnt;
	while(~scanf("%I64d",&n))
	{
		t++;
		cnt=0;
		for(k=1;k*k<=n;k++)
		{
			if(n%k==0)
			{
				m=n^k;
				if(m>=1&&m<=n&&gcd(n,m)==k)
				{
					ans[++cnt]=m;
				} 
				k2=n/k;
				if(k!=k2)
				{
					m=n^k2;
					if(m>1&&m<=n&&gcd(n,m)==k2)
					{
						ans[++cnt]=m;
					}
				}
			}
		}
		sort(ans+1,ans+cnt+1);
		 printf("Case #%d:\n%d\n", t, cnt);
		if(cnt>0)
		{
			printf("%I64d",ans[1]);
			for(int i=2;i<=cnt;i++)
			{
				printf(" %I64d",ans[i]);
			}
			//printf("\n");<strong>//注意格式,写在这里会PE!!!因为题目输出要求是3行,即使cnt==0也要输出空行!!!故只能写在if语句外!!!</strong>
			
		}
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(算法,ACM)