1022. Fib数列
https://acm.sjtu.edu.cn/OnlineJudge/problem/1022
Description
定义Fib数列:1,1,2,3,5,8,13,…1,1,2,3,5,8,13,…
求第NN项除以20102010的余数
Input Format
输入仅一行,为一个整数NN
Output Format
输出仅一行,为第NN项除以20102010的余数
Sample Input
3
Sample Output
2
Limits:
对于70%的数据 N≤1,000,000N≤1,000,000
对于100%的数据 N≤210,000,000,000
解:
此题数据范围太大,long到第99位就吼不住了,数据类型需要用BigInteger;
对于时间也有限制,于是抛弃了递归,使用for循环解决,仍达不到要求;
再次审题,对2010进行取余,难道是2010年出的题?还是说有什么规律可循的捷径?
于是依次打印了从1到50000的结果,对结果分析后发现每隔2040个数字就循环一次,于是将n对2040取余,得解。
import java.math.BigInteger;
import java.util.Scanner; public class Main { public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
n = n % 2040;
System.out.println(fib3(n).divideAndRemainder(new BigInteger("2010"))[1]); } static BigInteger fib3(long n) {
BigInteger n1 = new BigInteger("0");
BigInteger n2 = new BigInteger("1");
for (long i = 0; i < n; i++) {
BigInteger temp = n1;
n1 = n2.add(n1);
n2 = temp;
}
return n1;
} }
C++
#include<iostream>
using namespace std; int main(){
int t;
long long n;
cin>>n;
n = n % ;
int a=,b=;
if(n < ){
cout<<"";
}else{
for(long long i=;i<=n;i++){
t=a+b;
if(t>){
t-=;
}
a=b;
b=t;
}
cout<<t;
}
return ;
}
以下是为投机取巧的答案,为缩短执行时间,提前缓存一部分答案
一
import java.util.Scanner; public class Main {
static int[] data; static {
data = new int[]{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 574, 161, 735, 896, 1631, 517, 138, 655, 793, 1448, 231, 1679, 1910, 1579, 1479, 1048, 517, 1565, 72, 1637, 1709, 1336, 1035, 361, 1396, 1757, 1143, 890, 23, 913, 936, 1849, 775, 614, 1389, 2003, 1382, 1375, 747, 112, 859, 971, 1830, 791, 611, 1402, 3, 1405, 1408, 803, 201, 1004, 1205, 199, 1404, 1603, 997, 590, 1587, 167, 1754, 1921, 1665, 1576, 1231, 797, 18, 815, 833, 1648, 471, 109, 580, 689, 1269, 1958, 1217, 1165, 372, 1537, 1909, 1436, 1335, 761, 86, 847, 933, 1780, 703, 473, 1176, 1649, 815, 454, 1269, 1723, 982, 695, 1677, 362, 29, 391, 420, 811, 1231, 32, 1263, 1295, 548, 1843, 381, 214, 595, 809, 1404, 203, 1607, 1810, 1407, 1207, 604, 1811, 405, 206, 611, 817, 1428, 235, 1663, 1898, 1551, 1439, 980, 409, 1389, 1798, 1177, 965, 132, 1097, 1229, 316, 1545, 1861, 1396, 1247, 633, 1880, 503, 373, 876, 1249, 115, 1364, 1479, 833, 302, 1135, 1437, 562, 1999, 551, 540, 1091, 1631, 712, 333, 1045, 1378, 413, 1791, 194, 1985, 169, 144, 313, 457, 770, 1227, 1997, 1214, 1201, 405, 1606, 1, 1607, 1608, 1205, 803, 2008, 801, 799, 1600, 389, 1989, 368, 347, 715, 1062, 1777, 829, 596, 1425, 11, 1436, 1447, 873, 310, 1183, 1493, 666, 149, 815, 964, 1779, 733, 502, 1235, 1737, 962, 689, 1651, 330, 1981, 301, 272, 573, 845, 1418, 253, 1671, 1924, 1585, 1499, 1074, 563, 1637, 190, 1827, 7, 1834, 1841, 1665, 1496, 1151, 637, 1788, 415, 193, 608, 801, 1409, 200, 1609, 1809, 1408, 1207, 605, 1812, 407, 209, 616, 825, 1441, 256, 1697, 1953, 1640, 1583, 1213, 786, 1999, 775, 764, 1539, 293, 1832, 115, 1947, 52, 1999, 41, 30, 71, 101, 172, 273, 445, 718, 1163, 1881, 1034, 905, 1939, 834, 763, 1597, 350, 1947, 287, 224, 511, 735, 1246, 1981, 1217, 1188, 395, 1583, 1978, 1551, 1519, 1060, 569, 1629, 188, 1817, 2005, 1812, 1807, 1609, 1406, 1005, 401, 1406, 1807, 1203, 1000, 193, 1193, 1386, 569, 1955, 514, 459, 973, 1432, 395, 1827, 212, 29, 241, 270, 511, 781, 1292, 63, 1355, 1418, 763, 171, 934, 1105, 29, 1134, 1163, 287, 1450, 1737, 1177, 904, 71, 975, 1046, 11, 1057, 1068, 115, 1183, 1298, 471, 1769, 230, 1999, 219, 208, 427, 635, 1062, 1697, 749, 436, 1185, 1621, 796, 407, 1203, 1610, 803, 403, 1206, 1609, 805, 404, 1209, 1613, 812, 415, 1227, 1642, 859, 491, 1350, 1841, 1181, 1012, 183, 1195, 1378, 563, 1941, 494, 425, 919, 1344, 253, 1597, 1850, 1437, 1277, 704, 1981, 675, 646, 1321, 1967, 1278, 1235, 503, 1738, 231, 1969, 190, 149, 339, 488, 827, 1315, 132, 1447, 1579, 1016, 585, 1601, 176, 1777, 1953, 1720, 1663, 1373, 1026, 389, 1415, 1804, 1209, 1003, 202, 1205, 1407, 602, 2009, 601, 600, 1201, 1801, 992, 783, 1775, 548, 313, 861, 1174, 25, 1199, 1224, 413, 1637, 40, 1677, 1717, 1384, 1091, 465, 1556, 11, 1567, 1578, 1135, 703, 1838, 531, 359, 890, 1249, 129, 1378, 1507, 875, 372, 1247, 1619, 856, 465, 1321, 1786, 1097, 873, 1970, 833, 793, 1626, 409, 25, 434, 459, 893, 1352, 235, 1587, 1822, 1399, 1211, 600, 1811, 401, 202, 603, 805, 1408, 203, 1611, 1814, 1415, 1219, 624, 1843, 457, 290, 747, 1037, 1784, 811, 585, 1396, 1981, 1367, 1338, 695, 23, 718, 741, 1459, 190, 1649, 1839, 1478, 1307, 775, 72, 847, 919, 1766, 675, 431, 1106, 1537, 633, 160, 793, 953, 1746, 689, 425, 1114, 1539, 643, 172, 815, 987, 1802, 779, 571, 1350, 1921, 1261, 1172, 423, 1595, 8, 1603, 1611, 1204, 805, 2009, 804, 803, 1607, 400, 2007, 397, 394, 791, 1185, 1976, 1151, 1117, 258, 1375, 1633, 998, 621, 1619, 230, 1849, 69, 1918, 1987, 1895, 1872, 1757, 1619, 1366, 975, 331, 1306, 1637, 933, 560, 1493, 43, 1536, 1579, 1105, 674, 1779, 443, 212, 655, 867, 1522, 379, 1901, 270, 161, 431, 592, 1023, 1615, 628, 233, 861, 1094, 1955, 1039, 984, 13, 997, 1010, 2007, 1007, 1004, 1, 1005, 1006, 1, 1007, 1008, 5, 1013, 1018, 21, 1039, 1060, 89, 1149, 1238, 377, 1615, 1992, 1597, 1579, 1166, 735, 1901, 626, 517, 1143, 1660, 793, 443, 1236, 1679, 905, 574, 1479, 43, 1522, 1565, 1077, 632, 1709, 331, 30, 361, 391, 752, 1143, 1895, 1028, 913, 1941, 844, 775, 1619, 384, 2003, 377, 370, 747, 1117, 1864, 971, 825, 1796, 611, 397, 1008, 1405, 403, 1808, 201, 2009, 200, 199, 399, 598, 997, 1595, 582, 167, 749, 916, 1665, 571, 226, 797, 1023, 1820, 833, 643, 1476, 109, 1585, 1694, 1269, 953, 212, 1165, 1377, 532, 1909, 431, 330, 761, 1091, 1852, 933, 775, 1708, 473, 171, 644, 815, 1459, 264, 1723, 1987, 1700, 1677, 1367, 1034, 391, 1425, 1816, 1231, 1037, 258, 1295, 1553, 838, 381, 1219, 1600, 809, 399, 1208, 1607, 805, 402, 1207, 1609, 806, 405, 1211, 1616, 817, 423, 1240, 1663, 893, 546, 1439, 1985, 1414, 1389, 793, 172, 965, 1137, 92, 1229, 1321, 540, 1861, 391, 242, 633, 875, 1508, 373, 1881, 244, 115, 359, 474, 833, 1307, 130, 1437, 1567, 994, 551, 1545, 86, 1631, 1717, 1338, 1045, 373, 1418, 1791, 1199, 980, 169, 1149, 1318, 457, 1775, 222, 1997, 209, 196, 405, 601, 1006, 1607, 603, 200, 803, 1003, 1806, 799, 595, 1394, 1989, 1373, 1352, 715, 57, 772, 829, 1601, 420, 11, 431, 442, 873, 1315, 178, 1493, 1671, 1154, 815, 1969, 774, 733, 1507, 230, 1737, 1967, 1694, 1651, 1335, 976, 301, 1277, 1578, 845, 413, 1258, 1671, 919, 580, 1499, 69, 1568, 1637, 1195, 822, 7, 829, 836, 1665, 491, 146, 637, 783, 1420, 193, 1613, 1806, 1409, 1205, 604, 1809, 403, 202, 605, 807, 1412, 209, 1621, 1830, 1441, 1261, 692, 1953, 635, 578, 1213, 1791, 994, 775, 1769, 534, 293, 827, 1120, 1947, 1057, 994, 41, 1035, 1076, 101, 1177, 1278, 445, 1723, 158, 1881, 29, 1910, 1939, 1839, 1768, 1597, 1355, 942, 287, 1229, 1516, 735, 241, 976, 1217, 183, 1400, 1583, 973, 546, 1519, 55, 1574, 1629, 1193, 812, 2005, 807, 802, 1609, 401, 0, 401, 401, 802, 1203, 2005, 1198, 1193, 381, 1574, 1955, 1519, 1464, 973, 427, 1400, 1827, 1217, 1034, 241, 1275, 1516, 781, 287, 1068, 1355, 413, 1768, 171, 1939, 100, 29, 129, 158, 287, 445, 732, 1177, 1909, 1076, 975, 41, 1016, 1057, 63, 1120, 1183, 293, 1476, 1769, 1235, 994, 219, 1213, 1432, 635, 57, 692, 749, 1441, 180, 1621, 1801, 1412, 1203, 605, 1808, 403, 201, 604, 805, 1409, 204, 1613, 1817, 1420, 1227, 637, 1864, 491, 345, 836, 1181, 7, 1188, 1195, 373, 1568, 1941, 1499, 1430, 919, 339, 1258, 1597, 845, 432, 1277, 1709, 976, 675, 1651, 316, 1967, 273, 230, 503, 733, 1236, 1969, 1195, 1154, 339, 1493, 1832, 1315, 1137, 442, 1579, 11, 1590, 1601, 1181, 772, 1953, 715, 658, 1373, 21, 1394, 1415, 799, 204, 1003, 1207, 200, 1407, 1607, 1004, 601, 1605, 196, 1801, 1997, 1788, 1775, 1553, 1318, 861, 169, 1030, 1199, 219, 1418, 1637, 1045, 672, 1717, 379, 86, 465, 551, 1016, 1567, 573, 130, 703, 833, 1536, 359, 1895, 244, 129, 373, 502, 875, 1377, 242, 1619, 1861, 1470, 1321, 781, 92, 873, 965, 1838, 793, 621, 1414, 25, 1439, 1464, 893, 347, 1240, 1587, 817, 394, 1211, 1605, 806, 401, 1207, 1608, 805, 403, 1208, 1611, 809, 410, 1219, 1629, 838, 457, 1295, 1752, 1037, 779, 1816, 585, 391, 976, 1367, 333, 1700, 23, 1723, 1746, 1459, 1195, 644, 1839, 473, 302, 775, 1077, 1852, 919, 761, 1680, 431, 101, 532, 633, 1165, 1798, 953, 741, 1694, 425, 109, 534, 643, 1177, 1820, 987, 797, 1784, 571, 345, 916, 1261, 167, 1428, 1595, 1013, 598, 1611, 199, 1810, 2009, 1809, 1808, 1607, 1405, 1002, 397, 1399, 1796, 1185, 971, 146, 1117, 1263, 370, 1633, 2003, 1626, 1619, 1235, 844, 69, 913, 982, 1895, 867, 752, 1619, 361, 1980, 331, 301, 632, 933, 1565, 488, 43, 531, 574, 1105, 1679, 774, 443, 1217, 1660, 867, 517, 1384, 1901, 1275, 1166, 431, 1597, 18, 1615, 1633, 1238, 861, 89, 950, 1039, 1989, 1018, 997, 5, 1002, 1007, 2009, 1006, 1005, 1, 1006, 1007, 3, 1010, 1013, 13, 1026, 1039, 55, 1094, 1149, 233, 1382, 1615, 987, 592, 1579, 161, 1740, 1901, 1631, 1522, 1143, 655, 1798, 443, 231, 674, 905, 1579, 474, 43, 517, 560, 1077, 1637, 704, 331, 1035, 1366, 391, 1757, 138, 1895, 23, 1918, 1941, 1849, 1780, 1619, 1389, 998, 377, 1375, 1752, 1117, 859, 1976, 825, 791, 1616, 397, 3, 400, 403, 803, 1206, 2009, 1205, 1204, 399, 1603, 2002, 1595, 1587, 1172, 749, 1921, 660, 571, 1231, 1802, 1023, 815, 1838, 643, 471, 1114, 1585, 689, 264, 953, 1217, 160, 1377, 1537, 904, 431, 1335, 1766, 1091, 847, 1938, 775, 703, 1478, 171, 1649, 1820, 1459, 1269, 718, 1987, 695, 672, 1367, 29, 1396, 1425, 811, 226, 1037, 1263, 290, 1553, 1843, 1386, 1219, 595, 1814, 399, 203, 602, 805, 1407, 202, 1609, 1811, 1410, 1211, 611, 1822, 423, 235, 658, 893, 1551, 434, 1985, 409, 384, 793, 1177, 1970, 1137, 1097, 224, 1321, 1545, 856, 391, 1247, 1638, 875, 503, 1378, 1881, 1249, 1120, 359, 1479, 1838, 1307, 1135, 432, 1567, 1999, 1556, 1545, 1091, 626, 1717, 333, 40, 373, 413, 786, 1199, 1985, 1174, 1149, 313, 1462, 1775, 1227, 992, 209, 1201, 1410, 601, 1, 602, 603, 1205, 1808, 1003, 801, 1804, 595, 389, 984, 1373, 347, 1720, 57, 1777, 1834, 1601, 1425, 1016, 431, 1447, 1878, 1315, 1183, 488, 1671, 149, 1820, 1969, 1779, 1738, 1507, 1235, 732, 1967, 689, 646, 1335, 1981, 1306, 1277, 573, 1850, 413, 253, 666, 919, 1585, 494, 69, 563, 632, 1195, 1827, 1012, 829, 1841, 660, 491, 1151, 1642, 783, 415, 1198, 1613, 801, 404, 1205, 1609, 804, 403, 1207, 1610, 807, 407, 1214, 1621, 825, 436, 1261, 1697, 948, 635, 1583, 208, 1791, 1999, 1780, 1769, 1539, 1298, 827, 115, 942, 1057, 1999, 1046, 1035, 71, 1106, 1177, 273, 1450, 1723, 1163, 876, 29, 905, 934, 1839, 763, 592, 1355, 1947, 1292, 1229, 511, 1740, 241, 1981, 212, 183, 395, 578, 973, 1551, 514, 55, 569, 624, 1193, 1817, 1000, 807, 1807, 604, 401, 1005, 1406, 401, 1807, 198, 2005, 193, 188, 381, 569, 950, 1519, 459, 1978, 427, 395, 822, 1217, 29, 1246, 1275, 511, 1786, 287, 63, 350, 413, 763, 1176, 1939, 1105, 1034, 129, 1163, 1292, 445, 1737, 172, 1909, 71, 1980, 41, 11, 52, 63, 115, 178, 293, 471, 764, 1235, 1999, 1224, 1213, 427, 1640, 57, 1697, 1754, 1441, 1185, 616, 1801, 407, 198, 605, 803, 1408, 201, 1609, 1810, 1409, 1209, 608, 1817, 415, 222, 637, 859, 1496, 345, 1841, 176, 7, 183, 190, 373, 563, 936, 1499, 425, 1924, 339, 253, 592, 845, 1437, 272, 1709, 1981, 1680, 1651, 1321, 962, 273, 1235, 1508, 733, 231, 964, 1195, 149, 1344, 1493, 827, 310, 1137, 1447, 574, 11, 585, 596, 1181, 1777, 948, 715, 1663, 368, 21, 389, 410, 799, 1209, 2008, 1207, 1205, 402, 1607, 2009, 1606, 1605, 1201, 796, 1997, 783, 770, 1553, 313, 1866, 169, 25, 194, 219, 413, 632, 1045, 1677, 712, 379, 1091, 1470, 551, 11, 562, 573, 1135, 1708, 833, 531, 1364, 1895, 1249, 1134, 373, 1507, 1880, 1377, 1247, 614, 1861, 465, 316, 781, 1097, 1878, 965, 833, 1798, 621, 409, 1030, 1439, 459, 1898, 347, 235, 582, 817, 1399, 206, 1605, 1811, 1406, 1207, 603, 1810, 403, 203, 606, 809, 1415, 214, 1629, 1843, 1462, 1295, 747, 32, 779, 811, 1590, 391, 1981, 362, 333, 695, 1028, 1723, 741, 454, 1195, 1649, 834, 473, 1307, 1780, 1077, 847, 1924, 761, 675, 1436, 101, 1537, 1638, 1165, 793, 1958, 741, 689, 1430, 109, 1539, 1648, 1177, 815, 1992, 797, 779, 1576, 345, 1921, 256, 167, 423, 590, 1013, 1603, 606, 199, 805, 1004, 1809, 803, 602, 1405, 2007, 1402, 1399, 791, 180, 971, 1151, 112, 1263, 1375, 628, 2003, 621, 614, 1235, 1849, 1074, 913, 1987, 890, 867, 1757, 614, 361, 975, 1336, 301, 1637, 1938, 1565, 1493, 1048, 531, 1579, 100, 1679, 1779, 1448, 1217, 655, 1872, 517, 379, 896, 1275, 161, 1436, 1597, 1023, 610, 1633, 233, 1866, 89, 1955, 34, 1989, 13, 2002, 5, 2007, 2, 2009, 1, 0}; } public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
int m = (int) (n % 2040);
System.out.println(data[m]);
}
}
1022. Fib数列的更多相关文章
- FIB数列
斐波那契级数除以N会出现循环,此周期称为皮萨诺周期. 下面给出证明 必然会出现循环 这是基于下面事实: 1. R(n+2)=F(n+2) mod P=(F(n+1)+F(n)) mod P=(F(n+ ...
- bzoj5104: Fib数列
Description Fib数列为1,1,2,3,5,8... 求在Mod10^9+9的意义下,数字N在Fib数列中出现在哪个位置 无解输出-1 Input 一行,一个数字N,N < = 10 ...
- 动态规划之Fib数列类问题应用
一,问题描述 有个小孩上楼梯,共有N阶楼梯,小孩一次可以上1阶,2阶或者3阶.走到N阶楼梯,一共有多少种走法? 二,问题分析 DP之自顶向下分析方式: 爬到第N阶楼梯,一共只有三种情况(全划分,加法原 ...
- UVaLive 3357 Pinary (Fib数列+递归)
题意:求第 k 个不含前导 0 和连续 1 的二进制串. 析:1,10,100,101,1000,...很容易发现长度为 i 的二进制串的个数正好就是Fib数列的第 i 个数,因为第 i 个也有子问题 ...
- 【bzoj5118】Fib数列2 费马小定理+矩阵乘法
题目描述 Fib定义为Fib(0)=0,Fib(1)=1,对于n≥2,Fib(n)=Fib(n-1)+Fib(n-2) 现给出N,求Fib(2^n). 输入 本题有多组数据.第一行一个整数T,表示数据 ...
- HDU3977 Evil teacher 求fib数列模p的最小循环节
In the math class, the evil teacher gave you one unprecedented problem! Here f(n) is the n-th fibona ...
- [bzoj5118]Fib数列2_费马小定理_矩阵乘法
Fib数列2 bzoj-5118 题目大意:求Fib($2^n$). 注释:$1\le n\le 10^{15}$. 想法:开始一看觉得一定是道神题,多好的题面啊?结果...妈的,模数是质数,费马小定 ...
- 【BZOJ5104】Fib数列(BSGS,二次剩余)
[BZOJ5104]Fib数列(BSGS,二次剩余) 题面 BZOJ 题解 首先求出斐波那契数列的通项: 令\(A=\frac{1+\sqrt 5}{2},B=\frac{1-\sqrt 5}{2}\ ...
- @bzoj - 5104@ Fib数列
目录 @description@ @solution@ @accepted code@ @details@ @description@ Fib数列为1,1,2,3,5,8... 求在Mod10^9+9 ...
随机推荐
- C#读取Excel 几种方法的体会
(1) OleDb: 用这种方法读取Excel速度还是非常的快的,但这种方式读取数据的时候不太灵活,不过可以在 DataTable 中对数据进行一些删减修改 这种方式将Excel作为一个数据源,直接用 ...
- Chrome 浏览器如何完美实现滚动截图技巧
一.前言 我们平时在浏览网页时,想把碰到好的网页内容或者文章截屏保存,但是网页的长度常常会超出屏幕高度,一般的截屏功能只能截取显示在屏幕上的内容,那我们该如何方便快捷截取全部内容?今天就分享一个如何利 ...
- k8s资源清单定义入门
1.资源分类 a.workload型资源:service.pod.deployment.ReplicaSet.StatefulSet.Job.Cronjob; b.服务发现及服务均衡资源型资源:Ser ...
- 【APIO2016】Gap
题目描述 有 $N$ 个严格递增的非负整数 $a_1, a_2, \dots, a_N$($0 \leq a_1 < a_2 < \cdots < a_N \leq 10^{18}$ ...
- IE使用多彩文档上传数据库报错
使用多彩文档,用IE浏览器提交表单,双引号里面包含单引号,导致数据库插入不了,而用chrome浏览器不会报错,自动过滤单引号, 解决:content.replace("'", &q ...
- Java-HashMap原理解析
本文分析HashMap的实现原理. 数据结构(散列表) HashMap是一个散列表(也叫哈希表),用来存储键值对(key-value)映射.散列表是一种数组和链表的结合体,结构图如下: 简单来说散列表 ...
- Maven教程:tutorialspoint-maven
来自turorialspoint的Maven教程(英文),官网:http://www.tutorialspoint.com/maven/index.htm 这个教程在国内已经被翻译成中文,官网:htt ...
- MFC改变控件颜色
from http://www.cppblog.com/FandyM/archive/2010/07/21/120972.aspx MFC应用程序中,要改变控件的背景色可通过重载OnCtlColor( ...
- 【linux】CentOS编译程序报错 修复 ./Modules/_ssl.c:64:25: 致命错误:openssl/rsa.h:没有那个文件或目录
如果你在编译时遇到这个错误,这可能是下面的原因:你尝试编译的程序使用OpenSSL,但是需要和OpenSSL链接的文件(库和头文件)在你Linux平台上缺少. 所以在CentOS下, 退到根路径,[需 ...
- C# 通过WebService方式 IIS发布网站 上传文件到服务器[转]
http://blog.sina.com.cn/s/blog_517cae3c0102v0y7.html 应用场景:要将本地的文件 上传到服务器的虚拟机上 网络环境:公司局域网(如下图中第二种) 开发 ...