链接:https://ac.nowcoder.com/acm/contest/897/B
来源:牛客网

Trial of Devil
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

As an acmer, Devil Aguin particularly loves numbers. This time, with a sequence consisting of n elements 1∼n initially, Devil Aguin asks you to process the sequence until all the elements in it turn to zero. What you can do in one operation are as following :
    1. Select some elements from the sequence arbitrarily.
    2. Select a positive integer x arbitrarily.
    3. Subtract x from the elements you select.

It is obvious that there are various methods to make elements of the sequence turn to zero. But Devil Aguin demands of you to use the minimum operations. Please tell him how many operations you will use.

输入描述:

    The first line contains an integer number T, the number of test cases.

    ithith of each next T lines contains one integer n(1≤n≤1001≤n≤100), the number of sequence.

输出描述:

For each test case print a number, the minimum number of operations required.
示例1

输入

复制

2
1
2

输出

复制

1
2

题意:给你一个t组数据,每一个数据给你一个整数n,代表有一个n的全排列, 你可以做一些操作让他们的值都变成0,。每次操作,你可以选择全排列中的一些数,然后再选择任意一个x,让那些数减去x。现在问你最小的操作次数是多少?

思路:

对于每一个整数n的全排列,我们第一次就选择大于等于n/2的那些数,然后减去n/2。例如81 2 3 4 5 6 7 8 8/2=4减去4后就是1 2 3 0 1 2 3 4因为处理1 2 3 4 的时候,就可以顺带的把1 2 3 也给处理了,所以我们问题就转化为了 n/2的全排列的减为0的问题。通过一直这样转为更小的全排列的操作,我们即可得到最优的解,当n=1的时候,只需要1步就可以减为0。那么我们递归函数就可以写成:
int f(int x)
{if(x==1){return 1;}else{return 1+f(x/2);}
}

AC代码:

int f(int x)
{if(x==1){return 1;}else{return 1+f(x/2);}
}
int main()
{//freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);//freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);int t;gbtb;cin>>t;while(t--){int n;cin>>n;if(n==1){cout<<1<<endl;}else{cout<<f(n)<<endl;}}return 0;
}

转载于:https://www.cnblogs.com/qieqiemin/p/10931300.html

2019长安大学ACM校赛网络同步赛 B Trial of Devil (递归)相关推荐

  1. 2019长安大学ACM校赛网络同步赛 L XOR (规律,数位DP)

    链接:https://ac.nowcoder.com/acm/contest/897/L 来源:牛客网 XOR 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...

  2. 2019长安大学ACM校赛网络同步赛 J Binary Number(组合数学+贪心)

    链接:https://ac.nowcoder.com/acm/contest/897/J 来源:牛客网 Binary Number 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32 ...

  3. 2019长安大学ACM校赛网络同步赛 Trial of Devil

    链接:https://ac.nowcoder.com/acm/contest/view-submission?submissionId=40669755 来源:牛客网 题目描述 As an acmer ...

  4. 2019长安大学ACM校赛网络同步赛C LaTale (树上DP)

    链接:https://ac.nowcoder.com/acm/contest/897/C 来源:牛客网 LaTale 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 32768K,其他 ...

  5. 2019长安大学ACM校赛网络同步赛 L XOR

    题意 求区间内有多少数x满足x^4x^5x=0 1≤l≤r≤1018. 题解 根据异或的性质可以推到 x^4x=5x -->x^4x=x+4x 即x和4x每一位都不同,即x和(x<< ...

  6. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛题解

    A,C,I签到题,只搞了8题,还一题是神仙做的,我不会 链接:https://www.nowcoder.com/acm/contest/122/B 来源:牛客网 取石子 时间限制:C/C++ 1秒,其 ...

  7. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛B 取石子(博弈SG函数模板)

    题目链接:取石子 链接:https://www.nowcoder.com/acm/contest/122/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65 ...

  8. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 部分题解

    A-ID and password 签到题. #include <iostream> #include <cstdio> #include <bits/stdc++.h& ...

  9. 南昌大学航天杯第二届程序设计竞赛校赛网络同步赛 - 题解

    A - ID and password 题目描述 Users prefer simple passwords that are easy to remember, but such passwords ...

  10. 2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛

    2016广东工业大学新生杯决赛网络同步赛暨全国新生邀请赛 Ploblem A :   pigofzhou的巧克力棒 原题链接:http://gdutcode.sinaapp.com/problem.p ...

最新文章

  1. 抢购系统架构设计原理参考文档
  2. 电话号码 【trie树】
  3. 具体数学第二版第四章习题(1)
  4. 记录处理搜狗新闻分类
  5. SQL 备份与恢复之还原数据库
  6. ORA-16019: cannot use LOG_ARCHIVE_DEST_1 解决方法
  7. 企业级负载均衡如何实现
  8. js循环出来的数据补全_加推实战之数据预测
  9. Visio 2013专业版
  10. EasyDarwin接口的使用——使用C++编写客户端实现与EasyDarwin服务器的交互
  11. 阿里的世界版图——“风清扬”的全球梦
  12. NMOS 与 PMOS
  13. 【UEFI实战】EDK的编译流程说明
  14. 视频转换成gif动图如何操作?教你三步完成视频转gif
  15. zend studio设置字体大小和背景颜色
  16. Java后端落地项目记录
  17. 详解AUTOSAR:AUTOSAR方法论(理论篇—3)
  18. isam2 优化pose graph
  19. 常见锁的区别及适用场景
  20. 手机号查QQ号(Python3版)

热门文章

  1. NNDL 实验六 卷积神经网络(4) ResNet18实现MINIST
  2. 金融科技之能量守恒定律
  3. 嵌入式硬件-读懂原理图
  4. java 代码块 构造函数_Java学习笔记之------构造函数,静态关键字,静态代码块,构造代...
  5. 如何将Nios II硬件和软件合成一个文件(NIOS II)(硬件)(软件)(合并)
  6. 开启nexus出现If you insist running as root, then set the environment variable RUN_AS_USER=root
  7. 维吉尼亚密码(Vigenere)
  8. 骇客学堂:网络入侵检测初步探测方法
  9. windows10桌面_windows10 美化桌面加强能力
  10. 攻防世界web练习区