hdu1339

来源:互联网 发布:大数据一体机 编辑:程序博客网 时间:2024/05/19 00:41

A Simple Task

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3857    Accepted Submission(s): 2113
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1339

Problem Description
Given a positive integer n and the odd integer o and the nonnegative integer p such that n = o2^p.


Example

For n = 24, o = 3 and p = 3.


Task

Write a program which for each data set:

reads a positive integer n,

computes the odd integer o and the nonnegative integer p such that n = o2^p,

writes the result.
 

Input
The first line of the input contains exactly one positive integer d equal to the number of data sets, 1 <= d <= 10. The data sets follow.

Each data set consists of exactly one line containing exactly one integer n, 1 <= n <= 10^6.
 

Output
The output should consists of exactly d lines, one line for each data set.

Line i, 1 <= i <= d, corresponds to the i-th input and should contain two integers o and p separated by a single space such that n = o2^p.
 

Sample Input
124
 

Sample Output
3 3
解题思路:
题意就是给你n,输出满足式子n = o * 2 ^ p的o和p。
刚开始暴力(明知道超时····),果然超了····然后看看式子····果断用数学构造····
构造出来后很简单,就是不断地把n除以2,除的次数就是p,最后知道不能除为止,此时的n就是o。
完整代码:
#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;int main(){    #ifdef DoubleQ    freopen("in.txt","r",stdin);    #endif    int d;    scanf("%d",&d);    while(d--)    {        int n;        scanf("%d",&n);        int cnt = 0;        while(n % 2 == 0)        {            n /= 2;            cnt ++;        }        printf("%d %d\n",n , cnt);    }}


0 0
原创粉丝点击