codeforces C. Mashmokh and Numbers

题意:给你n和k,然后让你找出n个数使得gcd(a1,a2)+gcd(a3,a4)+......的和等于k;

思路:如果n为奇数,让前n-3个数的相邻两个数都为1,n-2和n-1两个数gcd为k-ans;ans为前n-3个数的和。为偶数的话,让前n-2个数的相邻两个数都为1,n和n-1两个数gcd为k-ans;

 1 #include <cstdio>
 2 #include <cmath>
 3 #include <cstring>
 4 #include <vector>
 5 #include <algorithm>
 6 #define maxn 10000
 7 #define ll long long
 8 using namespace std;
 9 
10 
11 int n,k;
12 vector<int>q;
13 
14 int main()
15 {
16     scanf("%d%d",&n,&k);
17     if((n%2==0&&(n/2)>k)||(n%2!=0&&(n-1)/2>k))
18     {
19         printf("-1\n");
20     }
21     else
22     {
23         if(n==1&&k==0)
24         {
25             printf("1\n");
26             return 0;
27         }
28         else if(n==1&&k>0)
29         {
30             printf("-1\n");
31             return 0;
32         }
33         if(n%2==0)
34         {
35             int ans=0;
36             ll c=1000000000;
37             for(int i=1; i<=n-2; i+=2)
38             {
39                 printf("%lld %lld ",c,c-1);
40                 c-=2;
41                 ans++;
42             }
43             printf("%lld %lld\n",(ll)(k-ans),(ll)((k-ans)*2));
44         }
45         else
46         {
47             int ans1=0;
48             ll c1=1000000000;
49             for(int i=1; i<n-2; i+=2)
50             {
51                 printf("%lld %lld ",c1,c1-1);
52                 c1-=2;
53                 ans1++;
54             }
55             printf("%lld %lld",(ll)(k-ans1),(ll)(k-ans1)*2);
56             printf(" %lld\n",c1);
57         }
58     }
59     return 0;
60 }
View Code
posted @ 2015-02-05 19:09  null1019  阅读(161)  评论(0编辑  收藏  举报