2016第七届蓝桥杯 四平方和(dfs)

四平方和


四平方和定理,又称为拉格朗日定理:
每个正整数都可以表示为至多4个正整数的平方和。
如果把0包括进去,就正好可以表示为4个数的平方和。


比如:
5 = 0^2 + 0^2 + 1^2 + 2^2
7 = 1^2 + 1^2 + 1^2 + 2^2
(^符号表示乘方的意思)


对于一个给定的正整数,可能存在多种平方和的表示法。
要求你对4个数排序:
0 <= a <= b <= c <= d
并对所有的可能表示法按 a,b,c,d 为联合主键升序排列,最后输出第一个表示法




程序输入为一个正整数N (N<5000000)
要求输出4个非负整数,按从小到大排序,中间用空格分开


例如,输入:
5
则程序应该输出:
0 0 1 2


再例如,输入:
12
则程序应该输出:
0 2 2 2


再例如,输入:
773535
则程序应该输出:
1 1 267 838


资源约定:
峰值内存消耗 < 256M

CPU消耗  < 3000ms

tips;简单深搜

#include<iostream>

using namespace std;

int sum;
int tmp[40];
int flag;
void dfs(int x,int step,int left)
{
	if(flag)return;
	if(step==4)
	{
		if(left==0){
			for(int i=1;i<=4;i++)cout<<tmp[i]<<" ";
			cout<<endl;
			flag=1;
		}
		return;
	}
	for(int i=x;i*i<=left;i++)
	{
		tmp[step+1]=i;
		dfs(i,step+1,left-i*i);
	}
}
int main()
{
	cin>>sum;
	dfs(0,0,sum);
	
	return 0;
}

#CUDA_VISIBLE_DEVICES=0 python pred.py 
# /home/ninesun/temp/output/model/model.h5 
# /home/ninesun/temp/output/Set5/ppx_GT.bmp 
# /home/ninesun/temp/output/pred/ 2

from keras.models import Sequential
from keras.layers import Conv2D, Input, BatchNormalization
from keras.callbacks import ModelCheckpoint
from keras.optimizers import SGD, Adam
import numpy
import math
import os
from keras.models import load_model
import cv2
from PIL import Image

input_size = 33
label_size = 33
scale=2
def setup_session():
    import tensorflow as tf
    from keras.backend import tensorflow_backend
    config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True))
    session = tf.Session(config=config)
    tensorflow_backend.set_session(session)

def predict(srcnn_model, input_path, out_dir,flag):
    img = cv2.imread(input_path, cv2.IMREAD_COLOR)
    shape=img.shape
    if (flag == 2):
        img = cv2.resize(img, (int(shape[1] *scale), int(shape[0] *scale)), cv2.INTER_CUBIC)

    h,w,c = img.shape
    pad_img = numpy.zeros((h+2*input_size,w+2*input_size,c))
    pad_img[input_size:input_size+h, input_size:input_size+w,:] = img

    up_bicu = numpy.zeros((h+2*input_size,w+2*input_size,c))

    dst=numpy.zeros(((h+2*input_size,w+2*input_size,c)))
    #dst[input_size:input_size + h, input_size:input_size + w, :] =img
    cnt =1
    print h,w,c
    for y in range(0, h+2*input_size, label_size):
        for x in range(0, w+2*input_size, label_size):
            if((y+input_size<h+2*input_size) and (x+input_size<w+2*input_size)):
                part = pad_img[y:y+input_size,x:x+input_size]
                #small,big = make_base(part)
                big=make_base(part)
                up_bicu[y:y+input_size,x:x+input_size] = big

                out = exec_pred(srcnn_model, part)

                dst[y:y + label_size, x:x + label_size,:] = out[0,:,:,:]

    up_path = os.path.join(out_dir, 'up.png')
    down_path=os.path.join(out_dir, 'down.png')
    out_path = os.path.join(out_dir, 'out.png')

    cv2.imwrite(up_path, up_bicu[input_size:-input_size, input_size:-input_size])
    cv2.imwrite(out_path, dst[input_size:-input_size,input_size:-input_size])

    img = img.astype(numpy.uint8)
    shape = img.shape
    down_bicu = cv2.resize(img, (int(shape[1] / scale), int(shape[0] / scale)), cv2.INTER_CUBIC)
    cv2.imwrite(down_path,down_bicu)

    p1=psnr_cal(input_path,up_path)
    p2=psnr_cal(input_path,out_path)

    print "bicubic: ",p1
    print "srcnn",p2
def psnr_cal(input,up):
    im1=cv2.imread(input,cv2.IMREAD_COLOR)
    im2=cv2.imread(up,cv2.IMREAD_COLOR)

    h,w,c=im1.shape
    mse=0.
    for i in range(0,h,1):
        for j in range(0,w,1):
            for k in range (0,c,1):
                mse=mse+(im1[i][j][k]-im2[i][j][k])*(im1[i][j][k]-im2[i][j][k])

    mse=mse/(h*w*c*1.0)
    psnr=10.0*math.log10(255.0*255.0/mse)

    return psnr




def make_base(img):
    img = img.astype(numpy.uint8)
    shape = img.shape
    img_down = cv2.resize(img, (int(shape[1] / 2), int(shape[0] / 2)), cv2.INTER_CUBIC)
    img_up = cv2.resize(img_down, (shape[1], shape[0]), cv2.INTER_CUBIC)
    return img_up

def exec_pred(srcnn_model, img):
    h,w,c = img.shape
    img = img.astype(numpy.uint8)
    shape = img.shape
    img = cv2.resize(img, (int(shape[1] / 2), int(shape[0] / 2)), cv2.INTER_CUBIC)
    img = cv2.resize(img, (shape[1], shape[0]), cv2.INTER_CUBIC)

    Y = numpy.zeros((1, img.shape[0], img.shape[1], c), dtype=float)
    Y[0] = img.astype(float) / 255.

    pre = run_pred(srcnn_model, Y)

    pre[pre[:] > 255] = 255
    pre[pre[:] < 0] = 0
    pre = pre.astype(numpy.uint8)
    return pre

def run_pred(model, Y):
        return model.predict(Y, batch_size=1) * 255.

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("model", help="model file path")
    parser.add_argument("input", help="data dir")
    parser.add_argument("out_dir", help="output dir")
    parser.add_argument("flag", help="2,small image 1,large image")
    args = parser.parse_args()
    print(args)

    if not os.path.exists(args.out_dir):
        os.makedirs(args.out_dir)

    setup_session()
    model = load_model(args.model)

    predict(model, args.input, args.out_dir,args.flag)

    print('fin')
    

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值