Link:http://acm.hdu.edu.cn/showproblem.php?pid=5174
题意:有 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; }
#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 againTime 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...N−1,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
Sample Output
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.
#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; }