Your team was exploring an ancient city. Suddenly you found an old scroll with 2 integer numbers N and K, which encrypts the secret code to open a treasure box. Considering a transformation on an integer X described as follows:
X = X + X mod 100,
the secret code can be obtained by applying the above-described transformation Ktimes successively to N.
Input
The input file consists of several datasets. The first line of the input file contains the number of datasets which is a positive integer and is not greater than 500.
Each dataset has two space-separated positive integers N and K (1 ≤ N ≤ 109, 1 ≤ K ≤ 109) written on a single line.
Output
For each dataset, write on a single line the secret number decrypted from N and K.
Input
3
10101 67
31102014 2
10101 10
Output
13228
31102056
10324
题意:给定x,k,(x,k皆小于exp(9)),问进行k次操作后x=x+xmod(100)值为多少
在纸上写几组数据, 发现当k一定大以后就会出现循环, 这可以归类于 循环(散乱的前缀+循环体)
找到周期, 然后一次性处理掉在周期内的, 然后处理剩余部分
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define Max 1050
long long mod(int x)
{
return x-(x/100)*100;
}
int main()
{
ios::sync_with_stdio(false);
freopen("a.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)
{
long long n,k;
scanf("%I64d%I64d", &n, &k);
long long flag[Max];//flag[]数组表示当值为x时,增加的值
long long num[Max];//num数组记录当值为x时,此时的序号
long long add=0;//记录执行k次后增加的值
memset(flag,-1,sizeof(flag));
memset(num,0,sizeof(num));
int i;
long long x=mod(n);
for( i=1;i<=k;i++)
{
add+=x;
if(flag[x]!=-1)//说明找到了重复增加的值,即找到了周期
{
int T=i-num[x];//此时的周期即为T
// cout<<"T="<<T<<" "<<endl;
long long kase=add-flag[x];//kase就代表一个周期内所增加的值
/*接下来就要算下面的几个周期内增加的值,*/
long long m=(k-i)/T;//m表示从i到k还有几个周期数
add+=m*kase;//将周期内增加的值加到add里面
i+=m*T;//i的值也应该发生变化,变化的大小就应该是这几个周期大小的和
/*周期内的运算次数算完了,所以可以跳出循环了*/
break;//如果此时i!=k,那么还要算剩余的几次运算
//需要注意的是,此时是上一次增加的值,在每次循环的最开始就已经加到了add里面,
//所以如果下面有剩余运算次数的话,要从下一个x开始,即x=mod(2*x);
}
else
{
flag[x]=add;//没找到就要记录下此时当值为x时增加的值;
num[x]=i;
x=mod(2*x);//根据公式继续往下运算,这里的2*x是表示下一个数的后两位数值是成倍增加的,写几个例子就能发现规律
}
}
// cout<<"x="<<x<<endl;
// cout<<"i="<<i<<endl;
for(;i<k;i++)//有剩余的情况
{
x=mod(2*x);
add+=x;
}
printf("%I64d\n", n + add);
}
return 0;
}
来源:https://blog.csdn.net/prolightsfxjh/article/details/52031851