SGU 107 987654321 problem(找规律|离线处理)

这道题说找规律不如说离线处理+组合更准确些。

先离线枚举1-10^9的平方其中末尾等于987654321的数的个数(共8个)

那么

n<9个数0;

n=9个数8;

n>9时候最高位不能取0,最后九位只有8个情况,所以共有72*10^(n-10)。

PS:曾经被然哥他们拿这题坑过,当场没做出来,想不到快一年了才A掉,哎,物是人非。

//SGU 107 987654321 problem 
//离线处理+组合
//by night_watcher
#include<iostream>
#include<cmath>
using namespace std;

int main(){
    int n;
    while(cin>>n){
        if(n<9) cout<<0<<endl;
        else if(n==9) cout<<8<<endl;
        else {
            cout<<72;
            for(int i=0;i<n-10;i++) cout<<0;
            cout<<endl;
        }
    }
    return 0;
}


 

你可能感兴趣的:(组合,离线处理)