light--oj--1116--Ekka Dokka(数学问题)
2015-12-04 21:24
218 查看
Description
Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the
cake such that Ekka gets a share of N square centimeters and Dokka gets a share of
M square centimeters where N is odd and
M is even. Both N and M are positive integers.
They want to divide the cake such that N * M = W, where
W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer W (2 ≤ W < 263). And
W will not be a power of 2.
Output
For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print
N and M. If there are multiple solutions, then print the result where
M is as small as possible.
Sample Input
3
10
5
12
Sample Output
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4
题目意思:给你一个数W,让你将其拆成两个整数的积的形式,其中一个整数为奇数,另一个为偶数。如果存在多组解,找到偶数最小的那组解。
思路:尽量的剪枝,使算法的时间能够更短一些,不至于出现超时的事情。
ac代码:
//对数据进行分析,发现只要是奇数肯定不符合条件,偶数的话肯定能写成1*N的形式一定符合。
#include<stdio.h>
//注意使用long long型来进行定义
int main(){
int T,cnt=0;
scanf("%d",&T);
while(T--){
long long w;
long long u,v;
scanf("%lld",&w);
if(w%2){//奇数一定不满足条件
printf("Case %d: Impossible\n",++cnt);
continue ;
}
for(int i=2;i<=w;i++){//偶数的话进行遍历。
if(w%i)
continue;
u=w/i;
if(u%2&&i%2==0){
v=i;
break;
}
}
printf("Case %d: %lld %lld\n",++cnt,u,v);
}
return 0 ;
}
Ekka and his friend Dokka decided to buy a cake. They both love cakes and that's why they want to share the cake after buying it. As the name suggested that Ekka is very fond of odd numbers and Dokka is very fond of even numbers, they want to divide the
cake such that Ekka gets a share of N square centimeters and Dokka gets a share of
M square centimeters where N is odd and
M is even. Both N and M are positive integers.
They want to divide the cake such that N * M = W, where
W is the dashing factor set by them. Now you know their dashing factor, you have to find whether they can buy the desired cake or not.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer W (2 ≤ W < 263). And
W will not be a power of 2.
Output
For each case, print the case number first. After that print "Impossible" if they can't buy their desired cake. If they can buy such a cake, you have to print
N and M. If there are multiple solutions, then print the result where
M is as small as possible.
Sample Input
3
10
5
12
Sample Output
Case 1: 5 2
Case 2: Impossible
Case 3: 3 4
题目意思:给你一个数W,让你将其拆成两个整数的积的形式,其中一个整数为奇数,另一个为偶数。如果存在多组解,找到偶数最小的那组解。
思路:尽量的剪枝,使算法的时间能够更短一些,不至于出现超时的事情。
ac代码:
//对数据进行分析,发现只要是奇数肯定不符合条件,偶数的话肯定能写成1*N的形式一定符合。
#include<stdio.h>
//注意使用long long型来进行定义
int main(){
int T,cnt=0;
scanf("%d",&T);
while(T--){
long long w;
long long u,v;
scanf("%lld",&w);
if(w%2){//奇数一定不满足条件
printf("Case %d: Impossible\n",++cnt);
continue ;
}
for(int i=2;i<=w;i++){//偶数的话进行遍历。
if(w%i)
continue;
u=w/i;
if(u%2&&i%2==0){
v=i;
break;
}
}
printf("Case %d: %lld %lld\n",++cnt,u,v);
}
return 0 ;
}
相关文章推荐
- python基础教程共60课-第3课IDE
- project euler 41
- 初探动态规划之数字三角形
- linux 安装mysql
- 利用JQuery jqprint实现打印功能
- 计算理论中的莱斯定理(Rice's Theorem)——证明与应用
- [置顶] 亮仔移植u-boot系列之-- S3c2440在最新版本U-boot-2015.10移植(支持SPL模式启动) -- 1
- Android Studio 快捷键
- Android自定义组件之圆形图片
- OpenCV2:图像的几何变换,平移、镜像、缩放、旋转(1)
- 用eclipse创建servlet小程序
- POJ 2352 Stars (区间建树,单点更新)
- cf C - Booking System
- centos下httpd-2.4的编译安装
- leetcode Count and Say
- project euler 40
- centos下httpd-2.4的编译安装
- android 布局优化(一)
- 问题处理:找不到Pch预编译文件?
- MATLAB 利用filter函数实现滑动平均滤波