CSUFT 2018年 个人赛(1) B题(HihoCoder 1331) 及G题 (HDU - 2669)

继续补题啦~

先上题目

  B 原题地址

It is well known that each digit in a binary number can be only 0 or 1. Little Hi is wondering what happens if digit 2 is also allowed. He calls these numbers Biinary Numbers. For example (21)ii = 2 * 21 + 1 = 5, (112)ii = 1 * 22 + 1 * 21 + 2 = 8.

Little Hi soon notices that in biinary number system, numbers may have more than one representations. For example 8 has other representations as (1000)ii, (200)ii, (120)ii.

Given a decimal number N, Little Hi wants to know how many different representations of N exist in biinary number system? 

Input

A decimal number N. (0 ≤ N ≤ 1000000000)

Output

The number of N's representations in biinary system.

Sample Input
8
Sample Output
4

思想:

判断数值是否为偶数 

扫描二维码关注公众号,回复: 1521747 查看本文章

若为偶数 则最后一位数为0或是2 

若为奇数 则最后一位数只能是1 

然后 递归继续讨论前一位的值

上代码:

#include <stdio.h>
#include <iostream>
//扩展二进制数
using namespace std;
int f(int x)
{
    if (x==1||x==0) return 1;//只有 0 或 1 的时候 存在一种情况 2 还可以再进一位
    if (x%2==0)//如果为偶数  末位可能为 0 或 2
        return f(x/2)+f((x-2)/2);
    else
        return f(x/2);
}

int main()
{
    int n;
    while (scanf("%d",&n)!=EOF)
    {
        printf("%d\n",f(n));
    }
    return 0;
}

在比赛的时候有想过 用三叉树模拟

但是 未能实现

就是 按照位数来一步一步往上相乘叠加

如果 有大神看到 并用这种方法做出来的请告诉我(≧∇≦)ノ


然后是G题

Romantic

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9210    Accepted Submission(s): 3895


Problem Description
The Sky is Sprite.
The Birds is Fly in the Sky.
The Wind is Wonderful.
Blew Throw the Trees
Trees are Shaking, Leaves are Falling.
Lovers Walk passing, and so are You. 
................................Write in English class by yifenfei

 

Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead.
 

Input
The input contains multiple test cases.
Each case two nonnegative integer a,b (0<a, b<=2^31)
 

Output
output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead. 
 

Sample Input
 
  
77 5110 4434 79
 

Sample Output
 
  
2 -3sorry7 -3
 

Author
yifenfei
 

Source
 

Recommend
lcy
 


欧几里得扩展定理

可以先了解一下 欧几里得定理 以及扩欧 戳这里

(直接拿模板做的)(逃

#include <cstdio>
int x,y;
int gcd(int a,int b)//欧几里得扩展
{
    int t,d;
    if (b==0)
    {
        x=1;
        y=0;
        return a;
    }
    d=gcd(b,a%b);
    t=x;
    x=y;
    y=t-(a/b)*y;
    return d;
}
int main ()
{
    int a,b;
    while(~scanf("%d%d",&a,&b))
    {
        int d = gcd(a,b);//a*x+b*y=d
        if(d!=1)
        {
            printf("sorry\n");
            continue;
        }
        while(x <= 0)
        {
            x=x+b;
            y=y-a;
        }
        printf("%d %d\n",x,y);
    }
    return 0;
}


最近都没怎么做题目啊

要好好加油了( ̄m ̄)

猜你喜欢

转载自blog.csdn.net/oneline_/article/details/80457407