codeforces 664C C. International Olympiad(数学)
题目链接:
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of formIAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.
For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 andIAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995.
You are given a list of abbreviations. For each of them determine the year it stands for.
The first line of the input contains a single integer n (1 ≤ n ≤ 1000) — the number of abbreviations to process.
Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
For each abbreviation given in the input, find the year of the corresponding Olympiad.
5
IAO'15
IAO'2015
IAO'1
IAO'9
IAO'0
2015
12015
1991
1989
1990
4
IAO'9
IAO'99
IAO'999
IAO'9999
1989
1999
2999
9999
题意:
从1989年开始,每个年份都用其后缀表示,如果这个后缀被之前的年份用过了,那么后缀就往前增加一位,现在给出后缀,问它表示的是哪一年;
思路:
可以发现,后缀的长度是有规律的,比如一个字符的后缀表示[1989,1998]两个字符的后缀表示[1999,2098]三个字符的后缀表示[2099,3098]...
可以发现这些区间的长度为后缀字符的个数^10;
所以就可以先处理所有的区间,然后再在前边加上相应的前缀,看是否在这个区间内就好了;
AC代码:
/*2014300227 662D - 46 GNU C++11 Accepted 15 ms 2180 KB*/ #include <bits/stdc++.h> using namespace std; const int N=1e6+5; typedef long long ll; const ll mod=1e9+7; ll dp[20]; int n; char str[20]; ll fun(int x) { ll ans=1; for(int i=1;i<=x;i++) { ans*=10; } return ans; } int solve() { int len=strlen(str); ll s=0; for(int i=4;i<len;i++) { s*=10; s+=str[i]-'0'; } for(int i=0;i<=200;i++) { ll num=s+(ll)i*fun(len-4); if(num>=dp[len-5]&&num<dp[len-4]) { cout<<num<<"\n"; return 0; } } } int main() { dp[0]=1989; ll temp=1; for(int i=1;i<=12;i++) { temp*=10; dp[i]=dp[i-1]+temp; } scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",str); solve(); } return 0; }