【FOJ2206 11月月赛B】【观察找规律】函数求解 分析函数求值

来源:互联网 发布:比特彗星监听端口 编辑:程序博客网 时间:2024/06/10 14:58

 Problem 2206 函数求解

Accept: 28    Submit: 46
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

给出n,求f(n)。

 Input

第一行一个正整数T,表示数据组数。 接下来T行,每行一个正整数n。 T<=20,n<=2015000000。

 Output

对于每组数据,输出一行f(n)。

 Sample Input

2120150001

 Sample Output

201520152014

 Source

FOJ有奖月赛-2015年11月


#include<stdio.h>#include<string.h>#include<ctype.h>#include<math.h>#include<iostream>#include<string>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}const int N=0,M=0,Z=1e9+7,ms63=1061109567;int casenum,casei;const int V=20150001;int n,ans;int f(int x){if(x<V)return x+2014;else return f(f(x-2015));}int main(){scanf("%d",&casenum);for(casei=1;casei<=casenum;casei++){scanf("%d",&n);//printf("%d\n",f(n));if(n<V)ans=n+2014;else ans=20152014;printf("%d\n",ans);}return 0;}/*【题意】给你一个函数f()有f[n]=n+2014,n<20150001,f( f(n-2015) ),n>=20150001。每次给你一个n,(1<=n<=2015000000),让你输出f(n)。【类型】暴力找规律观察分析结论【分析】这道题,一开始想起来感觉有些懵,然后怕耽误时间,于是我干脆直接把对f()的定义写成了函数,然后就找到规律AC掉了,规律是这样的——if(n<V)ans=n+2014;else ans=20152014;为什么呢?设V=20150001;首先,n<V时ans=n+2014很好理解。然后,n>=V时,我们的值会变为f(f(x-2015))的结果。当x-2015>=V时,迭代会继续下去当x-2015<V时,相当于先减2015再加2014,即变成--x;这样我们会一直操作,直到x=20150001,然后答案会变成f(20150000)于是最后的答案会锁定在20152014*/


1 0
原创粉丝点击