hdu--5019--开始参加bc了

开始 有时间 晚上bc比赛也去做了

这题 数据很大 虽然也注意到了 但还是一直tle

...

一开始用set做 tle

然后用vector做 tle

才发现是应该先去求出gcd(x,y)这样可以减少很多遍历

 1 #include <iostream>
 2 #include <set>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 typedef long long LL;
 7 set<LL>s;
 8
 9 void solve( LL x , LL y )
10 {
11     for( LL i = 1 ; i<=y/i && i<=x ; i++ )
12     {
13         if( x%i ==0 && y%i ==0 )
14         {
15             s.insert(i);
16         }
17         if( x%(y/i)==0 )
18         {
19             s.insert(y/i);
20         }
21     }
22 }
23
24 int main()
25 {
26     cin.sync_with_stdio(false);
27     int t , cnt;
28     LL x , y , k , temp , size;
29     cin >> t;
30     while( t-- )
31     {
32         s.clear();
33         cnt = 0;
34         set<LL>::iterator it;
35         cin >> x >> y >> k;
36         if( x>y )
37         {
38             swap(x,y);
39         }
40         solve(x,y);
41         size = s.size();
42         if( size<k )
43             cout << -1 << endl;
44         for( it = s.begin() ; it!=s.end() ; it++ )
45         {
46             ++ cnt;
47             if( cnt==size-k+1 )
48                 cout << *it << endl;
49         }
50     }
51     return 0;
52 }

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5
 6 typedef long long LL;
 7 vector<LL> ve_left;
 8 vector<LL> ve_right;
 9
10 void solve( LL x , LL y )
11 {
12     for( LL i = 1 ; i<=y/i && i<=x ; i++ )
13     {
14         if( y%i == 0 )
15         {
16             if( x%i==0 )
17             {
18                 ve_left.push_back(i);
19             }
20             if( y/i<=x && x%(y/i)==0 && i!=y/i )
21             {
22                 ve_right.push_back(y/i);
23             }
24         }
25     }
26
27 }
28
29 int main()
30 {
31     cin.sync_with_stdio(false);
32     int t;
33     LL x , y , k , Size , Size1 , Size2;
34     cin >> t;
35     while(t--)
36     {
37         ve_left.clear();
38         ve_right.clear();
39         cin >> x >> y >> k;
40         if( x>y )
41             swap( x , y );
42         solve( x , y );
43         Size1 = ve_left.size();
44         Size2 = ve_right.size();
45         Size = Size1 + Size2;
46         if( Size<k )
47             cout << -1 << endl;
48         else
49         {
50             if( Size2>=k )
51             {
52                 cout << ve_right[k-1] <<endl;
53             }
54             else
55             {
56                 k -= Size2;
57                 cout << ve_left[Size1-k] << endl;
58             }
59         }
60     }
61     return 0;
62 }

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <vector>
 4 using namespace std;
 5
 6 typedef long long LL;
 7 vector<LL> ve;
 8
 9 LL gcd( LL x , LL y )
10 {
11     return y%x == 0 ? x : gcd( LL(y%x) , x );
12 }
13
14 void solve( LL x , LL y , LL z )
15 {
16     for( LL i = 1 ; i<=z/i ; i++ )
17     {
18         if( z%i==0 )
19         {
20             ve.push_back(i);
21             if( i!=z/i )
22                 ve.push_back(z/i);
23         }
24     }
25 }
26
27 int main()
28 {
29     cin.sync_with_stdio(false);
30     int t;
31     LL x , y , k , z , Size;
32     cin >> t;
33     while(t--)
34     {
35         ve.clear();
36         cin >> x >> y >> k;
37         if( x>y )
38             swap( x , y );
39         z = gcd(x,y);
40         solve( x , y , z );
41         Size = ve.size();
42         if( Size<k )
43             cout << -1 << endl;
44         else
45         {
46             sort( ve.begin() , ve.end() );
47             cout << ve[Size-k] << endl;
48         }
49     }
50     return 0;
51 }

将写得过程中的代码 全贴上来 给自己点 经验#24...

today:

  用我一生 再换你十年天真无暇

  用我一生 再陪你十年颠沛流离

时间: 2025-01-09 07:28:45

hdu--5019--开始参加bc了的相关文章

HDU 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g

BestCoder10 1001 Revenge of GCD(hdu 5019) 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公约数依次为1 2 4 8,那么第 3 个 GCD(X, Y) = 2,也就是从后往前数第3个公共因子. TLE思路:求出 X 的所有因子(从小到大开始存储),再从后往前枚举这些因子,检查是否也为 Y 的因子,统计到第 K 个数就是答案了......以为可以顺利通过,原来数据量还是非常大滴!!! 正确

HDU 5019 Revenge of GCD(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 Problem Description In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), highest common factor (hcf), or greatest common measure (gcm), of two or more i

hdu 5233 Gunner II (bc #42 B)

Gunner II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1433    Accepted Submission(s): 540 Problem Description Long long ago, there was a gunner whose name is Jack. He likes to go hunting ver

hdu 5670 Machine(BC规律题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5670 Machine Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 451    Accepted Submission(s): 266 Problem Description There is a machine with m(2≤m≤

hdu 5178 pairs(BC第一题,,方法不止一种,,我用lower_bound那种。。。)

题意: X坐标上有n个数.JOHN想知道有多少对数满足:x[a]-x[b]<=k(题意给)[a<b] 思路: 额,,,直接看代码吧,,,, 代码: int T,n,k; int x[100005]; int main(){ cin>>T; while(T--){ cin>>n>>k; rep(i,1,n) scanf("%d",&x[i]); sort(x+1,x+1+n); ll ans=0; rep(i,2,n){ ll te

HDU 5019 简单数学题

这道题是说给定A和B,求第C大的公约数. 我们最长求的就是最大公约数了,也就是通常用的GCD算法.但是现在要求第C大的公约数,我们可以想见如果令第C大的公约数为x,最大公约数为g的话,那么x|g的,为什么呢? 我们可以直观的理解,最大公约数其实就是A和B分别进行素因子分解之后,能取到公共素因子乘起来得到的.而对于任意A.B的公约数,那么肯定包含了部分的最大公约数所包含的素因子,因此x|g. 于是要求第C大的公约数,只需要枚举g的因子就行了,我们知道求一个数的因子情况,是可以进行O(sqrt(n)

HDU 5019 Revenge of GCD

Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 122 Problem Description In mathematics, the greatest common divisor (gcd), also known as the greate

hdu 5019

#include <iostream> #include <fstream> #include <vector> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <algorithm> #include <cmath> #include <cfloat> #include