POJ2402/UVA 12050 Palindrome Numbers 数学思维

A palindrome is a word, number, or phrase that reads the same forwards as backwards. For example,
the name “anna” is a palindrome. Numbers can also be palindromes (e.g. 151 or 753357). Additionally
numbers can of course be ordered in size. The ?rst few palindrome numbers are: 1, 2, 3, 4, 5, 6, 7, 8,
9, 11, 22, 33, ...
The number 10 is not a palindrome (even though you could write it as 010) but a zero as leading
digit is not allowed.
Input
The input consists of a series of lines with each line containing one integer value i (1 ≤ i ≤ 2 ∗ 109
).
This integer value i indicates the index of the palindrome number that is to be written to the output,
where index 1 stands for the ?rst palindrome number (1), index 2 stands for the second palindrome
number (2) and so on. The input is terminated by a line containing ‘0’.
Output
For each line of input (except the last one) exactly one line of output containing a single (decimal)
integer value is to be produced. For each input value i the i-th palindrome number is to be written to
the output.
Sample Input
1
12
24
0
Sample Output
1
33
151

题意:给出i,输出第i个回文数,不能有前导0.

题解:第一和最后一位不为0,

    我们预处理i位数有多少,以及 在不是第一位和最后一位情况下i位有多少情况

  再不断细分就好了

//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
#include<vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll;

const int N = 1050;
const int M = 1000001;
const int inf = 0x3f3f3f3f;
const int MOD = 1000000007;
const double eps = 0.000001;

ll a[N],sum[N],b[N],s;
void init() {s=0;
   a[1] = 10;b[1] = 9;
   a[2] = 10;b[2] = 9;
   for(int i=3;i<=20;i++) {
    a[i] = a[i-2] * 10;
   }s=18;
   for(int i=3;i<=20;i++) {
    b[i] = 9*a[i-2];
    s+=b[i];
   // cout<<s<<endl;
   }
}
int main() {
    init();
    ll n, num, ans[N];
    while(scanf("%lld",&n)!=EOF) {
        if(!n) break;
        mem(ans);
        int l,r,mm;
        for(int i=1;i<=20;i++) {
            if(b[i]>=n) {
                num = i;
                mm = num;
                l = 1, r = num;
                while(l<=r) {
                    if(num == 1) {
                        if(l!=1) n--;
                        ans[l] = n;
                        ans[r] = n;break;
                    }
                    if(num == 2) {
                        if(l!=1)n--;
                        ans[l] = n;
                        ans[r] = n;break;
                    }
                    ans[l] = n/a[num-2] + (l == 1?1:0);
                    n = n%a[num-2];
                    if(n == 0 && num-2!=0) {
                        ans[l]--;
                        n = a[num-2];
                    }
                    ans[r] = ans[l];
                    l++,r--;num-=2;
                }
                break;
            }
            n -= b[i];
        }
        for(int i=1;i<=mm;i++) printf("%lld",ans[i]);
        printf("\n");
    }
    return 0;
}

代码

时间: 2024-10-31 12:32:33

POJ2402/UVA 12050 Palindrome Numbers 数学思维的相关文章

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5

UVA 12050 - Palindrome Numbers 模拟

题目大意:给出i,输出第i个镜像数,不能有前导0. 题解:从外层开始模拟 #include <stdio.h> int p(int x) { int sum, i; for(sum=i=1;i<=x;i++) sum *= 10; return sum; } int main() { int n, i, j, t, cs[1000], c; while(~scanf("%d", &n)) { if(n==0) break; i=1; while(n>9*

POJ 3252 Round Numbers 数学题解

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

UVA - 12046 Great Numbers

Description Problem G - Great Numbers In this problem you have to count the number of great numbers of length n. Here a great number must have the following property: the number must be divisible by all of its decimal digits. it does not contain any

程序设计中的数学思维函数总结(代码以C#为例)

最近以C#为例,学习了程序设计基础,其中涉及到一些数学思维,我们可以巧妙的将这些逻辑问题转换为代码,交给计算机运算. 现将经常会使用到的基础函数做一总结,供大家分享.自己备用. 1.判断一个数是否为奇数 定义:整数中,能被2整除的数是偶数,不能被2整除的数是奇数 思路点:n%2!=0则为奇数 /// <summary> /// 判断一个整数是不是奇数 /// </summary> /// <param name="n">要判断的整数</para

UVA - 471 Magic Numbers

Description  Magic Numbers  Write a program that finds and displays all pairs ofintegers and such that: neither nor have any digits repeated; and , where N is a given integer; Input and Output The input file consist a integer at the beginning indicat

hdu 4710 Balls Rearrangement (数学思维)

题意:就是  把编号从0-n的小球对应放进i%a编号的盒子里,然后又买了新盒子, 现在总共有b个盒子,Bob想把球装进i%b编号的盒子里.求重置的最小花费. 每次移动的花费为y - x ,即移动前后盒子编号的差值的绝对值. 算法: 题目就是要求                  先判断  n与  lcm(a,b)的大小,每一个周期存在循环,这样把区间缩短避免重复计算. 如果n>lcm(a,b)则   ans = (n/lcm)*solve(lcm)+solve(n%lcm) 否则   ans =

Acdreamoj1115(数学思维题)

题意:1,3是完美数,如果a,b是完美数,则2+a*b+2*a+2*b,判断给出的n是否是完美数. 解法:开始只看出来2+a*b+2*a+2*b=(a+2)*(b+2)-2,没推出更多结论,囧.没办法,只能暴力将所有的完美数求出来然后查表.正解是c+2=(a+2)*(b+2);完美数都是有质因子3或5组成的(5本身除外): 自己暴力代码: /****************************************************** * author:xiefubao *****

【程序员的数学思维修炼】总结一

第一章 数据的表示 主要学习了会了 0 不是什么都没有,比如在java里BigDecimal里面是根据最高的那个精度来的,比如1.99+0.01=2.00 这时候提交可能会判错,所以要去掉后导零 为啥要用二进制 还有哪些进制,神奇的八卦,八进制.钟表使用的十二进制.半斤八两十六进制.60年一个甲子六十进制 关于二进制,最主要的是要熟练掌握位运算!一些做题中常用的技巧,比如求n的m次方,判断奇偶,统计一的个数, 这一章里还提到了20!阶乘问题,还记得怎么解决大数阶乘吗. 第二章 神奇的素数 1.验