模数未返回正确值

时间:2018-08-04 05:00:18

标签: java modulo

我的一段代码:

##App.js
    /**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
const WEBVIEW_REF = "WEBVIEW_REF";
import React, { Component } from "react";
import { WebView } from "react-native";
import {
  AppRegistry,
  Platform,
  StyleSheet,
  Text,
  View,
  Button,
  Image
} from "react-native";
const instructions = Platform.select({
  ios: "Press Cmd+R to reload,\n" + "Cmd+D or shake for dev menu",
  android:
    "Double tap R on your keyboard to reload,\n" +
    "Shake or press menu button for dev menu"
});
export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      canGoBack: false,
      canGoForward: false,
      loading: false
    };
  }
  render() {
    return (
      <View style={{ flex: 1 }}>
        <View style={{ backgroundColor: "#FE9A2E", flex: 0.15 }}>
          <Image
            source={require("./header.png")}
            style={{ alignSelf: "center" }}
          />
        </View>
        <View style={{ flex: 0.8 }}>
          <WebView
            ref={WEBVIEW_REF}
            source={{ uri: "https://www.google.co.jp/" }}
            onNavigationStateChange={this.onNavigationStateChange.bind(this)}
          />
        </View>
        <View style={{ alignSelf: "center", flex: 0.08, flexDirection: "row" }}>
          <View style={styles.buttonContainer}>
            <Button
              onPress={this.onBack.bind(this)}
              title="←"
              color="#FE9A2E"
            />
          </View>
          <View style={styles.buttonContainer}>
            <Button
              onPress={this.onReload.bind(this)}
              title="↺"
              color="#FE9A2E"
            />
          </View>
          <View style={styles.buttonContainer}>
            <Button
              onPress={this.onForward.bind(this)}
              title="→"
              color="#FE9A2E"
            />
          </View>
        </View>
      </View>
    );
  }
  onBack() {
    this.refs[WEBVIEW_REF].goBack();
  }

  onForward() {
    this.refs[WEBVIEW_REF].goForward();
  }

  onReload() {
    this.refs[WEBVIEW_REF].reload();
  }

  onNavigationStateChange(navState) {
    this.setState({
      canGoBack: navState.canGoBack,
      canGoForward: navState.canGoForward,
      loading: navState.loading
    });
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  },
  welcome: {
    fontSize: 20,
    textAlign: "center",
    margin: 10
  },
  instructions: {
    textAlign: "center",
    color: "#333333",
    marginBottom: 5
  },
  buttonContainer: {
    width: 100,
    margin: 6
  }
});
AppRegistry.registerComponent("App", () => App);

测试用例显示 public static void main (String args[]) throws IOException { while ((sCurrentLine = bufferedReader.readLine()) != null) { if(count == 1){ // Reading via bufferedReader int resultNumber = 0; if (nthModifier == 0) { System.out.println("0"); } else if (nthModifier == 1) { System.out.println("0 1"); } else { int a = Integer.valueOf(nextLineString[0]); int b = Integer.valueOf(nextLineString[1]); int c = Integer.valueOf(nextLineString[2]); for (int i = kPreviousNumbers; i < nthModifier; i++) { int nextNumber = Math.floorMod(Math.floorMod(a , 1000000007) * Math.floorMod(b , 1000000007) * Math.floorMod(c, 1000000007), 1000000007); resultNumber = nextNumber; a = b; b = c; c = nextNumber; } } System.out.print(resultNumber + " "); } count++; } } 。但是我最终得到的结果是923527,我试图以三个为单位进行乘法运算,其概念与斐波那契数列相同,但是不是加法,而是以三个数的集合相乘。应用模数时我缺少什么吗?

1 个答案:

答案 0 :(得分:1)

您使用过:(a * b * c) = ( (a % mod) * (b % mod) * (c % mod) ) % mod ..

如果(a % mod)(b % mod)(c % mod)等于大数。 (mod-1)将会溢出,您将无法获得正确的结果。为了避免这种溢出,您需要执行以下操作:

( a * b * c ) % mod = ( ( (a % mod) * (b % mod) ) % mod ) * (c % mod) ) % mod

更改此:

Math.floorMod(Math.floorMod(a , 1000000007) * Math.floorMod(b , 1000000007) * Math.floorMod(c, 1000000007), 1000000007);

收件人

Math.floorMod(Math.floorMod(Math.floorMod(a , 1000000007) * Math.floorMod(b , 1000000007), 1000000007) * Math.floorMod(c, 1000000007), 1000000007);