URAL - 1495

这是在dp的专题里写了,想了半天的dp,其实就是暴力。。。

题目大意:给你一个n,问你在30位以内有没有一个只由1或2 构成的数被 n 整除,如果

有则输出最小的那个,否则输出Impossible。

思路:因为位数是30,所以我们不能直接暴力,这样的复杂度是2^30次。那么其实我们

可以分段暴力,我们只要求出15位的所有余数的最小值,我们再处理处由15数字组成的余数,

如果答案超过十五位,我们枚举超过15位部分的余数,找对应需要的余数。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[],f[],n;
void dfs(ll pos,ll now)
{
ll mod=now%n;
if(now && (dp[mod]>now || !dp[mod])) dp[mod]=now;
if(pos==)
{
if(!f[mod] || f[mod]>now) f[mod]=now;
return;
}
dfs(pos+,now*+);
dfs(pos+,now*+);
}
int main()
{
cin>>n;
if(n==)
{
puts("");
return ;
}
if(n==)
{
puts("");
return ;
}
dfs(,);
if(dp[])
{
cout<<dp[]<<endl;
return ;
}
ll ans1=-,ans2=-;
for(ll i=;i<n;i++)
{
if(!dp[i]) continue;
ll now=i;
for(int j=;j<=;j++)
{
now=now*%n;
}
ll p=n-now;
if(!f[p]) continue;
if(ans1==- || ans1>dp[i])
{
ans1=dp[i];
ans2=f[p];
}
}
if(ans1==-) puts("Impossible");
else printf("%I64d%I64d\n",ans1,ans2);
return ;
}

URAL - 1495 One-two, One-two 2的更多相关文章

  1. Regionals 2012 :: Chengdu

    题目连接 排行榜 A和I都是签到题 按位BFS K Yet Another Multiple Problem 题意:给一些可以用的数字,求最小的数,它由特定的数字组成且是n的倍数 分析:暴力枚举不可行 ...

  2. 【BFS】HDU 1495

    直达–> HDU 1495 非常可乐 相似题联动–>POJ 3414 Pots 题意:中文题,不解释. 思路:三个杯子倒来倒去,最后能让其中两个平分即可.可能性六种.判定的时候注意第三个杯 ...

  3. HDU 1495 非常可乐

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=103711#problem/M /*BFS简单题 链接地址: http://acm.hdu ...

  4. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  5. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  6. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  7. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  8. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  9. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

随机推荐

  1. Quartz.net创建windows服务

    序言 安装服务 sc create XXService binpath= "XXService.exe" start= auto sc description XXService ...

  2. 10 SpringBoot全面接管SpringMVC

    Spring Boot官方文档描述 If you want to keep Spring Boot MVC features and you want to add additional MVC co ...

  3. 微信小程序开发(1) 天气预报

    本文介绍如何使用微信小程序开发天气预报功能. 一.项目文件列表 二.小程序配置 使用app.json文件来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. ...

  4. js加减法运算多出很多小数点

    链接:http://www.jb51.net/article/73043.htm 解决方法:解决这种问题两种方法,第一种就是利用JavaScript 的toFixed(n) 方法,直接获取N 位小数, ...

  5. “微信小程序商城构建全栈应用”开发小记

    注意事项: 1.application\api\extra下的wx.php记得填写小程序的app_id.app_secret: 2.API测试小工具需要APPID:

  6. JavaScript学习 - 基础(四) - 控制语句/异常处理

    控制语句 if-else 语句 <script> //控制语句 //if-else格式: var x = 1 if(x==1){ console.log("this is if& ...

  7. 零基础http代理http完美代理访问

    如果翻过墙,或者做过渗透啥的,肯定对代理不陌生,说白了,代理服务器就是一个中转站,你对目标网址的请求都会进过代理服务器去请求,类似于一个被你操控的傀儡,别人能知道的也只能是这个代理,从而提升安全性和访 ...

  8. SpringBoot缓存

    (1).使用@EnableCaching注解开启基于注解的缓存 package cn.coreqi; import org.springframework.boot.SpringApplication ...

  9. Centos6.5使用yum安装mysql——快速上手必备

    第1步.yum安装mysql [root@stonex ~]#  yum -y install mysql-server 安装结果: Installed:     mysql-server.x86_6 ...

  10. javascript中的return、return true、return false、continue区别

    1.语法为:return 表达式; 2.w3c中的解释: 语句结束函数执行,返回调用函数,而且把表达式的值作为函数的结果  也就是:当代码执行到return语句时,函数返回一个结果就结束运行了,ret ...