↓ 跳过正文
  1. Posts/

codeforces 16 C. Monitor

·427 字·1 分钟

http://codeforces.com/contest/16/problem/C 题意:给定长宽a,b和分辨率x:y,注意分辨率x:y未必是最简比。问将现有的size裁剪成比例为x:y,使得面积最大的长宽是多少。 思路:可以通过找 x,y能扩大的倍数为k,找到一个最大的k使得kx<=a&&ky<=b。可以二分搞,但其实也可以不用。能扩大的最大的倍数其实就是 min(a/x,b/y). ps:收获了gcd更简单的一种写法。 直接 return b?gcd(b,a%b):a;

代码实现
 1/* ***********************************************
 2Author :111qqz
 3Created Time :2015年12月28日 星期一 22时50分23秒
 4File Name :code/cf/problem/16C.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33LL a,b,x,y;
34LL ax=0,ay=0;
35LL ans = -1;
36LL GCD;
37LL gcd(LL a,LL b)
38{
39    if (a<b) return gcd(b,a);
40    if (a%b==0) return b;
41    return gcd(b,a%b);
42   // return b?gcd(b,a%b):a;
43}
44
45
46int main()
47{
48	#ifndef  ONLINE_JUDGE
49	freopen("code/in.txt","r",stdin);
50  #endif
51	cin>>a>>b>>x>>y;
52	GCD = gcd(x,y);
53	x /=GCD;
54	y /=GCD;
55	cout<<x*min(a/x,b/y)<<" "<<y*min(a/x,b/y)<<endl;
56
57  #ifndef ONLINE_JUDGE
58  fclose(stdin);
59  #endif
60    return 0;
61}
62
63
64
65
66
67
68
69 while ( l != r )
70        {
71            mid = (l+r+1)>>1;
72            if ( x*mid <= a && y*mid <= b ) l = mid;
73            else r = mid-1;
74        }
75        if ( x*l > a || y*l > b ) puts ( "0 0" );
76        else printf ( "%I64d %I64d\n" , l*x , l*y );

相关文章

poj 2909 Goldbach's Conjecture (哥德巴赫猜想)

·244 字·1 分钟
水题 写一遍的目的是。。。复习一下快速筛的写法 喵呜 代码实现 1/************************************************************************* 2 > File Name: code/poj/2909.cpp 3 > Author: 111qqz 4 > Email: rkz2013@126.com 5 > Created Time: 2015年08月22日 星期六 14时25分34秒 6 ************************************************************************/ 7 8#include<iostream> 9#include<iomanip> 10#include<cstdio> 11#include<algorithm> 12#include<cmath> 13#include<cstring> 14#include<string> 15#include<map> 16#include<set> 17#include<queue> 18#include<vector> 19#include<stack> 20#define y0 abc111qqz 21#define y1 hust111qqz 22#define yn hez111qqz 23#define j1 cute111qqz 24#define tm crazy111qqz 25#define lr dying111qqz 26using namespace std; 27#define REP(i, n) for (int i=0;i<int(n);++i) 28typedef long long LL; 29typedef unsigned long long ULL; 30const int inf = 0x3f3f3f3f; 31const int N=1<<16; 32bool not_prime[N]; 33int prime[N]; 34int prime_num; 35int n; 36 37void init(){ 38 not_prime[0] = true; 39 not_prime[1] = true; 40 for ( int i =2 ; i < N ; i++){ 41 if (!not_prime[i]){ 42 prime[++prime_num] = i; 43 } 44 for ( int j = 1 ; j <= prime_num&&i*prime[j]<N ; j++){ 45 not_prime[i*prime[j]] = true; 46 if (i%prime[j]==0) break; 47 } 48 } 49} 50int main() 51{ 52 init(); 53 int n ; 54 while (scanf("%d",&n)&&n){ 55 int ans = 0 ; 56 for ( int i = 2 ; i <= n /2 ; i++){ 57 if (!not_prime[i]&&!not_prime[n-i]){ 58 ans++; 59 } 60 } 61 printf("%d\n",ans); 62 } 63 64 return 0; 65}