hdu4952 Number Transformation (找规律)
2014多校 第八题 1008
2014 Multi-University Training Contest 8
| 4952 | Number Transformation | 
| Number TransformationTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Teacher Mai has an integer x.
 He does the He wants to know what is the Input There are multiple test cases, terminated by a line "0 0". For each test case, the only one line contains two integers Output For each test case, output one line "Case #k: x", where k is the case number counting from 1. Sample Input 2520 10 2520 20 0 0 Sample Output Case #1: 2520 Case #2: 2600 Source Recommend hujie | 
大意:输入x和k,进行k次操作,每次把x变成大于等于x的最小的k的倍数,求最后的x。(1<=x k <=10^10)
题解:怒找规律。
这个的暴力做法很好写,几行就写出来了:
now=x;
for(i=;i<=k;i++){
mo=now%i;
if(mo!=){
now+=i-mo;
}
}
↑这个是比纯暴力优化了一点的,因为已知余数就可以直接算得下一个x,不用一个个加一个个试。
但是这样还是会超时得飞起来,10^10的数据必须找比O(n)小的方法。
于是我们可以在循环里面输出各种mo、i-mo、now之类的数,输入一点大数据观察一下有没有规律。
然后可以发现每次到后期,i-mo都会变成一个固定值!虽然我也不懂为什么,不过知道这个就可以了。当我们发现进入了这个状态时,直接now+=(不变的i-mo)*(k-i),得到最后结果。
于是我们统计i-mo连续多少次不变的次数,达到一定值就认为永远不变了,直接算。这个有时候也会出现连续5个不变之后又变了的,所以我设的是100次,多个100而已,对整体影响很小。
 ll farm(ll x,ll k) {
     ll i,now,mo;
     ll pre,cnt,maxc=;
     pre=-;
     cnt=;
     now=x;
     for(i=; i<=k; i++) {
         mo=now%i;
         if(mo!=) {
             now+=i-mo;
         }
         if(cnt>=maxc) {
             now+=pre*(k-i);
             break;
         }
         if(i-mo==pre)cnt++;
         else cnt=;
         pre=i-mo;
     }
     return now;
 }
全代码:
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<cmath>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
#define ll long long
#define usint unsigned int
#define mz(array) memset(array, 0, sizeof(array))
#define minf(array) memset(array, 0x3f, sizeof(array))
#define REP(i,n) for(i=0;i<(n);i++)
#define FOR(i,x,n) for(i=(x);i<=(n);i++)
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define RD3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define WN(x) printf("%d\n",x);
#define RE freopen("D.in","r",stdin)
#define WE freopen("1biao.out","w",stdout) ll farm(ll x,ll k) {
ll i,now,mo;
ll pre,cnt,maxc=;
pre=-;
cnt=;
now=x;
for(i=; i<=k; i++) {
mo=now%i;
if(mo!=) {
now+=i-mo;
}
if(cnt>=maxc) {
now+=pre*(k-i);
break;
}
if(i-mo==pre)cnt++;
else cnt=;
pre=i-mo;
}
return now;
} int main() {
int cas=, ans;
ll x,k,now,i,mo,pre,ppre,flag;
while(scanf("%I64d%I64d",&x,&k)!=EOF) {
if(x== && k==)break;
printf("Case #%d: %I64d\n",cas++,farm(x,k));
}
return ;
}
hdu4952 Number Transformation (找规律)的更多相关文章
- hdu 4952 Number Transformation (找规律)
		题目链接 题意:给你个x,k次操作,对于第i次操作是:要找个nx,使得nx是>=x的最小值,且能整除i,求k次操作后的数 分析: 经过打表找规律,会发现最后的x/i,这个倍数会趋于一个固定的值, ... 
- HDU 4279 Number(找规律)
		Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ... 
- LightOj 1245 --- Harmonic Number (II)找规律
		题目链接:http://lightoj.com/volume_showproblem.php?problem=1245 题意就是求 n/i (1<=i<=n) 的取整的和这就是到找规律的题 ... 
- HDU-4952                 Number Transformation
		http://acm.hdu.edu.cn/showproblem.php?pid=4952 Number Transformation Time Limit: 2000/1000 MS (Java/ ... 
- G - Harmonic Number (II)  找规律--> 给定一个数n,求n除以1~n这n个数的和。n达到2^31 - 1;
		/** 题目:G - Harmonic Number (II) 链接:https://vjudge.net/contest/154246#problem/G 题意:给定一个数n,求n除以1~n这n个数 ... 
- UVA 10706 Number Sequence (找规律 + 打表 + 查找)
		Problem B Number Sequence Input: standard input Output: standard output Time Limit: 1 second A singl ... 
- codeforces 622D   D. Optimal Number Permutation(找规律)
		D. Optimal Number Permutation time limit per test 1 second memory limit per test 256 megabytes input ... 
- 计蒜客31452   Supreme Number(找规律)
		A prime number (or a prime) is a natural number greater than 11 that cannot be formed by multiplying ... 
- 2018 ICPC 沈阳网络赛预赛 Supreme Number(找规律)
		[传送门]https://nanti.jisuanke.com/t/31452 [题目大意]:给定一个数字(最大可达10100),现在要求不超过它的最大超级质数.超级质数定义:对于一个数,把它看成数字 ... 
随机推荐
- php热身2:CRUD with Ajax
			这次热身是一个会员管理系统,包括会员注册.登录.资料修改功能,使用ajax技术 1.建表 use common_module; create table if not exists member( u ... 
- 【BZOJ-3667】Rabin_Miller算法    随机化判素数
			3667: Rabin-Miller算法 Time Limit: 60 Sec Memory Limit: 512 MBSubmit: 983 Solved: 302[Submit][Status ... 
- bzoj1121: [POI2008]激光发射器SZK
			#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ... 
- css3的@media媒体查询
			css3新功能,根据屏幕大小进行识别. 参考: http://www.runoob.com/cssref/css3-pr-mediaquery.html 
- Android成长日记-使用ViewFlipper实现屏幕切换动画效果
			(一) ViewFlipper介绍 Android系统自带的一个多页面管理控件,它可以实现子界面的自动切换 (二) 为ViewFlipper加入View 1. 静态导入:在Layout布局文件中直接导 ... 
- C++ 之 const references
			extraction from The C++ Programming Language 4th. ed., Section 7.7 References, Bjarne Stroustrup To ... 
- codevs 1013 求先序排列(二叉树遍历)
			传送门 Description 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). Input 两个字符串,分别是中序和后序(每行一个) Outp ... 
- [Android] View.setTag(key,Object) (java.lang.IllegalArgumentException: The key must be an application-specific resource id.)
			转自: http://blog.csdn.net/brokge/article/details/8536906 setTag是android的view类中很有用的一个方法,可以用它来给空间附加一些信息 ... 
- Python与C++结构体交互
			需求:根据接口规范,实现与服务端的数据交互 服务端结构体分包头.包体.包尾 包头C++结构体示例如下 typedef struct head { BYTE string1; BYTE string2; ... 
- 申请https证书需要注意的4大问题
			HTTPS证书是什么 https证书是数字证书中的一种,由受信任的数字证书颁发机构CA如[沃通CA]在验证服务器身份后颁发,具有服务器身份验证和数据传输加密 功能,因其要配置在服务器上,所以也称SSL ... 
